Skip to content

Commit

Permalink
Merge pull request #41936 from Nadeeshan96/8x-check-clone-bug
Browse files Browse the repository at this point in the history
[2201.8.x] Fix value conversion to union types with cyclic members
  • Loading branch information
warunalakshitha authored Feb 8, 2024
2 parents 28796a1 + 061f823 commit ffb3558
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -368,12 +369,12 @@ public static Type getConvertibleTypeInTargetUnionType(Object inputValue, BUnion

for (Type memType : memberTypes) {
if (TypeChecker.checkIsLikeType(inputValue, memType, false)) {
return getConvertibleType(inputValue, memType, varName, unresolvedValues, errors, false);
return getConvertibleType(inputValue, memType, varName, new HashSet<>(unresolvedValues), errors, false);
}
}
for (Type memType : memberTypes) {
Type convertibleTypeInUnion = getConvertibleType(inputValue, memType, varName,
unresolvedValues, errors, allowNumericConversion);
new HashSet<>(unresolvedValues), errors, allowNumericConversion);
if (convertibleTypeInUnion != null) {
return convertibleTypeInUnion;
}
Expand All @@ -391,7 +392,7 @@ private static Type getConvertibleStructuredTypeInUnion(Object inputValue, Strin
for (Type memType : memberTypes) {
initialErrorCount = errors.size();
Type convertibleTypeInUnion = getConvertibleType(inputValue, memType, varName,
unresolvedValues, errors, allowNumericConversion);
new HashSet<>(unresolvedValues), errors, allowNumericConversion);
currentErrorListSize = errors.size();
if (convertibleTypeInUnion != null) {
errors.subList(initialErrorListSize - 1, currentErrorListSize).clear();
Expand Down
108 changes: 108 additions & 0 deletions langlib/langlib-test/src/test/resources/test-src/valuelib_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,89 @@ function testCloneWithTypeWithAmbiguousUnion() {
<string>checkpanic err.detail()["message"]);
}

public type CodingExtension record {|
*Element;

uri url;
Coding valueCoding;
|};

public type Coding record {|
*Element;

string id?;
Extension[] extension?;
uri system?;
string 'version?;
code code?;
string display?;
boolean userSelected?;
|};

public type code string;

public type uri string;

public type ElementReadOnlyCyclic record {|
string id?;
Extension[] extension?;
((ElementReadOnlyCyclic & readonly)|())...;
|};

public type Element record {|
string id?;
Extension[] extension?;
Element...;
|};

public type ExtensionExtension record {|
*Element;

uri url;
Extension[] extension?;
|};

public type CodeableConceptExtension record {|
*Element;

uri url;
CodeableConcept valueCodeableConcept;
|};

public type CodeableConcept record {|
*Element;

string id?;
Extension[] extension?;
Coding[] coding?;
string text?;
|};

public type ExtensionExtension2 record {|
*ElementReadOnlyCyclic;

uri url;
Extension[] extension?;
|};

public type CodeableConceptExtension2 record {|
*ElementReadOnlyCyclic;

uri url;
CodeableConcept valueCodeableConcept;
|};

public type CodingExtension2 record {|
*ElementReadOnlyCyclic;

uri url;
Coding valueCoding;
|};

public type Extension CodeableConceptExtension|ExtensionExtension|CodingExtension;

public type Extension2 CodeableConceptExtension2|ExtensionExtension2|CodingExtension2;

function testCloneWithTypeToUnion() {
int|float|[string, string] unionVar = 2;
float|decimal|[string, int]|error tupleValue = unionVar.cloneWithType(UnionTypedesc);
Expand All @@ -2359,6 +2442,31 @@ function testCloneWithTypeToUnion() {
assertFalse(tupleValue is decimal);
assertFalse(tupleValue is [string, int]);
assertFalse(tupleValue is error);

json extCoding = {
"valueCoding": {
"system": "http://loinc.org",
"code": "LA29518-0",
"display": "he/him/his/himself"
},
"url": "http://open.epic.com/FHIR/StructureDefinition/extension/calculated-pronouns-to-use-for-text"
};

Extension ext = checkpanic extCoding.cloneWithType();
assertEquality(ext, <Extension>{
"url":
"http://open.epic.com/FHIR/StructureDefinition/extension/calculated-pronouns-to-use-for-text",
"valueCoding": {"system": "http://loinc.org", "code": "LA29518-0", "display": "he/him/his/himself"}
});
assertEquality((typeof ext).toString(), "typedesc CodingExtension");

Extension2 ext2 = checkpanic extCoding.cloneWithType();
assertEquality(ext2, <Extension2>{
"url":
"http://open.epic.com/FHIR/StructureDefinition/extension/calculated-pronouns-to-use-for-text",
"valueCoding": {"system": "http://loinc.org", "code": "LA29518-0", "display": "he/him/his/himself"}
});
assertEquality((typeof ext2).toString(), "typedesc CodingExtension2");
}

type UnionTypedesc typedesc<float|decimal|[string, int]>;
Expand Down

0 comments on commit ffb3558

Please sign in to comment.