diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 02f92d8e9f8..9ec3df3310b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -734,6 +734,9 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { beginArray(theEventWriter, arrayName); for (Map.Entry, Object> key : extensions) { ExtensionDt extension = (ExtensionDt) key.getValue(); + if (!extension.getAllUndeclaredExtensions().isEmpty()) { + throw new IllegalArgumentException("Sub-extensions on metadata isn't supported"); + } theEventWriter.beginObject(); writeOptionalTagWithTextNode(theEventWriter, "url", extension.getUrl()); String extensionDatatype = myContext.getRuntimeChildUndeclaredExtensionDefinition().getChildNameByDatatype(extension.getValue().getClass()); 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 dca45aaffa9..266321bda1a 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 @@ -1745,6 +1745,17 @@ public class JsonParserDstu2Test { // @formatter:on } + @Test(expected = IllegalArgumentException.class) + public void testCannotEncodeSubextensionsOnMeta() { + ProcedureRequest procedureRequest = new ProcedureRequest(); + procedureRequest.setStatus(ProcedureRequestStatusEnum.ACCEPTED); + ExtensionDt parent = new ExtensionDt(false, "#parent"); + parent.addUndeclaredExtension(new ExtensionDt(false, "#child", new DurationDt().setValue(123))); + procedureRequest.getResourceMetadata().put(new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(parent.getUrl()), parent); + + String json = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(procedureRequest); + } + private void addExtensionResourceMetadataKeyToResource(BaseResource resource, boolean isModifier, String url, String value) { ExtensionDt extensionDt = new ExtensionDt(isModifier, url, new StringDt(value)); resource.getResourceMetadata()