Skip to content

Commit ff06ced

Browse files
authored
Merge pull request #153 from atomic-ehr/fix/r5-types-in-r4-extensions
TypeSchema: better error + skip R5 types in R4-target extensions
2 parents d46a84e + 7d70bb9 commit ff06ced

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

examples/on-the-fly/kbv-r4/generate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ if (require.main === module) {
3232
registry: "https://packages.simplifier.net",
3333
ignorePackageIndex: true,
3434
})
35-
.fromPackage("hl7.fhir.r4.core", "4.0.1")
3635
.fromPackage("kbv.ita.for", "1.3.1")
36+
.fromPackage("de.basisprofil.r4", "1.6.0-ballot2")
37+
.fromPackage("kbv.basis", "1.8.0")
3738
.throwException()
3839
.typescript({
3940
withDebugComment: false,

src/typeschema/core/field-builder.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
FieldSlice,
1616
FieldSlicing,
1717
Name,
18+
PackageMeta,
1819
RegularField,
1920
RichFHIRSchema,
2021
TypeIdentifier,
@@ -25,6 +26,35 @@ import { mkBindingIdentifier, mkIdentifier } from "./identifier";
2526
import { mkSliceNameCandidates } from "./name-candidates";
2627
import { mkNestedIdentifier } from "./nested-types";
2728

29+
const R5_ONLY_TYPES = new Set([
30+
"Availability",
31+
"CodeableReference",
32+
"ExtendedContactDetail",
33+
"MonetaryComponent",
34+
"RatioRange",
35+
"VirtualServiceDetail",
36+
]);
37+
38+
const dependsOnR4Core = (register: Register, pkg: PackageMeta): boolean => {
39+
const pkgIndex = register.resolver[packageMetaToFhir(pkg)];
40+
if (!pkgIndex) return false;
41+
for (const options of Object.values(pkgIndex.canonicalResolution)) {
42+
for (const opt of options) {
43+
if (opt.pkg.name === "hl7.fhir.r4.core") return true;
44+
}
45+
}
46+
return false;
47+
};
48+
49+
const fieldTypeResolutionHint = (register: Register, pkg: PackageMeta, type: string): string => {
50+
if (!R5_ONLY_TYPES.has(type)) return "";
51+
if (!dependsOnR4Core(register, pkg)) return "";
52+
return (
53+
`\n hint: '${type}' is an R5+ type and is not available when generating against R4.` +
54+
`\n Either skip this canonical via skip-hack.ts, or upgrade the target to R5.`
55+
);
56+
};
57+
2858
function isRequired(register: Register, fhirSchema: RichFHIRSchema, path: string[]): boolean {
2959
const fieldName = path[path.length - 1];
3060
if (!fieldName) throw new Error(`Internal error: fieldName is missing for path ${path.join("/")}`);
@@ -283,10 +313,18 @@ export function buildFieldType(
283313
} else if (element.type) {
284314
const url = register.ensureSpecializationCanonicalUrl(element.type);
285315
const fieldFs = register.resolveFs(fhirSchema.package_meta, url);
286-
if (!fieldFs)
316+
if (!fieldFs) {
317+
const pkgId = packageMetaToFhir(fhirSchema.package_meta);
318+
const fieldPath = path.join(".");
319+
const hint = fieldTypeResolutionHint(register, fhirSchema.package_meta, element.type);
287320
throw new Error(
288-
`Could not resolve field type: <${fhirSchema.url}>.${path.join(".")}: <${element.type}> (pkg: '${packageMetaToFhir(fhirSchema.package_meta)}'))`,
321+
`Could not resolve field type:
322+
package: ${pkgId}
323+
schema: ${fhirSchema.url}
324+
field: ${fieldPath}
325+
type: ${element.type}${hint}`,
289326
);
327+
}
290328
return mkIdentifier(fieldFs);
291329
} else if (element.choices) {
292330
return undefined;

src/typeschema/skip-hack.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const availabilityInR4 = "Use Availability which is not provided by FHIR R4.";
55

66
export const skipList: Record<string, Record<CanonicalUrl, string>> = {
77
"hl7.fhir.uv.extensions.r4": {
8+
"http://hl7.org/fhir/StructureDefinition/biologicallyderivedproduct-manipulation": codeableReferenceInR4,
9+
"http://hl7.org/fhir/StructureDefinition/biologicallyderivedproduct-processing": codeableReferenceInR4,
810
"http://hl7.org/fhir/StructureDefinition/extended-contact-availability": availabilityInR4,
911
"http://hl7.org/fhir/StructureDefinition/immunization-procedure": codeableReferenceInR4,
1012
"http://hl7.org/fhir/StructureDefinition/specimen-additive": codeableReferenceInR4,

0 commit comments

Comments
 (0)