Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/on-the-fly/kbv-r4/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ if (require.main === module) {
registry: "https://packages.simplifier.net",
ignorePackageIndex: true,
})
.fromPackage("hl7.fhir.r4.core", "4.0.1")
.fromPackage("kbv.ita.for", "1.3.1")
.fromPackage("de.basisprofil.r4", "1.6.0-ballot2")
.fromPackage("kbv.basis", "1.8.0")
.throwException()
.typescript({
withDebugComment: false,
Expand Down
42 changes: 40 additions & 2 deletions src/typeschema/core/field-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
FieldSlice,
FieldSlicing,
Name,
PackageMeta,
RegularField,
RichFHIRSchema,
TypeIdentifier,
Expand All @@ -25,6 +26,35 @@ import { mkBindingIdentifier, mkIdentifier } from "./identifier";
import { mkSliceNameCandidates } from "./name-candidates";
import { mkNestedIdentifier } from "./nested-types";

const R5_ONLY_TYPES = new Set([
"Availability",
"CodeableReference",
"ExtendedContactDetail",
"MonetaryComponent",
"RatioRange",
"VirtualServiceDetail",
]);

const dependsOnR4Core = (register: Register, pkg: PackageMeta): boolean => {
const pkgIndex = register.resolver[packageMetaToFhir(pkg)];
if (!pkgIndex) return false;
for (const options of Object.values(pkgIndex.canonicalResolution)) {
for (const opt of options) {
if (opt.pkg.name === "hl7.fhir.r4.core") return true;
}
}
return false;
};

const fieldTypeResolutionHint = (register: Register, pkg: PackageMeta, type: string): string => {
if (!R5_ONLY_TYPES.has(type)) return "";
if (!dependsOnR4Core(register, pkg)) return "";
return (
`\n hint: '${type}' is an R5+ type and is not available when generating against R4.` +
`\n Either skip this canonical via skip-hack.ts, or upgrade the target to R5.`
);
};

function isRequired(register: Register, fhirSchema: RichFHIRSchema, path: string[]): boolean {
const fieldName = path[path.length - 1];
if (!fieldName) throw new Error(`Internal error: fieldName is missing for path ${path.join("/")}`);
Expand Down Expand Up @@ -283,10 +313,18 @@ export function buildFieldType(
} else if (element.type) {
const url = register.ensureSpecializationCanonicalUrl(element.type);
const fieldFs = register.resolveFs(fhirSchema.package_meta, url);
if (!fieldFs)
if (!fieldFs) {
const pkgId = packageMetaToFhir(fhirSchema.package_meta);
const fieldPath = path.join(".");
const hint = fieldTypeResolutionHint(register, fhirSchema.package_meta, element.type);
throw new Error(
`Could not resolve field type: <${fhirSchema.url}>.${path.join(".")}: <${element.type}> (pkg: '${packageMetaToFhir(fhirSchema.package_meta)}'))`,
`Could not resolve field type:
package: ${pkgId}
schema: ${fhirSchema.url}
field: ${fieldPath}
type: ${element.type}${hint}`,
);
}
return mkIdentifier(fieldFs);
} else if (element.choices) {
return undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/typeschema/skip-hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const availabilityInR4 = "Use Availability which is not provided by FHIR R4.";

export const skipList: Record<string, Record<CanonicalUrl, string>> = {
"hl7.fhir.uv.extensions.r4": {
"http://hl7.org/fhir/StructureDefinition/biologicallyderivedproduct-manipulation": codeableReferenceInR4,
"http://hl7.org/fhir/StructureDefinition/biologicallyderivedproduct-processing": codeableReferenceInR4,
"http://hl7.org/fhir/StructureDefinition/extended-contact-availability": availabilityInR4,
"http://hl7.org/fhir/StructureDefinition/immunization-procedure": codeableReferenceInR4,
"http://hl7.org/fhir/StructureDefinition/specimen-additive": codeableReferenceInR4,
Expand Down
Loading