skalarsystems / fhirzeug

A Python FHIR specification parser and class generator
Apache License 2.0
17 stars 1 forks source link

Can't instanciate a BundleEntry with an already instanciated Resource. #45

Closed Wauplin closed 4 years ago

Wauplin commented 4 years ago

I get a ValidationError : 'OperationOutcome' object is not subscriptable when running the above code. What I want to do is to instanciate the BundleEntry with the outcome instead of a dictionnary.

issue = r4.OperationOutcomeIssue(code="not-found", severity="warning")
outcome = r4.OperationOutcome(issue=[issue])
entry = r4.BundleEntry(resource=outcome)

The exception is thrown due to this code in r4.py :

class BundleEntry(BackboneElement):

    (...)

    resource: typing.Optional["Resource"]

    """ A resource in the bundle.
    Type `Resource` (represented as `dict` in JSON). """
    @pydantic.validator("resource", pre=True,  each_item=True)
    def resource_factory(cls, value):
        return from_dict(value)

BundleEntry expect only to get a dictionnary. What I suggest to do add a line to the code :

    @pydantic.validator("resource", pre=True,  each_item=True)
    def resource_factory(cls, value):
        if isinstance(value, Resource):
             return value
        return from_dict(value)

Change must be done here : https://github.com/skalarsystems/fhirzeug/blob/f08330ab4f3e67eeedc1b45e91926c8155db3a04/fhirzeug/generators/python_pydantic/templates/resource.py.jinja2#L52