The currect logic always creates a whole new type component which will remove any constraints!
static void correctIdElement(IElementList elements)
{
if (elements is null) return;
var idElements = elements.Element.Where(e => Regex.IsMatch(e.Path, @"^[a-zA-Z]+\.id$"));
if (idElements.Count() == 1 && idElements.Single().Type.Count == 1)
{
idElements.Single().Type = new() { new ElementDefinition.TypeRefComponent { Code = "id" } };
}
}
Maybe something like this:
if (idElements.Count() == 1)
{
var idElement = idElements.First();
if (idElement.Type.Count == 1 && idElement.Type[0].Code != "id")
idElement.Type[0].Code = "id";
}
PS. The use of Single() is unnecessary since Count() == 1 has already been checked!
Also the extension "http://hl7.org/fhir/StructureDefinition/structuredefinition-fhir-type" should be removed when its there.