From 08633066c69ee8d4146eead941044220acd8b3f5 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Mon, 7 Sep 2015 10:08:11 -0400 Subject: [PATCH] Fix #216 - Unexpected attributes when parsing composite elements should be logged --- .../java/ca/uhn/fhir/parser/ParserState.java | 2 + hapi-fhir-jpaserver-example/pom.xml | 1 + .../uhn/fhir/parser/XmlParserDstu2Test.java | 73 +++++++++++++++++++ src/changes/changes.xml | 4 + 4 files changed, 80 insertions(+) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java index 9205e7045d0..692fc2a3615 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java @@ -1552,6 +1552,8 @@ class ParserState { } } else if ("url".equals(theName) && myInstance instanceof ExtensionDt) { ((ExtensionDt) myInstance).setUrl(theValue); + } else { + super.attributeValue(theName, theValue); } } diff --git a/hapi-fhir-jpaserver-example/pom.xml b/hapi-fhir-jpaserver-example/pom.xml index a30cf4638ed..59e9cfe9a86 100644 --- a/hapi-fhir-jpaserver-example/pom.xml +++ b/hapi-fhir-jpaserver-example/pom.xml @@ -194,6 +194,7 @@ hapi-fhir-testpage-overlay + src/main/webapp/WEB-INF/web.xml diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index 262b0e2c7e7..48196c405a7 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -1,5 +1,6 @@ package ca.uhn.fhir.parser; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.emptyOrNullString; import static org.hamcrest.Matchers.not; @@ -11,6 +12,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import java.io.StringReader; import java.util.ArrayList; @@ -22,11 +27,13 @@ import java.util.UUID; import org.apache.commons.io.IOUtils; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; +import org.hamcrest.collection.IsEmptyCollection; import org.hamcrest.core.StringContains; import org.hamcrest.text.StringContainsInOrder; import org.hl7.fhir.instance.model.api.IBaseResource; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.ArgumentCaptor; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; @@ -64,6 +71,7 @@ import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum; import ca.uhn.fhir.model.dstu2.valueset.ContactPointSystemEnum; import ca.uhn.fhir.model.dstu2.valueset.DocumentReferenceStatusEnum; +import ca.uhn.fhir.model.dstu2.valueset.IdentifierTypeCodesEnum; import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; import ca.uhn.fhir.model.dstu2.valueset.MaritalStatusCodesEnum; import ca.uhn.fhir.model.dstu2.valueset.NameUseEnum; @@ -74,6 +82,7 @@ import ca.uhn.fhir.model.primitive.DateTimeDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.StringDt; +import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; import ca.uhn.fhir.rest.client.IGenericClient; import ca.uhn.fhir.rest.server.Constants; @@ -205,6 +214,70 @@ public class XmlParserDstu2Test { assertTrue(parsed.getEntries().get(0).getResource().getId().isEmpty()); } + /** + * See #216 + */ + @Test + public void testEncodeAndParseIdentifierDstu2() { + IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); + + Patient patient = new Patient(); + patient.addIdentifier().setSystem("SYS").setValue("VAL").setType(IdentifierTypeCodesEnum.MR); + + String out = xmlParser.encodeResourceToString(patient); + ourLog.info(out); + + //@formatter:off + assertThat(out, stringContainsInOrder("", + "", + "", + "", + "", + "", + "", + "", + "", + "")); + //@formatter:on + + patient = ourCtx.newXmlParser().parseResource(Patient.class, out); + assertThat(patient.getIdentifier().get(0).getType().getValueAsEnum(), contains(IdentifierTypeCodesEnum.MR)); + assertEquals("http://hl7.org/fhir/v2/0203", patient.getIdentifier().get(0).getType().getCoding().get(0).getSystem()); + assertEquals("MR", patient.getIdentifier().get(0).getType().getCoding().get(0).getCode()); + } + + /** + * See #216 + */ + @Test + public void testParseMalformedIdentifierDstu2() { + + // This was changed from 0.5 to 1.0.0 + + //@formatter:off + String out = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + IParserErrorHandler errorHandler = mock(IParserErrorHandler.class); + + IParser p = ourCtx.newXmlParser(); + p.setParserErrorHandler(errorHandler); + + Patient patient = p.parseResource(Patient.class, out); + assertThat(patient.getIdentifier().get(0).getType().getValueAsEnum(), IsEmptyCollection.empty()); + + ArgumentCaptor capt = ArgumentCaptor.forClass(String.class); + verify(errorHandler, times(1)).unknownAttribute(any(IParseLocation.class), capt.capture()); + + assertEquals("value", capt.getValue()); + } + @Test public void testEncodeAndParseContained() { IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 250b414b331..2b2062a9aee 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -164,6 +164,10 @@ library is in use (earlier than 4.0). Thanks to Bill de Beaubien for reporting! + + Invalid/unexpected attributes found when parsing composite elements + should be logged or reported to the parser error handler +