Skip to content

Commit

Permalink
Merge pull request #1545 from Nadeeshan96/master-use-clonewt-API
Browse files Browse the repository at this point in the history
Use convert API and enable disabled tests
  • Loading branch information
TharmiganK authored Apr 24, 2023
2 parents 640781d + b63cfd7 commit 81a4688
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ function testDataBindingWithTableofMapOfStringByTypeNegative() returns error? {
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
common:assertTrueTextPayload(response.getTextPayload(),
"data binding failed: {ballerina/lang.value}ConversionError");
"data binding failed: {ballerina}ConversionError");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand Down Expand Up @@ -771,8 +771,7 @@ function testDataBindingXmlNegative() {
}
}

// TODO: enable after fixing https://github.com/ballerina-platform/ballerina-lang/issues/38715
@test:Config {enable: false}
@test:Config {}
function testDataBindingXmlArray() {
xml[] j = [xml `<name>WSO2</name>`, xml `<name>Ballerina</name>`];
http:Response|error response = anydataBindingClient->post("/anydataB/checkXmlArray", j.toJson());
Expand All @@ -785,8 +784,7 @@ function testDataBindingXmlArray() {
}
}

// TODO: enable after fixing https://github.com/ballerina-platform/ballerina-lang/issues/38715
@test:Config {enable: false}
@test:Config {}
function testDataBindingXmlArrayByType() {
xml[] j = [xml `<name>WSO2</name>`, xml `<name>Ballerina</name>`];
http:Response|error response = anydataBindingClient->post("/anydataB/checkXmlArray", j.toJson(),
Expand All @@ -799,8 +797,7 @@ function testDataBindingXmlArrayByType() {
}
}

// TODO: enable after fixing https://github.com/ballerina-platform/ballerina-lang/issues/38715
@test:Config {enable: false}
@test:Config {}
function testDataBindingWithMapOfXml() {
xml wso2 = xml `<name>WSO2</name>`;
xml bal = xml `<name>Ballerina</name>`;
Expand All @@ -815,8 +812,7 @@ function testDataBindingWithMapOfXml() {
}
}

// TODO: enable after fixing https://github.com/ballerina-platform/ballerina-lang/issues/38715
@test:Config {enable: false}
@test:Config {}
function testDataBindingWithMapOfXmlByType() returns error? {
xml wso2 = xml `<name>WSO2</name>`;
xml bal = xml `<name>Ballerina</name>`;
Expand All @@ -832,8 +828,7 @@ function testDataBindingWithMapOfXmlByType() returns error? {
}
}

// TODO: enable after fixing https://github.com/ballerina-platform/ballerina-lang/issues/38715
@test:Config {enable: false}
@test:Config {}
function testDataBindingWithTableofMapOfXml() {
xml wso2 = xml `<name>WSO2</name>`;
xml bal = xml `<name>Ballerina</name>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function testDataBindingIncompatibleXMLPayload() {
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
common:assertTrueTextPayload(response.getTextPayload(),
"data binding failed: {ballerina/lang.value}ConversionError");
"data binding failed: {ballerina}ConversionError");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand Down Expand Up @@ -390,8 +390,8 @@ function testDataBindingStructWithNoMatchingContent() {
http:Response|error response = dataBindingClient->post("/dataBinding/body6", req);
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina/lang");
common:assertTrueTextPayload(response.getTextPayload(), ".value}ConversionError, {\"message\":\"'map<json>' ");
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina");
common:assertTrueTextPayload(response.getTextPayload(), "}ConversionError, {\"message\":\"'map<json>' ");
common:assertTrueTextPayload(response.getTextPayload(), "value cannot be converted to 'http_dispatching_tests:Person':");
common:assertTrueTextPayload(response.getTextPayload(), "missing required field 'age' of type 'int' in record 'http_dispatching_tests:Person'");
common:assertTrueTextPayload(response.getTextPayload(), "field 'team' cannot be added to the closed record 'http_dispatching_tests:Person'\"");
Expand All @@ -407,7 +407,7 @@ function testDataBindingStructWithInvalidTypes() {
http:Response|error response = dataBindingClient->post("/dataBinding/body7", req);
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina/lang.value}");
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina}");
common:assertTrueTextPayload(response.getTextPayload(), "'map<json>' value cannot be converted to 'http_dispatching_tests:Stock'");
common:assertTrueTextPayload(response.getTextPayload(), "missing required field 'price' of type 'float' in record 'http_dispatching_tests:Stock'");
common:assertTrueTextPayload(response.getTextPayload(), "missing required field 'id' of type 'int' in record 'http_dispatching_tests:Stock'");
Expand All @@ -425,7 +425,7 @@ function testDataBindingWithRecordArrayNegative() {
http:Response|error response = dataBindingClient->post("/dataBinding/body8", req);
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina/lang.value}" +
common:assertTrueTextPayload(response.getTextPayload(), "data binding failed: {ballerina}" +
"ConversionError, {\"message\":\"'json[]' value cannot be converted to 'http_dispatching_tests:Person[]'");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BRefValue;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.http.api.HttpConstants;
import io.ballerina.stdlib.http.api.HttpUtil;
import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage;
import org.ballerinalang.langlib.value.FromJsonWithType;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -119,10 +118,7 @@ public void populateFeed(HttpCarbonMessage httpCarbonMessage, ParamHandler param
paramFeed[index++] = paramArray;
} else if (paramType.getTag() == MAP_TAG || paramType.getTag() == RECORD_TYPE_TAG) {
Object json = JsonUtils.parse(queryValueArr.getBString(0).getValue());
Object param = FromJsonWithType.convert(json, paramType);
if (param instanceof BError) {
throw (BError) param;
}
Object param = ValueUtils.convert(json, paramType);
if (queryParam.isReadonly() && param instanceof BRefValue) {
((BRefValue) param).freezeDirect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.stdlib.http.api.BallerinaConnectorException;
import org.ballerinalang.langlib.value.FromJsonWithType;

import static io.ballerina.runtime.api.TypeTags.BOOLEAN_TAG;
import static io.ballerina.runtime.api.TypeTags.DECIMAL_TAG;
Expand Down Expand Up @@ -58,7 +58,7 @@ public static Object castParam(int targetParamTypeTag, String argValue) {
return ValueCreator.createDecimalValue(argValue);
case MAP_TAG:
Object json = JsonUtils.parse(argValue);
return FromJsonWithType.convert(json, MAP_TYPE);
return ValueUtils.convert(json, MAP_TYPE);
default:
return StringUtils.fromString(argValue);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ private static BArray getBArray(String[] valueArray, ArrayType arrayType, Type e
case MAP_TAG:
case RECORD_TYPE_TAG:
Object record = JsonUtils.parse(element);
arrayValue.add(index++, FromJsonWithType.convert(record, elementType));
arrayValue.add(index++, ValueUtils.convert(record, elementType));
break;
default:
throw new BallerinaConnectorException("Illegal state error: unexpected param type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
package io.ballerina.stdlib.http.api.service.signature.builder;

import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BRefValue;
import io.ballerina.stdlib.http.api.service.signature.converter.JsonToRecordConverter;
import io.ballerina.stdlib.mime.util.EntityBodyHandler;
import org.ballerinalang.langlib.value.FromJsonWithType;

/**
* The json type payload builder.
Expand All @@ -47,12 +45,9 @@ public Object getValue(BObject entity, boolean readonly) {
if (isSubtypeOfAllowedType(payloadType, TypeTags.RECORD_TYPE_TAG)) {
return JsonToRecordConverter.convert(payloadType, entity, readonly);
}
Object bjson = EntityBodyHandler.constructJsonDataSource(entity);
EntityBodyHandler.addJsonMessageDataSource(entity, bjson);
var result = FromJsonWithType.fromJsonWithType(bjson, ValueCreator.createTypedescValue(payloadType));
if (result instanceof BError) {
throw (BError) result;
}
Object bJson = EntityBodyHandler.constructJsonDataSource(entity);
EntityBodyHandler.addJsonMessageDataSource(entity, bJson);
Object result = ValueUtils.convert(bJson, payloadType);
if (readonly && result instanceof BRefValue) {
((BRefValue) result).freezeDirect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package io.ballerina.stdlib.http.api.service.signature.converter;

import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BRefValue;
import io.ballerina.stdlib.http.api.BallerinaConnectorException;
import io.ballerina.stdlib.mime.util.EntityBodyHandler;
import org.ballerinalang.langlib.value.CloneWithType;

/**
* The converter binds the JSON payload to a record.
Expand Down Expand Up @@ -55,12 +55,12 @@ private static Object getRecordEntity(BObject entity, Type entityBodyType) {
* Convert a json to the relevant record type.
*
* @param entityBodyType Represents entity body type
* @param bjson Represents the json value that needs to be converted
* @param bJson Represents the json value that needs to be converted
* @return the relevant ballerina record or object
*/
private static Object getRecord(Type entityBodyType, Object bjson) {
private static Object getRecord(Type entityBodyType, Object bJson) {
try {
return CloneWithType.convert(entityBodyType, bjson);
return ValueUtils.convert(bJson, entityBodyType);
} catch (NullPointerException ex) {
throw new BallerinaConnectorException("cannot convert payload to record type: " +
entityBodyType.getName());
Expand Down

0 comments on commit 81a4688

Please sign in to comment.