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

Parsing XML byte string MESH acknowledgment response #144

Closed lshironschec closed 8 months ago

lshironschec commented 9 months ago

fhir.resources==7.0.2 Python 3.11.0 Windows 10

Description

Attempting to use Bundle.parse_raw to convert XML byte string into a Bundle object.

XML byte string content is an acknowledgment response from NHS MESH ITK Test Harness, which includes OperationOutcome resource. XML Content :

<Bundle
    xmlns="http://hl7.org/fhir">
    <id value="cd15aa48-2d93-4854-a54e-9db64f849486"/>
    <meta>
        <profile value="https://fhir.nhs.uk/STU3/StructureDefinition/ITK-Message-Bundle-1"/>
    </meta>
    <identifier>
        <value value="18489fa2-830d-458c-b8b8-dcaf4482bfb5"/>
    </identifier>
    <type value="message"/>
    <entry>
        <fullUrl value="urn:uuid:8079c82d-3a33-4998-b0fa-92ecc9665932"/>
        <resource>
            <MessageHeader>
                <id value="8079c82d-3a33-4998-b0fa-92ecc9665932"/>
                <meta>
                    <profile value="https://fhir.nhs.uk/STU3/StructureDefinition/ITK-MessageHeader-2"/>
                </meta>
                <extension url="https://fhir.nhs.uk/STU3/StructureDefinition/Extension-ITK-MessageHandling-2">
                    <extension url="InfAckRequested">
                        <valueBoolean value="false"/>
                    </extension>
                    <extension url="RecipientType">
                        <valueCoding>
                            <system value="https://fhir.nhs.uk/STU3/CodeSystem/ITK-RecipientType-1"/>
                            <code value="FI"/>
                            <display value="For Information"/>
                        </valueCoding>
                    </extension>
                </extension>
                <event>
                    <system value="https://fhir.nhs.uk/STU3/CodeSystem/ITK-MessageEvent-2"/>
                    <code value="ITK008M"/>
                    <display value="ITK Response"/>
                </event>
                <sender>
                    <reference value="urn:uuid:b31bb32c-93c1-4068-9260-3f091b4487db"/>
                </sender>
                <timestamp value="2023-11-01T12:47:50Z"/>
                <source>
                    <endpoint value="urn:nhs-uk:addressing:ods:ITKTestHarness"/>
                </source>
                <response>
                    <identifier value="bab555e7-eda7-4b76-859e-f9af159cfd3c"/>
                    <code value="fatal-error"/>
                    <details>
                        <reference value="urn:uuid:5ab142e6-bb0d-4c13-9e71-531aa207786f"/>
                    </details>
                </response>
            </MessageHeader>
        </resource>
    </entry>
    <entry>
        <fullUrl value="urn:uuid:b31bb32c-93c1-4068-9260-3f091b4487db"/>
        <resource>
            <Practitioner>
                <id value="b31bb32c-93c1-4068-9260-3f091b4487db"/>
                <meta>
                    <profile value="https://fhir.nhs.uk/STU3/StructureDefinition/CareConnect-ITK-Header-Practitioner-1"/>
                </meta>
                <name>
                    <family value="Harness"/>
                    <given value="ITK Test"/>
                </name>
            </Practitioner>
        </resource>
    </entry>
    <entry>
        <fullUrl value="urn:uuid:5ab142e6-bb0d-4c13-9e71-531aa207786f"/>
        <resource>
            <OperationOutcome>
                <id value="5ab142e6-bb0d-4c13-9e71-531aa207786f"/>
                <meta>
                    <profile value="https://fhir.nhs.uk/STU3/StructureDefinition/ITK-Response-OperationOutcome-1"/>
                </meta>
                <issue>
                    <severity value="fatal"/>
                    <code value="processing"/>
                    <details>
                        <coding>
                            <system value="https://fhir.nhs.uk/STU3/CodeSystem/ITK-ResponseCodes-1"/>
                            <code value="10001"/>
                            <display value="Handling Specification Error"/>
                        </coding>
                    </details>
                </issue>
            </OperationOutcome>
        </resource>
    </entry>
</Bundle>

What I Did

When sending FHIR messages to the test harness, the FHIR release required is STU3. I have tried to parse the ACK using the following :

from fhir.resources.STU3.bundle import Bundle as STU3Bundle

message_content_xml_bytes = client.retrieve_message(message_id).read()
message_content_fhir = STU3Bundle.parse_raw(message_content_xml_bytes, content_type='text/xml')

Traceback:

    message_content_fhir = STU3Bundle.parse_raw(message_content_xml_bytes, content_type='text/xml')
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\fhirabstractmodel.py", line 258, in parse_raw
    obj = load_str_bytes(
          ^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\__init__.py", line 96, in load_str_bytes
    obj = xml_loads(extra["cls"], b, **params)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 1038, in xml_loads
    return node.to_fhir(cls)
           ^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 942, in to_fhir
    value = Node.get_fhir_value(child, field)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 895, in get_fhir_value
    value = obj.to_fhir(klass_)
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 942, in to_fhir
    value = Node.get_fhir_value(child, field)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 895, in get_fhir_value
    value = obj.to_fhir(klass_)
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 905, in to_fhir
    klass_ = get_fhir_root_module(f_release).get_fhir_model_class(child.name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\site-packages\fhir\resources\core\utils\xml.py", line 77, in get_fhir_root_module
    FHIR_ROOT_MODULES[fhir_release] = importlib.import_module(mod_name)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python 3.11.0\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'fhir.resources.R4'

I have also tried the same using other FHIR release types (default / R4B) but then get validation errors regarding the "event" property of the message.

Please let me know if you require any further information.

nazrulworld commented 9 months ago

@lshironschec thanks for your error report. I will look on it as soon as possible. Though it could take a bit time, as I am so busy now with my professional works.

lshironschec commented 9 months ago

bump <3