From 14505af3b597e3153c85dbad23307a5ddcc339d8 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 3 Jun 2020 09:32:52 +1000 Subject: [PATCH] Fix NPE validating from unknown code system when the code is in extension --- .../terminologies/ValueSetCheckerSimple.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetCheckerSimple.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetCheckerSimple.java index a27711c11..6e2caa7e9 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetCheckerSimple.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetCheckerSimple.java @@ -134,7 +134,7 @@ public class ValueSetCheckerSimple implements ValueSetChecker { cs = findSpecialCodeSystem(system); } if (cs == null) { - warningMessage = "Unable to resolve system "+system+" - system is not specified or implicit"; + warningMessage = "Unable to resolve system "+system; if (!inExpansion) throw new FHIRException(warningMessage); } @@ -144,8 +144,12 @@ public class ValueSetCheckerSimple implements ValueSetChecker { throw new FHIRException(warningMessage); } - if (cs!=null) + if (cs!=null) { res = validateCode(code, cs); + } else { + // it's in the expansion, but we could find it in a code system + res = findCodeInExpansion(code); + } } else { inExpansion = checkExpansion(code); } @@ -175,13 +179,38 @@ public class ValueSetCheckerSimple implements ValueSetChecker { return null; } - boolean checkExpansion(Coding code) { + private ValidationResult findCodeInExpansion(Coding code) { + if (valueset==null || !valueset.hasExpansion()) + return null; + return findCodeInExpansion(code, valueset.getExpansion().getContains()); + } + + private ValidationResult findCodeInExpansion(Coding code, List contains) { + for (ValueSetExpansionContainsComponent containsComponent: contains) { + if (containsComponent.getSystem().equals(code.getSystem()) && containsComponent.getCode().equals(code.getCode())) { + ConceptDefinitionComponent ccd = new ConceptDefinitionComponent(); + ccd.setCode(containsComponent.getCode()); + ccd.setDisplay(containsComponent.getDisplay()); + ValidationResult res = new ValidationResult(ccd); + return res; + } + if (containsComponent.hasContains()) { + ValidationResult res = findCodeInExpansion(code, containsComponent.getContains()); + if (res != null) { + return res; + } + } + } + return null; + } + + private boolean checkExpansion(Coding code) { if (valueset==null || !valueset.hasExpansion()) return false; return checkExpansion(code, valueset.getExpansion().getContains()); } - boolean checkExpansion(Coding code, List contains) { + private boolean checkExpansion(Coding code, List contains) { for (ValueSetExpansionContainsComponent containsComponent: contains) { if (containsComponent.getSystem().equals(code.getSystem()) && containsComponent.getCode().equals(code.getCode())) return true;