Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2201.8.x] Fix value conversion to union types with cyclic members #41936

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading