[(master)] Added support for references in meta

This commit is contained in:
Simon Marco Janic 2017-11-21 10:38:14 +01:00
parent f0477ac807
commit 0077215b19
4 changed files with 62 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import ca.uhn.fhir.model.api.*;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.base.composite.BaseContainedDt;
import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.narrative.INarrativeGenerator;
@ -736,8 +737,18 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
}
theEventWriter.beginObject();
writeOptionalTagWithTextNode(theEventWriter, "url", extension.getUrl());
String extensionDatatype = myContext.getRuntimeChildUndeclaredExtensionDefinition().getChildNameByDatatype(extension.getValue().getClass());
writeOptionalTagWithTextNode(theEventWriter, extensionDatatype, extension.getValueAsPrimitive());
RuntimeChildUndeclaredExtensionDefinition runtimeDefinitions = myContext.getRuntimeChildUndeclaredExtensionDefinition();
String extensionDatatype = runtimeDefinitions.getChildNameByDatatype(extension.getValue().getClass());
if (extension.getValue() instanceof IPrimitiveDatatype) {
writeOptionalTagWithTextNode(theEventWriter, extensionDatatype, extension.getValueAsPrimitive());
} else {
if (extension.getValue() instanceof BaseResourceReferenceDt) {
writeOptionalTagWithTextNode(theEventWriter, extensionDatatype, ((BaseResourceReferenceDt) extension.getValue()).getReference());
} else {
throw new IllegalArgumentException("Cannot parse meta extension with type: " + extension.getValue().getClass().getSimpleName());
}
}
theEventWriter.endObject();
}
theEventWriter.endArray();

View File

@ -489,6 +489,8 @@ class ParserState<T> {
}
} else if ("url".equals(theName) && myInstance instanceof ExtensionDt) {
((ExtensionDt) myInstance).setUrl(theValue);
} else if ("value".equals(theName) && myInstance instanceof BaseResourceReferenceDt) {
((BaseResourceReferenceDt) myInstance).setReference(new IdDt(theValue));
} else {
if (myJsonMode) {
myErrorHandler.incorrectJsonType(null, myElementName, ValueType.OBJECT, null, ValueType.SCALAR, ScalarType.STRING);

View File

@ -22,7 +22,9 @@ import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.AfterClass;
@ -1719,6 +1721,12 @@ public class JsonParserDstu2Test {
addExtensionResourceMetadataKeyToResource(procedureRequest, true, "http://someurl.com/modifier", "SomeValue");
addExtensionResourceMetadataKeyToResource(procedureRequest, true, "http://someurl.com/modifier2", "SomeValue2");
Organization organization = new Organization();
organization.setId(new IdDt("Someorganization"));
ExtensionDt extensionDt = new ExtensionDt(false, "http://someurl3.com", new ResourceReferenceDt(organization.getId()));
procedureRequest.getResourceMetadata()
.put(new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(extensionDt.getUrl()), extensionDt);
String json = ourCtx.newJsonParser().encodeResourceToString(procedureRequest);
// @formatter:off
@ -1741,6 +1749,33 @@ public class JsonParserDstu2Test {
// @formatter:on
}
@Test
public void testParseResourceWithReferenceExtensionMetadata() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/procedure-request.json"));
IParser parser = ourCtx.newJsonParser();
IParserErrorHandler peh = mock(IParserErrorHandler.class);
parser.setParserErrorHandler(peh);
ProcedureRequest p = parser.parseResource(ProcedureRequest.class, input);
ArgumentCaptor<String> capt = ArgumentCaptor.forClass(String.class);
verify(peh, Mockito.never()).unknownElement(Mockito.isNull(IParseLocation.class), capt.capture());
assertReferenceExtensionMetadata(p, "http://someurl3.com", false, ResourceReferenceDt.class, "dc0b549a-f852-11e6-bc64-92361f002671");
}
private void assertReferenceExtensionMetadata(
BaseResource resource,
String url,
boolean isModifier,
Class<?> expectedType,
String expectedValue) {
ExtensionDt extension = (ExtensionDt) resource.getResourceMetadata().get(new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(url));
assertThat(extension.getValue(), CoreMatchers.instanceOf(expectedType));
assertThat(extension.isModifier(), equalTo(isModifier));
assertThat(extension.getValue(), CoreMatchers.instanceOf(BaseResourceReferenceDt.class));
assertThat(((BaseResourceReferenceDt)extension.getValue()).getReference().getIdPart(), equalTo(expectedValue));
}
@Test(expected = IllegalArgumentException.class)
public void testCannotEncodeSubextensionsOnMeta() {
ProcedureRequest procedureRequest = new ProcedureRequest();

View File

@ -0,0 +1,12 @@
{
"resourceType": "ProcedureRequest",
"meta": {
"extension": [
{
"url": "http://someurl3.com",
"valueReference": "Organization/dc0b549a-f852-11e6-bc64-92361f002671"
}
]
},
"status": "accepted"
}