From cc85cbeb04656c2b4270e14fb3ca151a4d66309b Mon Sep 17 00:00:00 2001 From: Lee Surprenant Date: Sat, 21 May 2022 22:02:50 -0400 Subject: [PATCH] issue #3654 - update ConstraintGenerator to strip version suffix ConstraintGenerator.getProfiles() was called from 4 places in this class. All but one of those was for building the argument to the FHIRPath `extension()` function. That means that there should be no version suffix and so I renamed the method to `getProfilesWithoutVersion` and updated it accordingly. In the single other spot (`generateProfileConstraint(Node)`), I added the `getProfile()` logic in-line. Signed-off-by: Lee Surprenant --- .../ibm/fhir/profile/ConstraintGenerator.java | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/fhir-profile/src/main/java/com/ibm/fhir/profile/ConstraintGenerator.java b/fhir-profile/src/main/java/com/ibm/fhir/profile/ConstraintGenerator.java index 4cc4e3cefc1..0b39711ded4 100644 --- a/fhir-profile/src/main/java/com/ibm/fhir/profile/ConstraintGenerator.java +++ b/fhir-profile/src/main/java/com/ibm/fhir/profile/ConstraintGenerator.java @@ -1,5 +1,5 @@ /* - * (C) Copyright IBM Corp. 2019, 2021 + * (C) Copyright IBM Corp. 2019, 2022 * * SPDX-License-Identifier: Apache-2.0 */ @@ -368,7 +368,7 @@ private String discriminator(Node node) { // optimization if (hasProfileConstraint(elementDefinition)) { Type type = getTypes(elementDefinition).get(0); - String profile = getProfiles(type).get(0); + String profile = getProfilesWithoutVersion(type).get(0); sb.append("extension('").append(profile).append("')"); } else { String url = getExtensionUrl(node); @@ -490,13 +490,17 @@ private String generate(Node node) { return trace(node, sb.toString()); } + /** + * Guard calls to this method with calls to {@link #hasExtensionConstraint(ElementDefinition)} + * to ensure that the node's ElementDefinition has a single type with a single profile. + */ private String generateExtensionConstraint(Node node) { StringBuilder sb = new StringBuilder(); ElementDefinition elementDefinition = node.elementDefinition; Type type = getTypes(elementDefinition).get(0); - String profile = getProfiles(type).get(0); + String profile = getProfilesWithoutVersion(type).get(0); sb.append("extension('").append(profile).append("')").append(cardinality(node, sb.toString())); @@ -600,12 +604,18 @@ private String generatePatternValueConstraint(Node node, boolean discriminator) return sb.toString(); } + /** + * Guard calls to this method with calls to {@link #hasProfileConstraint(ElementDefinition)} + * to ensure that the node's ElementDefinition has a single type with a single profile. + */ private String generateProfileConstraint(Node node) { StringBuilder sb = new StringBuilder(); - + ElementDefinition elementDefinition = node.elementDefinition; - String profile = getProfiles(getTypes(elementDefinition).get(0)).get(0); + List types = getTypes(elementDefinition); + List profiles = types.get(0).getProfile(); + String profile = profiles.get(0).getValue(); sb.append("conformsTo('").append(profile).append("')"); return sb.toString(); @@ -618,6 +628,10 @@ private String generateProhibitedConstraint(ElementDefinition elementDefinition) return sb.toString(); } + /** + * Guard calls to this method with calls to {@link #hasReferenceTypeConstraint(ElementDefinition)} + * to ensure that the node's ElementDefinition has a single type. + */ private String generateReferenceTypeConstraint(Node node) { StringBuilder sb = new StringBuilder(); @@ -709,11 +723,29 @@ private String getMaxValueSet(Binding binding) { return null; } - private List getProfiles(Type type) { + /** + * Get the list of profiles referenced by the ElementDefinition.type. + * This method will strip any version or fragment suffixes from the canonical reference value. + */ + private List getProfilesWithoutVersion(Type type) { List profiles = new ArrayList<>(); for (Canonical profile : type.getProfile()) { - if (profile.getValue() != null) { - profiles.add(profile.getValue()); + if (profile.hasValue()) { + String profileString = profile.getValue(); + + // First strip off any version suffix + int delIndex = profileString.lastIndexOf("|"); + if (delIndex > 0) { + profileString = profileString.substring(0, delIndex); + } + + // If the version suffix didn't exist, we might have a fragment to strip + delIndex = profileString.lastIndexOf("#"); + if (delIndex > 0) { + profileString = profileString.substring(0, delIndex); + } + + profiles.add(profileString); } } return profiles; @@ -860,7 +892,7 @@ private boolean hasPatternValueConstraint(ElementDefinition elementDefinition) { private boolean hasProfileConstraint(ElementDefinition elementDefinition) { List types = getTypes(elementDefinition); if (types.size() == 1) { - List profiles = getProfiles(types.get(0)); + List profiles = getProfilesWithoutVersion(types.get(0)); return (profiles.size() == 1) && !isQuantityProfile(profiles.get(0)); } return false;