Skip to content

Commit

Permalink
Merge pull request #43461 from HindujaB/json-string-fix-master
Browse files Browse the repository at this point in the history
[master] Fix json string to record with union fields conversion
  • Loading branch information
gimantha authored Oct 8, 2024
2 parents b67b420 + e773263 commit 4105ab0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,15 @@ private void processRecordType(Type targetType) throws ParserException {
private void processJsonAnydataType() {
if (this.nodesStackSizeWhenUnionStarts == this.nodesStack.size()) {
this.targetTypes.remove(this.targetTypes.size() - 1);
this.nodesStackSizeWhenUnionStarts = -1;
}
}

private void processUnionTableFiniteType(Type targetType) {
if (this.nodesStackSizeWhenUnionStarts == this.nodesStack.size()) {
this.targetTypes.remove(this.targetTypes.size() - 1);
this.currentJsonNode = convert(this.currentJsonNode, targetType);
this.nodesStackSizeWhenUnionStarts = -1;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ type Foo6 record {
string x3;
};

public type EndpointSecurity record {
BasicEndpointSecurity|APIKeyEndpointSecurity securityType?;
};

public type BasicEndpointSecurity record {
string secretName;
string userNameKey;
};

public type APIKeyEndpointSecurity record {
string secretName;
string apiKeyNameKey;
};

public type EndpointConfiguration record {
EndpointSecurity endpointSecurity?;
};

function testFromJsonWithTypeRecord2() {
string str = "{\"name\":\"Name\",\"age\":35}";
json j = <json>checkpanic str.fromJsonString();
Expand Down Expand Up @@ -410,6 +428,20 @@ function testFromJsonStringWithTypeRecord() {
assertEquality(studentOrError is Student3, true);
Student3 student = checkpanic studentOrError;
assertEquality(student.name, "Name");

string security = string `{"securityType":{"secretName":"backend-creds","userNameKey":"username"}}`;
EndpointSecurity|error securityOrError = security.fromJsonStringWithType();
assertEquality(securityOrError is EndpointSecurity, true);
EndpointSecurity epSecurity = <EndpointSecurity> checkpanic securityOrError;
assertEquality(epSecurity.securityType.toString(), "{\"secretName\":\"backend-creds\",\"userNameKey\":\"username\"}");

string epConfig = string `{"endpoint":{},"endpointSecurity":{"securityType":{"secretName":"backend-creds","userNameKey":"username"}}}`;
EndpointConfiguration|error epConfigOrError = epConfig.fromJsonStringWithType();
assertEquality(epConfigOrError is EndpointConfiguration, true);
EndpointConfiguration epConfiguration = <EndpointConfiguration> checkpanic epConfigOrError;
assertEquality(epConfiguration.endpointSecurity.toString(), string `{"securityType":{"secretName":"backend-creds","userNameKey":"username"}}`);
anydata ep = epConfiguration["endpoint"];
assertEquality(ep.toString(), "{}");
}

function testFromJsonStringWithAmbiguousType() {
Expand Down

0 comments on commit 4105ab0

Please sign in to comment.