nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
365 stars 104 forks source link

Incorrect parsing of StructureDefinition element ids for polymorphic elements #124

Closed ArdonToonstra closed 1 year ago

ArdonToonstra commented 1 year ago

Description

I am trying to parse our created logical models in StructueDefinitions. For a few elements, we have defined polymorphic elements, like the JSON snippet below. I believe this is valid FHIR.

 {
   "id": "LaboratoryTestResult.LaboratoryTest.TestResult[x]",
   "path": "LaboratoryTestResult.LaboratoryTest.TestResult[x]",
    "short": "TestResult",

However, If I try to parse this resource, I get errors, like this:

2023-03-17 18:27:01.457242  error   Parsing error, skipped: package/LogicalModel-HdBe-LaboratoryTestResult.json. Error message: 3 validation errors for StructureDefinition
differential -> element -> 3 -> id
  string does not match regex "^[A-Za-z0-9\-.]+$" (type=value_error.str.regex; pattern=^[A-Za-z0-9\-.]+$)
differential -> element -> 7 -> id
  string does not match regex "^[A-Za-z0-9\-.]+$" (type=value_error.str.regex; pattern=^[A-Za-z0-9\-.]+$)
differential -> element -> 8 -> id
  string does not match regex "^[A-Za-z0-9\-.]+$" (type=value_error.str.regex; pattern=^[A-Za-z0-9\-.]+$)

This is the related definition of the FHIR element: https://www.hl7.org/fhir/element-definitions.html#Element.id which is of type string The constraint is based on: https://www.hl7.org/fhir/datatypes.html#id for type id.

Should this constraint apply to ElementDefinition.id ? Also, FHIR itself uses [x] in the Element.id : https://www.hl7.org/fhir/observation.profile.json.html

What I Did

if resource.get("resourceType") == "StructureDefinition" and resource.get("kind") == "logical":
                try:
                    struct_def = StructureDefinition.parse_obj(resource)
                    struct_defs.append(struct_def)
                except Exception as e:
                    write_log('error', f'Parsing error, skipped: {member.name}. Error message: {str(e)}' )
                    print(str(member.name) + ': Parsing error, skipped')
                    pass  
delcroip commented 1 year ago

see #55

my regex


from fhir.resources.fhirtypes import Id
Id.configure_constraints(regex=re.compile(r"^[A-Za-z0-9\-\.]+(\[x\](\.[a-zA-Z]+)?)?(:[A-Za-z0-9\-.]+(\[x\](\.[a-zA-Z]+)?)?)?$"))      
Id.configure_constraints(max_length=128)      
``
ArdonToonstra commented 1 year ago

Thanks! That works. Will close the issue.