dcarbone / php-fhir

Tools for consuming data from a FHIR server with PHP
Apache License 2.0
129 stars 40 forks source link

Parsing extensions in json #38

Closed leonrenkema closed 4 years ago

leonrenkema commented 5 years ago

I found that there is a trick used in the JSON format to support extensions on a simple value. But the library appears not to support it. When I switch to the XML format there is no problem.

I get the following error when parsing an Organization ErrorException : Could not find mapped property called "_name" on object "Organization".

"name": "Apotheek Janssen B.V.",
"_name": {"extension": [
{
      "url": "http://hl7.org/fhir/StructureDefinition/TBD",
       "valueString": "Legal"
},
{
      "url": "http://hl7.org/fhir/StructureDefinition/TBD",
      "valueBoolean": true
}
]},
<name value="Apotheek Janssen B.V.">
         <extension url="http://hl7.org/fhir/StructureDefinition/TBD">
              <valueString value="Legal">
              </valueString>
         </extension>
         <extension url="http://hl7.org/fhir/StructureDefinition/TBD">
              <valueBoolean value="true">
              </valueBoolean>
        </extension>
</name>

For now there is no problem because we can switch to the XML format but maybe some nice feature to support.

dcarbone commented 5 years ago

That's an interesting approach. I will probably not update the old version of the generator going forward, its code base is too complex and difficult to maintain.

I can, however, add this to the new refactor branch. I'll keep this open just as a reminder.

dcarbone commented 5 years ago

You can see example output here: https://github.com/dcarbone/php-fhir-generated/tree/feature/refactor

LucidTaZ commented 5 years ago

This would be really useful to have. In our case we use an extension on the family attribute of a HumanName to support Dutch family names with prefixes. Since family is a scalar value, we receive _family instead containing this extension: http://hl7.org/fhir/extension-humanname-own-prefix.html.

I tried the generator with the feature/refactor code but it's understandably not stable. Furthermore the latest version of the code you linked above is empty.

Could you please give a rough indication (I understand it's not a guarantee!) of when you expect the refactor to be done? Is there a roadmap somewhere?

leonrenkema commented 5 years ago

Isn't parsing the XML variant of the resource an option? That fixed it for me.

LucidTaZ commented 5 years ago

I understand, but I don't have full control of the message format since it concerns an existing implementation with a customer.

dcarbone commented 5 years ago

@leonrenkema & @LucidTaZ: Could you please try the latest output from v1.0 and see if it works for you?

dcarbone commented 4 years ago

Closing as this is now implemented.

LucidTaZ commented 4 years ago

Hi @dcarbone, it works now, thanks!

Code sample that shows the fix:

$parser = new PHPFHIRResponseParser();

$json = '{"resourceType":"Patient","name":[{"family":"Jansen","_family":{"extension":[{"url":"http://hl7.org/fhir/StructureDefinition/humanname-own-prefix","valueString":"van"}]},"given":["Jan"]}]}';

/** @var FHIRPatient $patientResource */
$patientResource = $parser->parse($json);

$name = $patientResource->getName()[0];
$givenName = $name->getGiven()[0];
$familyName = $name->getFamily();
$familyExtension = $familyName->getExtension()[0];

$this->assertEquals('Jan', $givenName->getValue());
$this->assertEquals('http://hl7.org/fhir/StructureDefinition/humanname-own-prefix', $familyExtension->getUrl()->getValue());
$this->assertEquals('van', $familyExtension->getValueString()->getValue());
$this->assertEquals('Jansen', $familyName->getValue());
dcarbone commented 4 years ago

@LucidTaZ: good to hear! let me know if you find any issues with the new version!