Skip to content

Commit

Permalink
issue #3654 - update ConstraintGenerator to strip version suffix
Browse files Browse the repository at this point in the history
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 <lmsurpre@us.ibm.com>
  • Loading branch information
lmsurpre committed May 22, 2022
1 parent 4d8235d commit aedfd7f
Showing 1 changed file with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright IBM Corp. 2019, 2021
* (C) Copyright IBM Corp. 2019, 2022
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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<Type> types = getTypes(elementDefinition);
List<Canonical> profiles = types.get(0).getProfile();
String profile = profiles.get(0).getValue();
sb.append("conformsTo('").append(profile).append("')");

return sb.toString();
Expand All @@ -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();

Expand Down Expand Up @@ -709,11 +723,29 @@ private String getMaxValueSet(Binding binding) {
return null;
}

private List<String> 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<String> getProfilesWithoutVersion(Type type) {
List<String> 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;
Expand Down Expand Up @@ -860,7 +892,7 @@ private boolean hasPatternValueConstraint(ElementDefinition elementDefinition) {
private boolean hasProfileConstraint(ElementDefinition elementDefinition) {
List<Type> types = getTypes(elementDefinition);
if (types.size() == 1) {
List<String> profiles = getProfiles(types.get(0));
List<String> profiles = getProfilesWithoutVersion(types.get(0));
return (profiles.size() == 1) && !isQuantityProfile(profiles.get(0));
}
return false;
Expand Down

0 comments on commit aedfd7f

Please sign in to comment.