Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to fix issues of missing extensions and others #1050

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Schema getDataType() {
// FHIR (Double and BigDecimal are not assignable to each other hence explicit conversions).
// Note with this approach we are loosing some precision!
private static final HapiConverter<Schema> DOUBLE_CONVERTER =
new PrimitiveConverter<Schema>("Double") {
new PrimitiveConverter<Schema>("Decimal") {
@Override
public void toHapi(Object input, IPrimitiveType primitive) {
Preconditions.checkArgument((input instanceof BigDecimal) || (input instanceof Double));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Provenance;
import org.hl7.fhir.r4.model.Quantity;
import org.hl7.fhir.r4.model.QuestionnaireResponse;
import org.hl7.fhir.r4.model.Task;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -101,6 +102,13 @@ public class R4AvroConverterUsCoreTest {

private static Encounter testEncounterDecoded;

private static QuestionnaireResponse testQuestionnaireResponse =
TestData.newQuestionnaireResponse();

private static Record avroQuestionnaireResponse;

private static QuestionnaireResponse testQuestionnaireResponseDecoded;

private static FhirContext fhirContext;

/** Initialize test data. */
Expand Down Expand Up @@ -175,6 +183,15 @@ public static void convertTestData() throws ProfileException {
AvroConverter.forResources(fhirContext, R4UsCoreProfileData.US_CORE_ENCOUNTER_PROFILES, 1);
avroEncounter = (Record) encounterConverter.resourceToAvro(testEncounter);
testEncounterDecoded = (Encounter) encounterConverter.avroToResource(avroEncounter);

AvroConverter questionnaireResponseConverter =
AvroConverter.forResources(
fhirContext, R4UsCoreProfileData.US_CORE_QUESTIONNAIRE_RESPONSE_PROFILES, 1);
avroQuestionnaireResponse =
(Record) questionnaireResponseConverter.resourceToAvro(testQuestionnaireResponse);
testQuestionnaireResponseDecoded =
(QuestionnaireResponse)
questionnaireResponseConverter.avroToResource(avroQuestionnaireResponse);
}

@Test
Expand Down Expand Up @@ -588,4 +605,9 @@ public void testEncounterConversions() {
Assert.assertEquals(
testEncounter.getParticipant().size(), testEncounterDecoded.getParticipant().size());
}

@Test
public void setTestQuestionnaireResponseConversions() {
Assert.assertTrue(testQuestionnaireResponse.equalsDeep(testQuestionnaireResponseDecoded));
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CanonicalType;
import org.hl7.fhir.r4.model.ElementDefinition;
import org.hl7.fhir.r4.model.StructureDefinition;
Expand Down Expand Up @@ -38,8 +39,11 @@ public FhirConversionSupport conversionSupport() {

@Override
protected IStructureDefinition getStructureDefinition(String resourceUrl) {
return new StructureDefinitionWrapper(
(StructureDefinition) context.getValidationSupport().fetchStructureDefinition(resourceUrl));
IBaseResource baseResource =
context.getValidationSupport().fetchStructureDefinition(resourceUrl);
return baseResource == null
? null
: new StructureDefinitionWrapper((StructureDefinition) baseResource);
}

// FHIR version specific interface implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import org.hl7.fhir.r4.model.Period;
import org.hl7.fhir.r4.model.Provenance;
import org.hl7.fhir.r4.model.Quantity;
import org.hl7.fhir.r4.model.QuestionnaireResponse;
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent;
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseStatus;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.Task;
Expand Down Expand Up @@ -438,6 +442,26 @@ public static Provenance newProvenance() {
return provenance;
}

/**
* Returns a new QuestionnaireResponse for testing.
*
* @return a FHIR QuestionnaireResponse for testing.
*/
public static QuestionnaireResponse newQuestionnaireResponse() {
QuestionnaireResponse response = new QuestionnaireResponse();
response.setId("test-questionnaire-response-id");
response.setStatus(QuestionnaireResponseStatus.COMPLETED);
response.setSubject(new Reference("Patient/12345").setDisplay("Here is a display for you."));
QuestionnaireResponseItemComponent component = new QuestionnaireResponseItemComponent();
component.setText("What is your Gender?");
QuestionnaireResponseItemAnswerComponent answerComponent =
new QuestionnaireResponseItemAnswerComponent();
answerComponent.setValue(new StringType("Male"));
component.addAnswer(answerComponent);
response.addItem(component);
return response;
}

/**
* Returns a new Patient from Bunsen Test profile for testing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Collectors;
import org.hl7.fhir.dstu3.model.ElementDefinition;
import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,8 +33,11 @@ public FhirConversionSupport conversionSupport() {

@Override
protected IStructureDefinition getStructureDefinition(String resourceUrl) {
return new StructureDefinitionWrapper(
(StructureDefinition) context.getValidationSupport().fetchStructureDefinition(resourceUrl));
IBaseResource baseResource =
context.getValidationSupport().fetchStructureDefinition(resourceUrl);
return baseResource == null
? null
: new StructureDefinitionWrapper((StructureDefinition) baseResource);
}

// FHIR version specific interface implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String extensionUrl() {

/**
* The FHIR type of the element to be converted, or null if there is no FHIR type, such as within
* a FHIR backbone element.
* a FHIR backbone element. The return value should be a valid FHIR type.
*
* @return FHIR type of the element to be converted.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private <T> List<StructureField<T>> extensionElementToFields(
String profileUrl = element.getFirstTypeProfile();
if (profileUrl != null) {
definition = getStructureDefinition(profileUrl);
if (definition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", profileUrl));
}
}

List<StructureField<T>> extensions;
Expand Down Expand Up @@ -314,6 +318,10 @@ private <T> List<StructureField<T>> elementToFields(
choiceTypes.put(typeCode, child);
} else {
IStructureDefinition structureDefinition = getStructureDefinition(typeCode);
if (structureDefinition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", typeCode));
}

// TODO document why we are resetting the stack here; it is not clear
// why this cannot lead to infinite recursion for choice types. If
Expand Down Expand Up @@ -545,12 +553,24 @@ private <T> T transform(
List<String> referenceProfiles = parentElement.getReferenceTargetProfiles();
List<String> referenceTypes =
referenceProfiles.stream()
.map(profile -> getStructureDefinition(profile).getType())
.map(
profile -> {
IStructureDefinition structureDefinition = getStructureDefinition(profile);
if (structureDefinition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", profile));
}
return structureDefinition.getType();
})
.sorted()
// Retrieve only the unique reference types
.distinct()
.collect(Collectors.toList());
return visitor.visitReference(parentElement.toString(), referenceTypes, childElements);

String elementName = DefinitionVisitorsUtil.elementName(parentElement.getPath());
String elementFullPath = DefinitionVisitorsUtil.pathFromStack(elementName, stack);
return visitor.visitReference(
DefinitionVisitorsUtil.recordNameFor(elementFullPath), referenceTypes, childElements);
} else {
String rootName = DefinitionVisitorsUtil.elementName(root.getPath());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public class R4UsCoreProfileData {
Arrays.asList(
"http://hl7.org/fhir/StructureDefinition/Medication",
"http://hl7.org/fhir/us/core/StructureDefinition/us-core-medication");

public static final List<String> US_CORE_QUESTIONNAIRE_RESPONSE_PROFILES =
Arrays.asList(
"http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse",
"http://hl7.org/fhir/us/core/StructureDefinition/us-core-questionnaireresponse");
}
Loading
Loading