diff --git a/bunsen-core/src/main/scala/com/cerner/bunsen/EncoderBuilder.scala b/bunsen-core/src/main/scala/com/cerner/bunsen/EncoderBuilder.scala index aaf7b5f4..c18018fb 100644 --- a/bunsen-core/src/main/scala/com/cerner/bunsen/EncoderBuilder.scala +++ b/bunsen-core/src/main/scala/com/cerner/bunsen/EncoderBuilder.scala @@ -112,26 +112,29 @@ private[bunsen] class EncoderBuilder(fhirContext: FhirContext, * Returns the accessor method for the given child field. */ private def accessorFor(field: BaseRuntimeChildDefinition): String = { + // Elements called `class` have an underscore appended to the end of their name within getters. + val elementName = if (field.getElementName.equals("class")) + field.getElementName.capitalize + "_" + else field.getElementName.capitalize // Primitive single-value types typically use the Element suffix in their // accessors, with the exception of the "div" field for reasons that are not clear. if (field.isInstanceOf[RuntimeChildPrimitiveDatatypeDefinition] && field.getMax == 1 && field.getElementName != "div") - "get" + field.getElementName.capitalize + "Element" - else { - if (field.getElementName.equals("class")) { - "get" + field.getElementName.capitalize + "_" - } else { - "get" + field.getElementName.capitalize - } - } + "get" + elementName + "Element" + else + "get" + elementName } /** * Returns the setter for the given field name.s */ private def setterFor(field: BaseRuntimeChildDefinition): String = { + // Elements called `class` have an underscore appended to the end of their name within getters. + val elementName = if (field.getElementName.equals("class")) + field.getElementName.capitalize + "_" + else field.getElementName.capitalize // Primitive single-value types typically use the Element suffix in their // setters, with the exception of the "div" field for reasons that are not clear. @@ -140,9 +143,9 @@ private[bunsen] class EncoderBuilder(fhirContext: FhirContext, // Enumerations are set directly rather than via elements. !field.isInstanceOf[RuntimeChildPrimitiveEnumerationDatatypeDefinition] && field.getMax == 1 && field.getElementName != "div") - "set" + field.getElementName.capitalize + "Element" + "set" + elementName + "Element" else - "set" + field.getElementName.capitalize + "set" + elementName } /** diff --git a/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/FhirEncodersTest.java b/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/FhirEncodersTest.java index 0387baba..f4d8d82b 100644 --- a/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/FhirEncodersTest.java +++ b/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/FhirEncodersTest.java @@ -17,6 +17,7 @@ import org.hl7.fhir.dstu3.model.Annotation; import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Condition; +import org.hl7.fhir.dstu3.model.Coverage; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.IntegerType; import org.hl7.fhir.dstu3.model.Medication; @@ -60,6 +61,10 @@ public class FhirEncodersTest { private static Dataset medDataset; private static MedicationRequest decodedMedRequest; + private static Coverage coverage = TestData.newCoverage(); + private static Dataset coverageDataset; + private static Coverage decodedCoverage; + /** * Set up Spark. */ @@ -85,6 +90,9 @@ public static void setUp() { medDataset = spark.createDataset(ImmutableList.of(medRequest), encoders.of(MedicationRequest.class, Medication.class, Provenance.class)); decodedMedRequest = medDataset.head(); + + coverageDataset = spark.createDataset(ImmutableList.of(coverage), encoders.of(Coverage.class)); + decodedCoverage = coverageDataset.head(); } /** @@ -333,4 +341,12 @@ public void testEncoderCached() throws IOException { Assert.assertSame(encoders.of(Patient.class), encoders.of(Patient.class)); } + + @Test + public void testPrimitiveClassDecoding() { + Assert.assertEquals(coverage.getGrouping().getClass_(), + coverageDataset.select("grouping.class").head().get(0)); + Assert.assertEquals(coverage.getGrouping().getClass_(), + decodedCoverage.getGrouping().getClass_()); + } } diff --git a/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/TestData.java b/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/TestData.java index 681467fb..21ced942 100644 --- a/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/TestData.java +++ b/bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/TestData.java @@ -4,6 +4,7 @@ import org.hl7.fhir.dstu3.model.Annotation; import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Condition; +import org.hl7.fhir.dstu3.model.Coverage; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.Identifier; import org.hl7.fhir.dstu3.model.IntegerType; @@ -192,4 +193,22 @@ public static MedicationRequest newMedRequest() { return medReq; } + + /** + * Returns a FHIR Coverage resource for testing purposes. + */ + public static Coverage newCoverage() { + Coverage coverage = new Coverage(); + + coverage.setId("test-coverage"); + coverage.setStatus(Coverage.CoverageStatus.ACTIVE); + coverage.setSubscriber(new Reference("Patient/test-patient")); + + Coverage.GroupComponent groupComponent = new Coverage.GroupComponent(); + groupComponent.setGroup("some-group"); + groupComponent.setClass_("some-group-class"); + coverage.setGrouping(groupComponent); + + return coverage; + } }