Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public String toXML(String source) throws MessageSerializerException {
Message message = null;
source = source.trim();

if (source.length() > 0 && source.charAt(0) == '<') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking - How can this be documented in the release notes?

if (serializationProperties.isAllowXml() && source.length() > 0 && source.charAt(0) == '<') {
if (serializationProperties.isUseStrictValidation()) {
// If the message is XML and strict validation is needed, we'll need to create a message to be encoded.
message = serializationXmlParser.parse(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class HL7v2SerializationProperties extends SerializationProperties {
private boolean handleSubcomponents = true;
private boolean useStrictParser = false;
private boolean useStrictValidation = false;
private boolean allowXml = false;
private boolean stripNamespaces = false;
private String segmentDelimiter = "\\r";
private boolean convertLineBreaks = true;
Expand All @@ -35,6 +36,7 @@ public Map<String, DataTypePropertyDescriptor> getPropertyDescriptors() {
properties.put("handleSubcomponents", new DataTypePropertyDescriptor(handleSubcomponents, "Parse Subcomponents", "Parse subcomponents (applies to Non-Strict Parser only).", PropertyEditorType.BOOLEAN));
properties.put("useStrictParser", new DataTypePropertyDescriptor(useStrictParser, "Use Strict Parser", "Parse messages based upon strict HL7 specifications.", PropertyEditorType.BOOLEAN));
properties.put("useStrictValidation", new DataTypePropertyDescriptor(useStrictValidation, "Validate in Strict Parser", "Validate messages using HL7 specifications (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
properties.put("allowXml", new DataTypePropertyDescriptor(allowXml, "Allow XML Messages", "Allow input that appears to be XML-formatted to be parsed as HL7-XML instead of being rejected as invalid ER7 (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
properties.put("stripNamespaces", new DataTypePropertyDescriptor(stripNamespaces, "Strip Namespaces", "Strips namespace definitions from the transformed XML message (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
properties.put("segmentDelimiter", new DataTypePropertyDescriptor(segmentDelimiter, "Segment Delimiter", "This is the input delimiter character(s) expected to occur after each segment.", PropertyEditorType.STRING));
properties.put("convertLineBreaks", new DataTypePropertyDescriptor(convertLineBreaks, "Convert Line Breaks", "Convert all styles of line breaks (CRLF, CR, LF) in the raw message to the segment delimiter. ", PropertyEditorType.BOOLEAN));
Expand All @@ -61,6 +63,10 @@ public void setProperties(Map<String, Object> properties) {
this.useStrictValidation = (Boolean) properties.get("useStrictValidation");
}

if (properties.get("allowXml") != null) {
this.allowXml = (Boolean) properties.get("allowXml");
}

if (properties.get("stripNamespaces") != null) {
this.stripNamespaces = (Boolean) properties.get("stripNamespaces");
}
Expand Down Expand Up @@ -107,6 +113,14 @@ public void setUseStrictValidation(boolean useStrictValidation) {
this.useStrictValidation = useStrictValidation;
}

public boolean isAllowXml() {
return allowXml;
}

public void setAllowXml(boolean allowXml) {
this.allowXml = allowXml;
}

public boolean isStripNamespaces() {
return stripNamespaces;
}
Expand Down Expand Up @@ -152,6 +166,7 @@ public Map<String, Object> getPurgedProperties() {
purgedProperties.put("handleRepetitions", handleRepetitions);
purgedProperties.put("handleSubcomponents", handleSubcomponents);
purgedProperties.put("useStrictParser", useStrictParser);
purgedProperties.put("allowXml", allowXml);
purgedProperties.put("stripNamespaces", stripNamespaces);
purgedProperties.put("convertLineBreaks", convertLineBreaks);
return purgedProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mirth.connect.plugins.datatypes.hl7v2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -15,11 +16,22 @@

public class ER7SerializerTest {
private static ER7Serializer serializer;

private static ER7Serializer strictSerializer;
private static ER7Serializer strictSerializerAllowXml;

@BeforeClass
public static void setupClass() throws Exception {
SerializerProperties serializerProperties = new SerializerProperties(new HL7v2SerializationProperties(), new HL7v2DeserializationProperties(), null);
serializer = new ER7Serializer(serializerProperties);

HL7v2SerializationProperties strictProperties = new HL7v2SerializationProperties();
strictProperties.setUseStrictParser(true);
strictSerializer = new ER7Serializer(new SerializerProperties(strictProperties, new HL7v2DeserializationProperties(), null));

HL7v2SerializationProperties strictPropertiesAllowXml = new HL7v2SerializationProperties();
strictPropertiesAllowXml.setUseStrictParser(true);
strictPropertiesAllowXml.setAllowXml(true);
strictSerializerAllowXml = new ER7Serializer(new SerializerProperties(strictPropertiesAllowXml, new HL7v2DeserializationProperties(), null));
}

@Test
Expand Down Expand Up @@ -53,4 +65,27 @@ public void testValidFromXMLWithExternalDTD() throws Exception {

assertFalse(exceptionThrown);
}

@Test
public void testToXmlWithStrictParserRejectsXmlInputByDefault() throws Exception {
String xmlDisguisedAsHl7 = "<foo><bar>notreallyhl7</bar></foo>";

boolean exceptionThrown = false;
try {
strictSerializer.toXML(xmlDisguisedAsHl7);
} catch (MessageSerializerException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this specifically inspect the exception message and cause? That way it validates it threw because of the XML parsing and not because of some other serialization fault.

What is the output of this test before and after the other code changes?

exceptionThrown = true;
}

assertTrue(exceptionThrown);
}

@Test
public void testToXmlWithStrictParserAllowsXmlInputWhenOptedIn() throws Exception {
String xmlDisguisedAsHl7 = "<foo><bar>notreallyhl7</bar></foo>";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this to a constant since its used in multiple tests


String result = strictSerializerAllowXml.toXML(xmlDisguisedAsHl7);

assertEquals(xmlDisguisedAsHl7, result);
}
}
Loading