Efferent-Health / HL7-dotnetcore

Lightweight HL7 C# parser and composer for .NET Core, .NET Standard and .NET 5+
MIT License
229 stars 115 forks source link

HasRepetitions inaccurate when using Dot Notation #90

Closed omarserenity closed 1 year ago

omarserenity commented 3 years ago

Example Message:

MSH|^~\&|IA PHIMS Stage^2.16.840.1.114222.4.3.3.5.1.2^ISO|IA Public Health Lab^2.16.840.1.114222.4.1.10411^ISO|IA.DOH.IDSS^2.16.840.1.114222.4.3.3.19^ISO|IADOH^2.16.840.1.114222.4.1.3650^ISO|201203312359||ORU^R01^ORU_R01|2.16.840.1.114222.4.3.3.5.1.2-20120314235954.325|T|2.5.1|||AL|NE|USA||||PHLabReport-Ack^^2.16.840.1.113883.9.10^ISO
SFT|Orion Health^L|2.4.3.52854|Rhapsody|2.4.3.52854||20070725111624
PID|1||14^^^IA PHIMS Stage&2.16.840.1.114222.4.3.3.5.1.2&ISO^PI^IA Public Health Lab&2.16.840.1.114222.4.1.10411&ISO~145^^^IA PHIMS Stage&2.16.840.1.114222.4.3.3.5.1.2&ISO^PI^IA Public Health Lab&2.16.840.1.114222.4.1.10411&ISO||Finn^Huckleberry^^^^^L||19630815|M||2106-3^White^CDCREC^^^^04/24/2007~1002-5^American Indian or Alaska Native^CDCREC^^^^04/24/2007|721SPRING STREET^^GRINNELL^IA^50112^USA^H|||||M^Married^HL70002^^^^2.5.1||||||H^Hispanic orLatino^HL70189^^^^2.5.1
ORC|RE||986^IA PHIMS Stage^2.16.840.1.114222.4.3.3.5.1.2^ISO||A|||||||^SAWYER TOMMD^^^^^^^^L|||||||||MISSOURI DEPARTMENT OF HEALTH LABORATORY - MISSOURI DEPARTMENT OF HEALTHLABORATORY^L|307 W MCCARTY ST^^JEFFERSON CITY^MO^65101^USA^B|^WPN^PH^^1^^5555555
OBR|1||986^IA PHIMS Stage^2.16.840.1.114222.4.3.3.5.1.2^ISO|625-4^Bacteria identified in Stool byCulture^LN^^^^2.33^^Enteric Culture|||20120301|||||||||^SAWYER TOMMD^^^^^^^^L||||||201203140957|||P
NTE|1|L|Enteric culture includes testing for Salmonella, Shigella, Campylobacter, Yersinia, E.coliO157:H7 \T\ other STECs, and Aeromonas|RE^Remark^HL70364^^^^2.5.1
OBX|1|CWE|625-4^Bacteria identified in Stool by Culture^LN^^^^2.33^^result1|1|27268008^Salmonella^SCT^^^^20090731^^Salmonella species|||A^A^HL70078^^^^2.5|||P|||20120301|||^^^^^^^^Bacterial Culture||201203140957||||State Hygienic Laboratory^L^^^^IA Public HealthLab&2.16.840.1.114222.4.1.10411&ISO^FI^^^16D0648109|State Hygienic Laboratory^UI Research Park -Coralville^Iowa City^IA^52242-5002^USA^B^^19103|^Atchison^Christopher^^^^^^^L
SPM|1|^2012999999&IA PHIMS Stage&2.16.840.1.114222.4.3.3.5.1.2&ISO||119339001^Stool specimen(specimen)^SCT^SL^Stool^L^20090731^v unknown|||||||P^Patient^HL70369^^^^2.5.1||||||20120301|201203061451

Example Code:

Message hl7 = new Message(msg);
hl7.ParseMessage();

bool PID3RepsByDotNotation = hl7.HasRepetitions("PID.3");
bool PID3RepsByField = hl7.Segments("PID")[0].Fields(3).HasRepetitions;

Console.WriteLine("Multiple PID.3 (By DotNotation) {0}", PID3RepsByDotNotation ? "Yes" : "No"); // Prints No
Console.WriteLine("Multiple PID.3 (By Field) {0}", PID3RepsByField ? "Yes" : "No"); // Prints Yes
omarserenity commented 3 years ago

In Message.cs, method public bool HasRepetitions(string strValueFormat) (line 448), there is a call to this.getField(segment, allComponents[1]);

In Message.cs, method private Field getField(Segment segment, string index), it will never return all repetitions of a field, if it has repetitions (line 703-704):

if (field.HasRepetitions)
    return field.RepetitionList[repetition];

Thus, when control returns to Message.cs, HasRepetitions, line 448, var field now contains just the first repetition, so when it checks field.HasRepetitions on line 450, there is no repetition character in the field, so HasRepetitions incorrectly returns false.

In contrast, calling

hl7.Segments("PID")[0].Fields(3).HasRepetitions;

returns all repetitions of the field in hl7.Segments("PID")[0].Fields(3), so the field returned contains the repetition character and so HasRepetitions correctly returns True.

jaime-olivares commented 3 years ago

Hi, thanks for reviewing. Can you post a Pull Request?

jaime-olivares commented 1 year ago

This is solved in the last commit, nuget version 2.34