crs4 / hl7apy

Python library to parse, create and handle HL7 v2 messages.
http://crs4.github.io/hl7apy/
MIT License
224 stars 88 forks source link

Unable to parse v2.3 messages #39

Closed nguyenvpd closed 5 years ago

nguyenvpd commented 5 years ago

Trying to parse this message:

MSH|^~\&|SOURCEEHR|WA|MIRTHDST|WA|201611111111||SIU^S12|MSGID10001|P|2.3|
SCH|30000001|||||CHECKUP|ROUTINE|NORMAL|20|MIN|201611111400|||||JOHN||||(206)555-5309|||||ARRIVED
PD1||||123456^DOCTOR^BOB^T^^DR|
PID|1|100001^^^1^MRN1|900001||DOE^JOHN^^^^||19601111|M||WH|111 THAT PL^^HERE^WA^98020^USA||(206)555-5309|||M|NON|999999999|
PV1|1|O|||||123456^DOCTOR^BOB^T^^DR|^|||||||||||||||||||||||||||||||||||||||||||||
AIL|||123456^DOCTOR^BOB^T^^DR|||201611110000|||20|MIN
AIG|||7777|||||201611110000|||20|MIN
NTE|1||ANNUAL HLCK^^|
AIP|1||PBN^LISAPORTER|50|||||||||

found here: https://github.com/aws-samples/aws-big-data-blog/blob/master/aws-blog-mirth-healthcare-hub/samples/hl7-siu-s12.txt

fails with hl7apy.exceptions.ChildNotValid: <Component CM_MSG_1 (MESSAGE_TYPE) of type ID> is not a valid child for <Field MSH_9 (MESSAGE_TYPE) of type CM_MSG>

It looks like this is due to this method: https://github.com/crs4/hl7apy/blob/v1.3.0/hl7apy/core.py#L89 where we try to parent, index = child_name.split("_") on a name that will actually have 3 values after the .split(). Examples of those here: https://github.com/crs4/hl7apy/blob/v1.3.0/hl7apy/v2_3/datatypes.py#L94

It may be worth doing something like this in that method instead:

        child_name_segments = child_name.split("_")
        index = child_name_segments.pop()
        parent = '_'.join(child_name_segments)
        int(index)
svituz commented 5 years ago

Hi @nguyenvpd, you're right, the problem is with that split. I'll mark it as a bug and I'll try to fix this ASAP, since it make it impossible to use version 2.3 messages. Thank you, Vittorio

svituz commented 5 years ago

I've fixed the bug in version 1.3.1