hapifhir / org.hl7.fhir.core

Apache License 2.0
156 stars 160 forks source link

FHIRException: Cannot addChild on a primitive type Extension.url #1343

Closed bschaffh closed 1 year ago

bschaffh commented 1 year ago

Hello,

I am currently generating classes from their StructureDefinition stored in a Json file in a npm package.

For this, I use the following code :

public void Load() throws Exception {
        if (ctxt == null) {
            ctxt = TestingUtilities.getSharedWorkerContext();
            FilesystemPackageCacheManager pc = new FilesystemPackageCacheManager(true);
            NpmPackage npm = pc.loadPackage("hl7.fhir.fr.core", "1.1.0");
            ctxt.loadFromPackage(npm, new TestPackageLoader(Utilities.strings(SimpleWorkerContext.defaultTypesToLoad().stream().toArray(String[]::new))));

            File folder = new File("C:\\Users\\a884402\\.fhir\\packages\\hl7.fhir.fr.core#1.1.0\\package\\");

            for (File file : folder.listFiles()){
                if (!file.isDirectory()){
                    try {
                        ctxt.cacheResource(new JsonParser().parse(Files.readString(Path.of(file.getAbsolutePath()))));
                        System.out.println(file.getAbsolutePath() + " chargé.");
                    }
                    catch(Exception e){
                        System.out.println(file.getAbsolutePath() + " PAS chargé.");
                    }
                }
            }
           }
    }

public void CreatePatientFromProfile(String profileURL) throws Exception {
        this.Load();

        try {
            var peBuilder = new PEBuilder(ctxt, PEBuilder.PEElementPropertiesPolicy.NONE, false);
            Patient res = (Patient) peBuilder.createResource(profileURL, true);

            String json = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res);
            System.out.println(json);
        }
        catch (FHIRException fhirException){fhirException.printStackTrace();}
    }

My problem is, when I call CreatePatientFromProfile(url), it throws the following exception :

image

After looking at the Extension class, I saw that the method addChild() throws this exception, but I don't understand why. In my patient profile, I have an extension with an URL with a fixed value and a cardinality of 1..1, so the generation of the class with the PEBuilder will try to add a child as an URL, but then it fails because of the exception.

Is there a workaround to avoid the exception, or is thsi normal and there's something I haven't understood properly?

The problem is that because of this error, I can't generate my class according a profile structure definition with all the required fields.

Thank you !

grahamegrieve commented 1 year ago

You don't say what the profile URL is that's causing you to blow up

grahamegrieve commented 1 year ago

Should be fixed now