Open UMEihle opened 3 years ago
Another test case using HAPI 7.0.2:
package com.test.test
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import org.hl7.fhir.r4.model.Patient
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
class Test {
val fhirString = """
{
"resourceType": "Patient",
"address": [
{
"type": "both",
"_line": [
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-houseNumber",
"valueString": "4"
},
{
"url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-streetName",
"valueString": "Doktor Sven Johanssons Backe"
}
]
},
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-additionalLocator",
"valueString": "It's very hilly"
}
]
}
],
"line": [
"Doktor Sven Johanssons Backe 4",
"It's very hilly"
],
"city": "Göteborg",
"country": "SE"
}
]
}
""".trimIndent()
@Test
fun test() {
val fhirContext: FhirContext = FhirContext.forR4()
val fhirParser: IParser = fhirContext.newJsonParser()
val org = fhirParser.parseResource(fhirString) as Patient
assert(org.address.first().line.first().extension.size == 2) { "Didn't have in org" }
println("original serialized ${fhirParser.encodeResourceToString(org)}")
val fileOutputStream = ByteArrayOutputStream()
val objectOutputStream = ObjectOutputStream(fileOutputStream)
objectOutputStream.writeObject(org)
objectOutputStream.flush()
val array = fileOutputStream.toByteArray()
objectOutputStream.close()
val fileInputStream = ByteArrayInputStream(array)
val objectInputStream = ObjectInputStream(fileInputStream)
val copy = objectInputStream.readObject() as Patient
objectInputStream.close()
println(fhirParser.encodeResourceToString(copy))
assert(copy.address.first().line.first().extension.size == 2) { "Didn't have in copy" }
}
}
plugins {
kotlin("jvm") version "1.9.22"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("ca.uhn.hapi.fhir:hapi-fhir-base:7.0.2")
implementation("ca.uhn.hapi.fhir:hapi-fhir-structures-r4:7.0.2")
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(20)
}
Not an issue in HAPI per-se, instead the correct serialization method needs to be used: https://hapifhir.io/hapi-fhir/docs/model/parsers.html#encoding-aka-serializing
Description Extensions on primitive elements (https://www.hl7.org/fhir/json.html#primitive) get lost when serialized and after that, deserialized again. Especially with Camunda process variables this behavior is not good as Camunda can (de-)serialize process variables. https://docs.camunda.org/manual/7.15/user-guide/process-engine/variables/#java-object-api
To Reproduce
Output
Expected behavior FHIR resource objects should be the same after (de-)serialization.
Environment