From b13851f0c990e407d298c1d6441b08e6b12565dc Mon Sep 17 00:00:00 2001 From: leif stawnyczy Date: Tue, 7 Jan 2025 10:58:31 -0500 Subject: [PATCH] fixing broken tests --- .../java/ca/uhn/fhir/parser/BaseParser.java | 3 ++- .../java/ca/uhn/fhir/util/ResourceUtil.java | 23 +++++++++++++++---- .../fhir/validation/ValidationContext.java | 5 +++- .../r4/FhirResourceDaoR4QueryCountTest.java | 4 ++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index 3ff4226fcf4..d16b50953c4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -41,6 +41,7 @@ import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.parser.path.EncodeContextPath; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.CollectionUtil; @@ -656,7 +657,7 @@ public T parseResource(Class theResourceType, Reade PreserveStringReader psr = (PreserveStringReader) readerToUse; if (psr.hasString()) { try { - ResourceUtil.addRawDataToResource(retVal, psr.toString()); + ResourceUtil.addRawDataToResource(retVal, getEncoding(), psr.toString()); psr.close(); } catch (IOException ex) { ourLog.warn( diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ResourceUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ResourceUtil.java index 1f205b741a5..10d170d4324 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ResourceUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ResourceUtil.java @@ -22,6 +22,7 @@ import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.api.EncodingEnum; import jakarta.annotation.Nonnull; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -30,7 +31,8 @@ public class ResourceUtil { - private static final String RAW_JSON = "RAW_JSON"; + private static final String ENCODING = "ENCODING_TYPE"; + private static final String RAW_ = "RAW_%s"; private ResourceUtil() {} @@ -55,11 +57,24 @@ public static void removeNarrative(FhirContext theContext, IBaseResource theInpu } } - public static void addRawDataToResource(IBaseResource theResource, String theRawJson) throws IOException { - theResource.setUserData(RAW_JSON, theRawJson); + public static void addRawDataToResource(@Nonnull IBaseResource theResource, @Nonnull EncodingEnum theEncodingType, String theSerializedData) throws IOException { + theResource.setUserData(getRawUserDataKey(theEncodingType), theSerializedData); + theResource.setUserData(ENCODING, theEncodingType); + } + + public static EncodingEnum getEncodingTypeFromUserData(@Nonnull IBaseResource theResource) { + return (EncodingEnum) theResource.getUserData(ENCODING); } public static String getRawStringFromResourceOrNull(@Nonnull IBaseResource theResource) { - return (String) theResource.getUserData(RAW_JSON); + EncodingEnum type = (EncodingEnum) theResource.getUserData(ENCODING); + if (type != null) { + return (String) theResource.getUserData(getRawUserDataKey(type)); + } + return null; + } + + private static String getRawUserDataKey(EncodingEnum theEncodingEnum) { + return String.format(RAW_, theEncodingEnum.name()); } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/validation/ValidationContext.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/validation/ValidationContext.java index 6e44dc05a4c..b7de70778d0 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/validation/ValidationContext.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/validation/ValidationContext.java @@ -114,7 +114,10 @@ public String encode() { @Override public EncodingEnum getEncoding() { - return EncodingEnum.JSON; + return ObjectUtils.defaultIfNull( + ResourceUtil.getEncodingTypeFromUserData(theResource), + EncodingEnum.JSON + ); } }; return new ValidationContext<>(theContext, theResource, encoder, options); diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4QueryCountTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4QueryCountTest.java index 0071e50e681..b928c6ecf2b 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4QueryCountTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4QueryCountTest.java @@ -4017,8 +4017,8 @@ public void testValidateResource(boolean theStoredInRepository) { assertThat(((OperationOutcome)outcome.getOperationOutcome()).getIssueFirstRep().getDiagnostics()).contains("No issues detected"); myCaptureQueriesListener.logSelectQueries(); if (theStoredInRepository) { - assertEquals(7, myCaptureQueriesListener.countGetConnections()); - assertEquals(8, myCaptureQueriesListener.countSelectQueries()); + assertEquals(5, myCaptureQueriesListener.countGetConnections()); + assertEquals(6, myCaptureQueriesListener.countSelectQueries()); } else { assertEquals(6, myCaptureQueriesListener.countGetConnections()); assertEquals(6, myCaptureQueriesListener.countSelectQueries());