Merge pull request #736 from malcolmm83/master

Fix for Binary.securityContext is not being encoded #734
This commit is contained in:
James Agnew 2017-11-01 15:37:43 -04:00 committed by GitHub
commit 256d541dda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -49,6 +49,7 @@ import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.util.ElementUtil;
import static ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum.ID_DATATYPE;
import static ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum.PRIMITIVE_DATATYPE;
import java.lang.reflect.Method;
/**
* This class is the FHIR JSON parser/encoder. Users should not interact with this class directly, but should use
@ -705,6 +706,21 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
if (isNotBlank(contentAsBase64)) {
write(theEventWriter, "content", contentAsBase64);
}
try {
Method getSC = bin.getClass().getMethod("getSecurityContext");
Object securityContext = getSC.invoke(bin);
if (securityContext != null) {
Method getRef = securityContext.getClass().getMethod("getReference");
String securityContextRef = (String) getRef.invoke(securityContext);
if (securityContextRef != null) {
beginObject(theEventWriter, "securityContext");
writeOptionalTagWithTextNode(theEventWriter, "reference", securityContextRef);
theEventWriter.endObject();
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
encodeCompositeElementToStreamWriter(theResDef, theResource, theResource, theEventWriter, theContainedResource, theSubResource, new CompositeChildElement(resDef, theSubResource));
}

View File

@ -575,6 +575,23 @@ public class JsonParserDstu3Test {
String output = ourCtx.newJsonParser().encodeResourceToString(new Binary());
assertEquals("{\"resourceType\":\"Binary\"}", output);
}
@Test
public void testEncodeBinaryWithSecurityContext() {
Binary bin = new Binary();
bin.setContentType("text/plain");
bin.setContent("Now is the time".getBytes());
Reference securityContext = new Reference();
securityContext.setReference("DiagnosticReport/1");
bin.setSecurityContext(securityContext);
String encoded = ourCtx.newJsonParser().encodeResourceToString(bin);
assertThat(encoded, containsString("Binary"));
assertThat(encoded, containsString("contentType"));
assertThat(encoded, containsString("text/plain"));
assertThat(encoded, containsString("Tm93IGlzIHRoZSB0aW1l"));
assertThat(encoded, containsString("securityContext"));
assertThat(encoded, containsString("{\"reference\":\"DiagnosticReport/1\"}"));
}
/**
* #158