From b147083f5a028222a40eb269605f6358f2aaf10b Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Wed, 2 Mar 2016 22:18:33 -0500 Subject: [PATCH] Fix #308 - Remove unneded warning on parsing declared extension --- .../java/ca/uhn/fhir/parser/ParserState.java | 9 ++++++++ .../uhn/fhir/parser/JsonParserDstu2Test.java | 21 ++++++++++++++++- .../ca/uhn/fhir/parser/ReportObservation.java | 2 ++ .../uhn/fhir/parser/XmlParserDstu2Test.java | 23 +++++++++++++++++-- src/changes/changes.xml | 4 ++++ 5 files changed, 56 insertions(+), 3 deletions(-) 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 60bf57d68d2..b5eb1057d63 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 @@ -1480,6 +1480,15 @@ class ParserState { myParentInstance = theParentInstance; } + @Override + public void attributeValue(String theName, String theValue) throws DataFormatException { + if (theName.equals("url")) { + // This can be ignored + return; + } + super.attributeValue(theName, theValue); + } + @Override public void endingElement() throws DataFormatException { pop(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java index e50f90c4362..a9d435467bc 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java @@ -25,6 +25,7 @@ import org.junit.Assert; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import org.mockito.internal.stubbing.answers.ThrowsException; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; @@ -70,6 +71,24 @@ public class JsonParserDstu2Test { private static final FhirContext ourCtx = FhirContext.forDstu2(); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class); + /** + * See #308 + */ + @Test + public void testDeclaredExtensionsDontProduceWarning() { + ReportObservation obs = new ReportObservation(); + obs.setReadOnly(true); + + IParser p = ourCtx.newJsonParser(); + p.setParserErrorHandler(mock(IParserErrorHandler.class, new ThrowsException(new IllegalStateException()))); + + String encoded = p.encodeResourceToString(obs); + ourLog.info(encoded); + + obs = p.parseResource(ReportObservation.class, encoded); + assertEquals(true, obs.getReadOnly().getValue().booleanValue()); + } + @Test public void testEncodeAndParseExtensions() throws Exception { @@ -1124,7 +1143,7 @@ public class JsonParserDstu2Test { ourLog.info(message); Assert.assertThat(message, containsString("contained")); } - + /** * See #144 and #146 */ diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ReportObservation.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ReportObservation.java index fe0116a6cfb..1072eb0a081 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ReportObservation.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ReportObservation.java @@ -12,6 +12,8 @@ import ca.uhn.fhir.util.ElementUtil; @ResourceDef(name = "Observation", id = "reportobservation") public class ReportObservation extends Observation { + private static final long serialVersionUID = 1L; + /** * Each extension is defined in a field. Any valid HAPI Data Type can be used for the field type. Note that the * [name=""] attribute in the @Child annotation needs to match the name for the bean accessor and mutator methods. 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 691f276dc09..6ae1a0db08c 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 @@ -36,6 +36,7 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.internal.stubbing.answers.ThrowsException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; @@ -97,7 +98,6 @@ public class XmlParserDstu2Test { private static final FhirContext ourCtx = FhirContext.forDstu2(); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserDstu2Test.class); - @Test public void testBundleWithBinary() { //@formatter:off @@ -130,6 +130,7 @@ public class XmlParserDstu2Test { } + @Test public void testChoiceTypeWithProfiledType() { //@formatter:off @@ -150,7 +151,6 @@ public class XmlParserDstu2Test { assertThat(encoded, containsString("")); } - @Test public void testChoiceTypeWithProfiledType2() { Parameters par = new Parameters(); @@ -168,6 +168,7 @@ public class XmlParserDstu2Test { assertEquals(MarkdownDt.class, par.getParameter().get(1).getValue().getClass()); } + @Test public void testContainedResourceInExtensionUndeclared() { Patient p = new Patient(); @@ -190,6 +191,24 @@ public class XmlParserDstu2Test { assertEquals("ORG", o.getName()); } + /** + * See #308 + */ + @Test + public void testDeclaredExtensionsDontProduceWarning() { + ReportObservation obs = new ReportObservation(); + obs.setReadOnly(true); + + IParser p = ourCtx.newJsonParser(); + p.setParserErrorHandler(mock(IParserErrorHandler.class, new ThrowsException(new IllegalStateException()))); + + String encoded = p.encodeResourceToString(obs); + ourLog.info(encoded); + + obs = p.parseResource(ReportObservation.class, encoded); + assertEquals(true, obs.getReadOnly().getValue().booleanValue()); + } + @Test public void testDuration() { Encounter enc = new Encounter(); diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cedad40b082..cfbf9ecfb58 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -170,6 +170,10 @@ generics this helps improve the error message at runtime. Thanks to Hugo Soares for suggesting. + + Prevent an unneeded warning when parsing a resource containing + a declared extension. Thanks to Matt Blanchette for reporting! +