beda-software / fhir-py-types

Convert FHIR StructureDefinition into Python type annotations
BSD 3-Clause "New" or "Revised" License
15 stars 1 forks source link

WIP: Validate generated Pydantic models on Synthea. #2

Closed axelv closed 1 year ago

axelv commented 1 year ago

First of all thx for starting this awesome library!

This is WIP but I'd like your feedback on how to proceed. My goal is to validate the generated models on Synthea generated FHIR resources so we that we are confident that the models work correctly. During this attempt I encountered several issues:

  1. Pydantic models have circular references which need to be resolved using update_forward_refs. I added this in ast.py

  2. We will need a union type of all resources to parse resources inside a Bundle. Currently, Pydantic tries to validate Bundle.entry.resource as a Resource but actually, it should validate it as a Union of all possible resources. I added the automatic generation of such a union type (see: AnyResource) to ast.py.

  3. resourceType was missing. I tried to generate it automatically but failed to do it correctly for all resources because some resources are split into multiple models. I think this is the result of polymorphic elements. Ex Immunization = _ImmunizationDateTime | _ImmunizationString. Having two models with the same resourceType is not practical because then we can't use Pydantic's Field(discriminator='resourceType') for Union types

ToDo:

Any thoughts on the issues above? Feedback on code style and directory structure is welcome!

m0rl commented 1 year ago

Thanks for helping us making this library even better! There were some updates to the main branch recently that include resourceType generation and automation of update_forward_refs for BaseModel resources. Please let us know if there are any issues left with that. As for the AnyResource type - I believe it's a great idea and we can also extend it to other contexts where we now have Resource (like contained property). Will look into it!

m0rl commented 1 year ago

Quick update on the proposed changes: synthea-generated resources validation should now work in the main branch.

axelv commented 1 year ago

Very cool @m0rl! Meanwhile I'm looking for an elegant way to implement the Any* types for abstract structures like Resource, DomainResource. Not sure if this also holds for Element and BackboneElement (these also have abstract = true).

axelv commented 1 year ago

Very cool @m0rl! Meanwhile I'm looking for an elegant way to implement the Any* types for abstract structures like Resource, DomainResource. Not sure if this also holds for Element and BackboneElement (these also have abstract = true).

Nevermind. I just notice that you already fixed the AnyResource generation. Good job Bundle.parse_file on the Synthea works fine👍 🚀

Pytest results on 1180 Bundles

Total number of tests expected to run: 1180
Total number of tests run: 1180
Total number of tests passed: 1180
Total number of tests failed: 0
Total number of tests failed with errors: 0
Total number of tests skipped: 0
Total number of tests with no result data: 0
Finished running tests!
m0rl commented 1 year ago

Thanks for testing it! We also plan to add synthea bundle validation regression tests in the near future (together with the regression tests for R4/R5 spec parsing and typecheck).