From 2744b5eeed56b9c7fa1f831d1cfe1b26f36f4f91 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 29 Nov 2022 14:42:47 -0500 Subject: [PATCH] Fixes to schema generator --- .../fhir/r5/utils/GraphQLSchemaGenerator.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/GraphQLSchemaGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/GraphQLSchemaGenerator.java index dbcc72d72..bd2bf1854 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/GraphQLSchemaGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/GraphQLSchemaGenerator.java @@ -277,8 +277,14 @@ public class GraphQLSchemaGenerator { private void generateElementBase(Writer writer, EnumSet operations) throws IOException { if (operations.contains(FHIROperationType.READ) || operations.contains(FHIROperationType.SEARCH)) { + writer.write("interface IElement {\r\n"); + writer.write(" id: String\r\n"); + writer.write(" extension: [Extension]\r\n"); + writer.write("}\r\n"); + writer.write("\r\n"); + writer.write("type ElementBase {\r\n"); - writer.write(" id: ID\r\n"); + writer.write(" id: String\r\n"); writer.write(" extension: [Extension]\r\n"); writer.write("}\r\n"); writer.write("\r\n"); @@ -299,12 +305,8 @@ public class GraphQLSchemaGenerator { StringBuilder b = new StringBuilder(); list.add(b); b.append("interface "); - b.append(sd.getName() + "Base"); - StructureDefinition baseSd = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition()); - if (baseSd != null) { - b.append(" implements "); - b.append(baseSd.getType() + "Base"); - } + b.append("I" + sd.getName()); + generateTypeSuperinterfaceDeclaration(sd, b); b.append(" {\r\n"); ElementDefinition ed = sd.getSnapshot().getElementFirstRep(); generateProperties(existingTypeNames, list, b, sd.getName(), sd, ed, "type", ""); @@ -314,8 +316,7 @@ public class GraphQLSchemaGenerator { b.append("type "); b.append(sd.getName()); - b.append(" implements "); - b.append(sd.getName() + "Base"); + generateTypeSuperinterfaceDeclaration(sd, b); b.append(" {\r\n"); generateProperties(existingTypeNames, list, b, sd.getName(), sd, ed, "type", ""); b.append("}"); @@ -347,6 +348,23 @@ public class GraphQLSchemaGenerator { } + private void generateTypeSuperinterfaceDeclaration(StructureDefinition theParentSd, StringBuilder theBuilder) { + StructureDefinition baseSd = theParentSd; + boolean first = true; + while (baseSd != null && baseSd.getBaseDefinition() != null) { + baseSd = context.fetchResource(StructureDefinition.class, baseSd.getBaseDefinition()); + if (baseSd != null) { + if (first) { + theBuilder.append(" implements "); + first = false; + } else { + theBuilder.append(" & "); + } + theBuilder.append("I" + baseSd.getType()); + } + } + } + private void generateProperties(Map existingTypeNames, List list, StringBuilder b, String typeName, StructureDefinition sd, ElementDefinition ed, String mode, String suffix) throws IOException { List children = profileUtilities.getChildList(sd, ed); for (ElementDefinition child : children) {