Skip to content

Commit

Permalink
fixing broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leif stawnyczy committed Jan 7, 2025
1 parent 1ca719b commit b13851f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -656,7 +657,7 @@ public <T extends IBaseResource> T parseResource(Class<T> 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(
Expand Down
23 changes: 19 additions & 4 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ResourceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {}

Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit b13851f

Please sign in to comment.