From 8c3c8e215f01903a34a2e28f662d09c5cecb12b6 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 4 Jun 2015 08:19:17 -0400 Subject: [PATCH] Fix #178 - Allow links in Bundle.entry when parsing old Bundle in DSTU2 mode --- .../java/ca/uhn/fhir/parser/ParserState.java | 2 + .../java/ca/uhn/fhir/parser/XmlParser.java | 2 + .../uhn/fhir/parser/XmlParserDstu2Test.java | 38 +++++++++++++++++++ .../src/test/resources/bundle-example.xml | 4 ++ src/changes/changes.xml | 5 +++ 5 files changed, 51 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 bb12d0ae9bb..ee8822253a2 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 @@ -1054,6 +1054,8 @@ class ParserState { push(new PreResourceStateHapi(myEntry, myResourceType).setRequireResourceType(false)); } else if ("deleted".equals(theLocalPart)) { push(new BundleEntryDeletedState(getPreResourceState(), myEntry)); + } else if ("link".equals(theLocalPart)) { + push(new BundleLinkState(myEntry)); } else { throw new DataFormatException("Unexpected element in entry: " + theLocalPart); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java index f990bf5836e..e2be433ef03 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java @@ -400,6 +400,8 @@ public class XmlParser extends BaseParser implements IParser { writeOptionalTagWithValue(theEventWriter, "base", determineResourceBaseUrl(bundleBaseUrl, nextEntry)); + writeBundleResourceLink(theEventWriter, "alternate", nextEntry.getLinkAlternate()); + IResource resource = nextEntry.getResource(); if (resource != null && !resource.isEmpty() && !deleted) { theEventWriter.writeStartElement("resource"); 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 2a84067a8ca..6d051c4bb52 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 @@ -1053,6 +1053,7 @@ public class XmlParserDstu2Test { assertEquals(2, parsed.getEntries().size()); assertEquals("http://foo?search", parsed.getEntries().get(0).getLinkSearch().getValue()); + assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", parsed.getEntries().get(0).getLinkAlternate().getValue()); MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource(); assertEquals("Patient/347", p.getPatient().getReference().getValue()); assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); @@ -1086,6 +1087,8 @@ public class XmlParserDstu2Test { assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLink().get(1).getUrlElement().getValueAsString()); assertEquals(2, parsed.getEntry().size()); + assertEquals("alternate", parsed.getEntry().get(0).getLink().get(0).getRelation()); + assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", parsed.getEntry().get(0).getLink().get(0).getUrl()); assertEquals("http://foo?search", parsed.getEntry().get(0).getTransaction().getUrlElement().getValueAsString()); MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); @@ -1215,6 +1218,41 @@ public class XmlParserDstu2Test { } + @Test + public void testParseBundleOldStyleWithUnknownLinks() throws Exception { + //@formatter:off + String bundle = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + Bundle b = ourCtx.newXmlParser().parseBundle(bundle); + assertEquals(1, b.getEntries().size()); + + + } + /** * See #103 */ diff --git a/hapi-fhir-structures-dstu2/src/test/resources/bundle-example.xml b/hapi-fhir-structures-dstu2/src/test/resources/bundle-example.xml index e8f971b72bd..10af217e13b 100644 --- a/hapi-fhir-structures-dstu2/src/test/resources/bundle-example.xml +++ b/hapi-fhir-structures-dstu2/src/test/resources/bundle-example.xml @@ -16,6 +16,10 @@ + + + + diff --git a/src/changes/changes.xml b/src/changes/changes.xml index be1be038635..0a9718f8752 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -55,6 +55,11 @@ Web tester UI now supports _revinclude + + Support link elements in Bundle.entry when parsing in DSTU2 mode + using the old (non-resource) Bundle class. Thanks to GitHub user + @joedai for reporting! +