simple fix (#5630)

Co-authored-by: Long Ma <long@smilecdr.com>
This commit is contained in:
longma1 2024-01-25 09:48:29 -07:00 committed by GitHub
parent ba28ff977f
commit 1441a90a9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.primitive.CodeDt;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.ICompositeType;
@ -41,8 +42,8 @@ public class AttachmentUtil {
return getOrCreateChild(theContext, theAttachment, "data", "base64Binary");
}
public static IPrimitiveType<String> getOrCreateContentType(FhirContext theContext, ICompositeType theAttachment) {
return getOrCreateChild(theContext, theAttachment, "contentType", "string");
public static IPrimitiveType<CodeDt> getOrCreateContentType(FhirContext theContext, ICompositeType theAttachment) {
return getOrCreateChild(theContext, theAttachment, "contentType", "code");
}
public static IPrimitiveType<String> getOrCreateUrl(FhirContext theContext, ICompositeType theAttachment) {

View File

@ -0,0 +1,5 @@
---
type: fix
issue: 5537
title: "Calling the method getOrCreateContentType in AttachmentUtil on an attachment with no content type would throw exception because contentType is a code not a string.
This fixes the function to create an empty code as expected"

View File

@ -1,11 +1,16 @@
package ca.uhn.fhir.validator;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.primitive.CodeDt;
import ca.uhn.fhir.util.AttachmentUtil;
import org.hl7.fhir.instance.model.api.ICompositeType;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.hl7.fhir.r4.model.Attachment;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class AttachmentUtilTest {
@ -53,4 +58,18 @@ public class AttachmentUtilTest {
String encoded = ctx.newJsonParser().encodeResourceToString(communication);
assertEquals("{\"resourceType\":\"Communication\",\"payload\":[{\"contentAttachment\":{\"contentType\":\"text/plain\",\"data\":\"AAECAw==\",\"url\":\"http://foo\",\"size\":123}}]}", encoded);
}
@Test
public void testGetOrCreateContentTypeOnEmptyAttachmentR4(){
FhirContext ctx = FhirContext.forR4Cached();
Attachment attachment = (Attachment) AttachmentUtil.newInstance(ctx);
assertNull(attachment.getContentType());
IPrimitiveType<CodeDt> contentType = AttachmentUtil.getOrCreateContentType(ctx, attachment);
contentType.setValueAsString("text/plain");
assertNotNull(attachment.getContentType());
}
}