[(master)] Added support for extensions in Meta resource
This commit is contained in:
parent
2b4a492870
commit
b4032f4e8c
|
@ -613,5 +613,30 @@ public abstract class ResourceMetadataKeyEnum<T> implements Serializable {
|
||||||
public abstract void put(IAnyResource theResource, T2 theObject);
|
public abstract void put(IAnyResource theResource, T2 theObject);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class ExtensionResourceMetadataKey extends ResourceMetadataKeyEnum<ExtensionDt> {
|
||||||
|
public ExtensionResourceMetadataKey(String url) {
|
||||||
|
super(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExtensionDt get(IResource theResource) {
|
||||||
|
Object retValObj = theResource.getResourceMetadata().get(this);
|
||||||
|
if (retValObj == null) {
|
||||||
|
return null;
|
||||||
|
} else if (retValObj instanceof ExtensionDt) {
|
||||||
|
return (ExtensionDt) retValObj;
|
||||||
|
}
|
||||||
|
throw new InternalErrorException("Found an object of type '" + retValObj.getClass().getCanonicalName()
|
||||||
|
+ "' in resource metadata for key " + this.name() + " - Expected "
|
||||||
|
+ ExtensionDt.class.getCanonicalName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void put(IResource theResource, ExtensionDt theObject) {
|
||||||
|
theResource.getResourceMetadata().put(this, theObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -911,6 +911,18 @@ public abstract class BaseParser implements IParser {
|
||||||
throw new DataFormatException(nextChild + " has no child of type " + theType);
|
throw new DataFormatException(nextChild + " has no child of type " + theType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> getExtensionMetadataKeys(IResource resource) {
|
||||||
|
List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionMetadataKeys = new ArrayList<Map.Entry<ResourceMetadataKeyEnum<?>, Object>>();
|
||||||
|
for (Map.Entry<ResourceMetadataKeyEnum<?>, Object> entry : resource.getResourceMetadata().entrySet()) {
|
||||||
|
if (entry.getKey() instanceof ResourceMetadataKeyEnum.ExtensionResourceMetadataKey) {
|
||||||
|
extensionMetadataKeys.add(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return extensionMetadataKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected static <T> List<T> extractMetadataListNotNull(IResource resource, ResourceMetadataKeyEnum<List<T>> key) {
|
protected static <T> List<T> extractMetadataListNotNull(IResource resource, ResourceMetadataKeyEnum<List<T>> key) {
|
||||||
List<? extends T> securityLabels = key.get(resource);
|
List<? extends T> securityLabels = key.get(resource);
|
||||||
if (securityLabels == null) {
|
if (securityLabels == null) {
|
||||||
|
|
|
@ -655,8 +655,9 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
|
||||||
if (isBlank(versionIdPart)) {
|
if (isBlank(versionIdPart)) {
|
||||||
versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
|
versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
|
||||||
}
|
}
|
||||||
|
List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionMetadataKeys = getExtensionMetadataKeys(resource);
|
||||||
|
|
||||||
if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
|
if (super.shouldEncodeResourceMeta(resource) && (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) || !extensionMetadataKeys.isEmpty()) {
|
||||||
beginObject(theEventWriter, "meta");
|
beginObject(theEventWriter, "meta");
|
||||||
writeOptionalTagWithTextNode(theEventWriter, "versionId", versionIdPart);
|
writeOptionalTagWithTextNode(theEventWriter, "versionId", versionIdPart);
|
||||||
writeOptionalTagWithTextNode(theEventWriter, "lastUpdated", updated);
|
writeOptionalTagWithTextNode(theEventWriter, "lastUpdated", updated);
|
||||||
|
@ -696,6 +697,8 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
|
||||||
theEventWriter.endArray();
|
theEventWriter.endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addExtensionMetadata(extensionMetadataKeys, theEventWriter);
|
||||||
|
|
||||||
theEventWriter.endObject(); // end meta
|
theEventWriter.endObject(); // end meta
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -705,6 +708,41 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
|
||||||
theEventWriter.endObject();
|
theEventWriter.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addExtensionMetadata(List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionMetadataKeys, JsonLikeWriter theEventWriter) throws IOException {
|
||||||
|
if (extensionMetadataKeys.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionKeys = new ArrayList<>(extensionMetadataKeys.size());
|
||||||
|
List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> modifierExtensionKeys = new ArrayList<>(extensionKeys.size());
|
||||||
|
for (Map.Entry<ResourceMetadataKeyEnum<?>, Object> entry : extensionMetadataKeys) {
|
||||||
|
if (!((ExtensionDt) entry.getValue()).isModifier()) {
|
||||||
|
extensionKeys.add(entry);
|
||||||
|
} else {
|
||||||
|
modifierExtensionKeys.add(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeMetadataExtensions(extensionKeys, "extension", theEventWriter);
|
||||||
|
writeMetadataExtensions(extensionKeys, "modifierExtension", theEventWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeMetadataExtensions(List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensions, String arrayName, JsonLikeWriter theEventWriter) throws IOException {
|
||||||
|
if (extensions.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
beginArray(theEventWriter, arrayName);
|
||||||
|
for (Map.Entry<ResourceMetadataKeyEnum<?>, Object> key : extensions) {
|
||||||
|
ExtensionDt extension = (ExtensionDt) key.getValue();
|
||||||
|
theEventWriter.beginObject();
|
||||||
|
writeOptionalTagWithTextNode(theEventWriter, "url", extension.getUrl());
|
||||||
|
String extensionDatatype = myContext.getRuntimeChildUndeclaredExtensionDefinition().getChildNameByDatatype(extension.getValue().getClass());
|
||||||
|
writeOptionalTagWithTextNode(theEventWriter, extensionDatatype, extension.getValueAsPrimitive());
|
||||||
|
theEventWriter.endObject();
|
||||||
|
}
|
||||||
|
theEventWriter.endArray();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object
|
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object
|
||||||
* called _name): resource extensions, and extension extensions
|
* called _name): resource extensions, and extension extensions
|
||||||
|
|
|
@ -841,6 +841,25 @@ class ParserState<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enteringNewElementExtension(StartElement theElem, String theUrlAttr, boolean theIsModifier, final String baseServerUrl) {
|
||||||
|
ResourceMetadataKeyEnum.ExtensionResourceMetadataKey resourceMetadataKeyEnum = new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(theUrlAttr);
|
||||||
|
Object metadataValue = myMap.get(resourceMetadataKeyEnum);
|
||||||
|
ExtensionDt newExtension;
|
||||||
|
if (metadataValue == null) {
|
||||||
|
newExtension = new ExtensionDt(theIsModifier);
|
||||||
|
} else if (metadataValue instanceof ExtensionDt) {
|
||||||
|
newExtension = (ExtensionDt) metadataValue;
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("Expected ExtensionDt as custom resource metadata type, got: " + metadataValue.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
newExtension.setUrl(theUrlAttr);
|
||||||
|
myMap.put(resourceMetadataKeyEnum, newExtension);
|
||||||
|
|
||||||
|
ExtensionState newState = new ExtensionState(getPreResourceState(), newExtension);
|
||||||
|
push(newState);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MetaVersionElementState extends BaseState {
|
private class MetaVersionElementState extends BaseState {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package ca.uhn.fhir.parser;
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.Matchers.contains;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
import static org.hamcrest.Matchers.stringContainsInOrder;
|
import static org.hamcrest.Matchers.stringContainsInOrder;
|
||||||
|
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -1688,19 +1690,65 @@ public class JsonParserDstu2Test {
|
||||||
Patient p = parser.parseResource(Patient.class, input);
|
Patient p = parser.parseResource(Patient.class, input);
|
||||||
|
|
||||||
ArgumentCaptor<String> capt = ArgumentCaptor.forClass(String.class);
|
ArgumentCaptor<String> capt = ArgumentCaptor.forClass(String.class);
|
||||||
verify(peh, times(4)).unknownElement(Mockito.isNull(IParseLocation.class), capt.capture());
|
verify(peh, Mockito.never()).unknownElement(Mockito.isNull(IParseLocation.class), capt.capture());
|
||||||
|
|
||||||
//@formatter:off
|
|
||||||
List<String> strings = capt.getAllValues();
|
|
||||||
assertThat(strings, contains(
|
|
||||||
"extension",
|
|
||||||
"extension",
|
|
||||||
"modifierExtension",
|
|
||||||
"modifierExtension"
|
|
||||||
));
|
|
||||||
//@formatter:off
|
|
||||||
|
|
||||||
assertEquals("Smith", p.getName().get(0).getGiven().get(0).getValue());
|
assertEquals("Smith", p.getName().get(0).getGiven().get(0).getValue());
|
||||||
|
assertExtensionMetadata(p, "fhir-request-method", false, StringDt.class, "POST");
|
||||||
|
assertExtensionMetadata(p, "fhir-request-uri", false, UriDt.class, "Patient");
|
||||||
|
assertExtensionMetadata(p, "modified-fhir-request-method", true, StringDt.class, "POST");
|
||||||
|
assertExtensionMetadata(p, "modified-fhir-request-uri", true, UriDt.class, "Patient");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertExtensionMetadata(
|
||||||
|
BaseResource resource,
|
||||||
|
String url,
|
||||||
|
boolean isModifier,
|
||||||
|
Class<?> expectedType,
|
||||||
|
String expectedValue) {
|
||||||
|
ExtensionDt extension = (ExtensionDt) resource.getResourceMetadata().get(new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(url));
|
||||||
|
assertThat(extension.getValue(), instanceOf(expectedType));
|
||||||
|
assertThat(extension.isModifier(), equalTo(isModifier));
|
||||||
|
assertThat(extension.getValueAsPrimitive().getValueAsString(), equalTo(expectedValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncodeResourceWithExtensionMetadata() throws Exception {
|
||||||
|
ProcedureRequest procedureRequest = new ProcedureRequest();
|
||||||
|
procedureRequest.setStatus(ProcedureRequestStatusEnum.ACCEPTED);
|
||||||
|
addExtensionResourceMetadataKeyToResource(procedureRequest, false, "http://someurl.com", "SomeValue");
|
||||||
|
addExtensionResourceMetadataKeyToResource(procedureRequest, false, "http://someurl2.com", "SomeValue2");
|
||||||
|
addExtensionResourceMetadataKeyToResource(procedureRequest, true, "http://someurl.com/modifier", "SomeValue");
|
||||||
|
addExtensionResourceMetadataKeyToResource(procedureRequest, true, "http://someurl.com/modifier2", "SomeValue2");
|
||||||
|
|
||||||
|
String json = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(procedureRequest);
|
||||||
|
|
||||||
|
// @formatter:off
|
||||||
|
assertThat(json, stringContainsInOrder("\"meta\": {",
|
||||||
|
"\"extension\": [", "{",
|
||||||
|
"\"url\": \"http://someurl.com\",",
|
||||||
|
"\"valueString\": \"SomeValue\"",
|
||||||
|
"},",
|
||||||
|
"{",
|
||||||
|
"\"url\": \"http://someurl2.com\",",
|
||||||
|
"\"valueString\": \"SomeValue2\"",
|
||||||
|
"}",
|
||||||
|
"],",
|
||||||
|
"\"modifierExtension\": [",
|
||||||
|
"{",
|
||||||
|
"\"url\": \"http://someurl.com\",",
|
||||||
|
"\"valueString\": \"SomeValue\"",
|
||||||
|
"},",
|
||||||
|
"{",
|
||||||
|
"\"url\": \"http://someurl2.com\",",
|
||||||
|
"\"valueString\": \"SomeValue2\"",
|
||||||
|
"}",
|
||||||
|
"]"));
|
||||||
|
// @formatter:on
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addExtensionResourceMetadataKeyToResource(BaseResource resource, boolean isModifier, String url, String value) {
|
||||||
|
ExtensionDt extensionDt = new ExtensionDt(isModifier, url, new StringDt(value));
|
||||||
|
resource.getResourceMetadata()
|
||||||
|
.put(new ResourceMetadataKeyEnum.ExtensionResourceMetadataKey(extensionDt.getUrl()), extensionDt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,35 +1,35 @@
|
||||||
{
|
{
|
||||||
"id": "73b551fb-46f5-4fb8-b735-2399344e9717",
|
"id": "73b551fb-46f5-4fb8-b735-2399344e9717",
|
||||||
"meta": {
|
"meta": {
|
||||||
"extension": [
|
"extension": [
|
||||||
{
|
{
|
||||||
"url": "fhir-request-method",
|
"url": "fhir-request-method",
|
||||||
"valueString": "POST"
|
"valueString": "POST"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"url": "fhir-request-uri",
|
"url": "fhir-request-uri",
|
||||||
"valueUri": "Patient"
|
"valueUri": "Patient"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"modifierExtension": [
|
"modifierExtension": [
|
||||||
{
|
{
|
||||||
"url": "fhir-request-method",
|
"url": "modified-fhir-request-method",
|
||||||
"valueString": "POST"
|
"valueString": "POST"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"url": "fhir-request-uri",
|
"url": "modified-fhir-request-uri",
|
||||||
"valueUri": "Patient"
|
"valueUri": "Patient"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"versionId": "01e5253d-d258-494c-8d8e-f22ad6d8f19b",
|
"versionId": "01e5253d-d258-494c-8d8e-f22ad6d8f19b",
|
||||||
"lastUpdated": "2016-02-20T11:01:56.155Z"
|
"lastUpdated": "2016-02-20T11:01:56.155Z"
|
||||||
},
|
},
|
||||||
"name": [
|
"name": [
|
||||||
{
|
{
|
||||||
"given": [
|
"given": [
|
||||||
"Smith"
|
"Smith"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"resourceType": "Patient"
|
"resourceType": "Patient"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue