From 1441a90a9fba4ddd3eaf10fc3bd962c998e9b454 Mon Sep 17 00:00:00 2001 From: longma1 <32119004+longma1@users.noreply.github.com> Date: Thu, 25 Jan 2024 09:48:29 -0700 Subject: [PATCH] simple fix (#5630) Co-authored-by: Long Ma --- .../java/ca/uhn/fhir/util/AttachmentUtil.java | 5 +++-- ...5537-attachment-util-content-type-fix.yaml | 5 +++++ .../fhir/validator/AttachmentUtilTest.java | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5537-attachment-util-content-type-fix.yaml diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/AttachmentUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/AttachmentUtil.java index 783d71682f5..cee970715af 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/AttachmentUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/AttachmentUtil.java @@ -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 getOrCreateContentType(FhirContext theContext, ICompositeType theAttachment) { - return getOrCreateChild(theContext, theAttachment, "contentType", "string"); + public static IPrimitiveType getOrCreateContentType(FhirContext theContext, ICompositeType theAttachment) { + return getOrCreateChild(theContext, theAttachment, "contentType", "code"); } public static IPrimitiveType getOrCreateUrl(FhirContext theContext, ICompositeType theAttachment) { diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5537-attachment-util-content-type-fix.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5537-attachment-util-content-type-fix.yaml new file mode 100644 index 00000000000..6a72835bcc0 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5537-attachment-util-content-type-fix.yaml @@ -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" diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/validator/AttachmentUtilTest.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/validator/AttachmentUtilTest.java index 072b1a5d6b2..fa622067321 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/validator/AttachmentUtilTest.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/validator/AttachmentUtilTest.java @@ -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 contentType = AttachmentUtil.getOrCreateContentType(ctx, attachment); + + contentType.setValueAsString("text/plain"); + + assertNotNull(attachment.getContentType()); + } }