ensure ElementDefinition properties with longer names are processed

first in order to find the property that most matches the given nodeName
This commit is contained in:
michael.i.calderero 2016-12-02 21:13:09 -06:00
parent ffefb79bf1
commit 7a203fa7d5
3 changed files with 358 additions and 322 deletions

View File

@ -3,6 +3,9 @@ package org.hl7.fhir.dstu3.elementmodel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
@ -289,7 +292,16 @@ public class XmlParser extends ParserBase {
}
private Property getElementProp(List<Property> properties, String nodeName) {
for (Property p : properties)
List<Property> propsSortedByLongestFirst = new ArrayList<Property>(properties);
// sort properties according to their name longest first, so .requestOrganizationReference comes first before .request[x]
// and therefore the longer property names get evaluated first
Collections.sort(propsSortedByLongestFirst, new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
return o2.getName().length() - o1.getName().length();
}
});
for (Property p : propsSortedByLongestFirst)
if (!p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR) && !p.getDefinition().hasRepresentation(PropertyRepresentation.XMLTEXT)) {
if (p.getName().equals(nodeName))
return p;

View File

@ -3,6 +3,7 @@ package org.hl7.fhir.dstu3.hapi.validation;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -200,4 +201,27 @@ public class ResourceValidatorDstu3Test {
assertThat(messageString, not(containsString("valueResource")));
}
@Test
public void testValidateDifferentPropertyButSameStartsWithPath() throws Exception {
EnrollmentResponse fhirObj = new EnrollmentResponse();
Organization org = new Organization();
org.setId("1");
fhirObj.setRequestOrganization(new Reference(org));
String input = ourCtx.newXmlParser().encodeResourceToString(fhirObj);
FhirValidator validator = ourCtx.newValidator();
validator.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = validator.validateWithResult(input);
assertEquals(3, result.getMessages().size());
fhirObj = new EnrollmentResponse();
Identifier ident = new Identifier().setSystem("a").setValue("b");
fhirObj.setRequest(ident);
input = ourCtx.newXmlParser().encodeResourceToString(fhirObj);
result = validator.validateWithResult(input);
assertEquals(2, result.getMessages().size());
}
}