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

[master] Use toJson method from data.jsondata module in http client #2203

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d07f5fd
Use toJson method from jsondata module
lnash94 Sep 18, 2024
5514b62
Merge branch 'master' of https://github.com/ballerina-platform/module…
lnash94 Sep 18, 2024
3b8c267
Update data.jsondata version
lnash94 Sep 18, 2024
f373e02
[Automated] Update the native jar versions
lnash94 Sep 18, 2024
9ad1b3b
Update dependencies for data.jsondata version
lnash94 Sep 19, 2024
daf51fe
[Automated] Update the native jar versions
lnash94 Sep 19, 2024
73c58e7
[Automated] Update the native jar versions
lnash94 Sep 19, 2024
a8f7a5a
[Automated] Update the native jar versions
lnash94 Sep 19, 2024
a23dcca
[Automated] Update the native jar versions
lnash94 Sep 19, 2024
b48db72
Update Ballerina toml dependency
lnash94 Sep 19, 2024
adbff7e
Update the service data biding with toJson
lnash94 Sep 20, 2024
932492b
[Automated] Update the native jar versions
lnash94 Sep 20, 2024
21cf933
Fix tests with new parserasType , toJson migration
lnash94 Sep 20, 2024
cf81568
Update the tests
lnash94 Sep 23, 2024
a017d4a
Disable test related to table type
lnash94 Sep 23, 2024
1addfb2
Merge branch 'master' into integrate-data.json
lnash94 Sep 23, 2024
2470d52
Update json data version
lnash94 Sep 23, 2024
14d017f
Merge branch 'integrate-data.json' of https://github.com/lnash94/modu…
lnash94 Sep 23, 2024
5e10d38
Fix http2 tests
lnash94 Sep 23, 2024
65646c2
Add test for new migration
lnash94 Sep 23, 2024
a639dee
Merge branch 'master' into integrate-data.json
lnash94 Sep 23, 2024
e76835a
Add tests and fix review suggestions
lnash94 Sep 23, 2024
a261992
Merge branch 'integrate-data.json' of https://github.com/lnash94/modu…
lnash94 Sep 23, 2024
6419f30
Resolve conflicts
lnash94 Sep 23, 2024
73d91ab
[Automated] Update the native jar versions
lnash94 Sep 23, 2024
d2f70ee
Remove unwanted dependencies
lnash94 Sep 23, 2024
e65a1ad
Updated the changelog.md
lnash94 Sep 23, 2024
6f4fe95
Updated tests
lnash94 Sep 23, 2024
665c8e9
Merge branch 'master' into integrate-data.json
lnash94 Sep 25, 2024
df75b5c
Add workaround for the test failure
lnash94 Sep 27, 2024
12988be
Merge branch 'integrate-data.json' of https://github.com/lnash94/modu…
lnash94 Sep 27, 2024
cb80e0b
Merge branch 'master' into integrate-data.json
lnash94 Sep 27, 2024
4aec3be
Revert "Add workaround for the test failure"
lnash94 Sep 27, 2024
cc0a3a7
[Automated] Update the native jar versions
lnash94 Oct 8, 2024
4a8aecc
[Automated] Update the native jar versions
lnash94 Oct 8, 2024
651a110
Merge branch 'master' of https://github.com/ballerina-platform/module…
lnash94 Oct 9, 2024
a66cc08
[Automated] Update the native jar versions
lnash94 Oct 10, 2024
18b9d18
[Automated] Update the native jar versions
lnash94 Oct 10, 2024
d853e6e
Update tests
lnash94 Oct 10, 2024
6b2a72b
Merge branch 'master' of https://github.com/ballerina-platform/module…
lnash94 Oct 10, 2024
f925f94
Merge branch 'integrate-data.json' of https://github.com/lnash94/modu…
lnash94 Oct 10, 2024
1e0d0f8
[Automated] Update the native jar versions
lnash94 Oct 11, 2024
e40b281
Merge branch 'master' into integrate-data.json
lnash94 Oct 15, 2024
2e36e15
Update data.json data version
lnash94 Oct 29, 2024
c93ac98
Merge branch 'master' of https://github.com/ballerina-platform/module…
lnash94 Oct 29, 2024
88d7ffb
Merge branch 'integrate-data.json' of https://github.com/lnash94/modu…
lnash94 Oct 29, 2024
3853c40
Update test case
lnash94 Nov 1, 2024
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
13 changes: 12 additions & 1 deletion ballerina-tests/http-advanced-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,16 @@ modules = [
{org = "ballerina", packageName = "crypto", moduleName = "crypto"}
]

[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.3.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -79,6 +89,7 @@ dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
17 changes: 16 additions & 1 deletion ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,19 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.3.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]
modules = [
{org = "ballerina", packageName = "data.jsondata", moduleName = "data.jsondata"}
]

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -76,6 +89,7 @@ dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -105,6 +119,7 @@ name = "http_client_tests"
version = "2.13.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "http_test_common"},
{org = "ballerina", name = "io"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/mime;
import ballerina/test;
import ballerina/data.jsondata;

service /api on new http:Listener(resBindingAdvancedPort) {

Expand All @@ -41,6 +43,22 @@ service /api on new http:Listener(resBindingAdvancedPort) {
resource function get byteArray() returns byte[] {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
}

resource function get overwriteNames/jsont(boolean y) returns json|TPerson {
if y {
return {"name": "John", "age": "23"};
}
TPerson t = {firstName: "Potter", personAge: "30"};
return t;
}

resource function post overwriteNames/jsont(TPerson payload) returns TPerson {
return payload;
}

resource function get status/code() returns OKPerson {
return {body: {firstName: "Potter", personAge: "40"}};
}
}

final http:Client clientEP = check new (string `localhost:${resBindingAdvancedPort}/api`);
Expand Down Expand Up @@ -101,3 +119,39 @@ function testResponseWithAnydataResBinding() returns error? {
test:assertFail("Invalid response type");
}
}

public type TPerson record {
@jsondata:Name {
value: "name"
}
string firstName;
@jsondata:Name {
value: "age"
}
string personAge;
};

public type OKPerson record {|
*http:Ok;
TPerson body;
|};

@test:Config {enable: false}
function clientoverwriteResponseJsonName() returns error? {
TPerson res1 = check clientEP->/overwriteNames/jsont(y = true);
test:assertEquals(res1, {firstName: "John", personAge: "23"});

json res2 = check clientEP->/overwriteNames/jsont(y = false);
test:assertEquals(res2, {"name": "Potter", "age": "30"});

json j = {
name: "Sumudu",
age: "29"
};

TPerson res3 = check clientEP->/overwriteNames/jsont.post(j);
test:assertEquals(res3, {firstName: "Sumudu", personAge: "29"});

json res4 = check clientEP->/status/code;
test:assertEquals(res4, {name: "Potter", age: "40"});
}
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,9 @@ function testAllBindingErrorsWithNillableTypes() returns error? {
test:assertEquals(response.statusCode, 200, msg = "Found unexpected output");
common:assertHeaderValue(check response.getHeader(common:CONTENT_TYPE), common:TEXT_PLAIN);
common:assertTextPayload(response.getTextPayload(),
"Payload binding failed: 'map<json>' value cannot be converted to " +
"'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>?'|" +
"incompatible typedesc int? found for 'text/plain' mime type");
"Payload binding failed: incompatible expected type 'xml<(lang.xml:Element|lang.xml:Comment|" +
"lang.xml:ProcessingInstruction|lang.xml:Text)>?' for value " +
"'{\"id\":\"chamil\",\"values\":{\"a\":2,\"b\":45,\"c\":{\"x\":\"mnb\",\"y\":\"uio\"}}}'|incompatible typedesc int? found for 'text/plain' mime type");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand Down Expand Up @@ -815,9 +815,7 @@ function testDBRecordErrorNegative() {
ClientDBErrorPerson|error response = clientDBBackendClient->post("/backend/getRecord", "want record");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'map<json>' value cannot be converted to 'http_client_tests:ClientDBErrorPerson'");
common:assertTrueTextPayload(response.message(),
"missing required field 'weight' of type 'float' in record 'http_client_tests:ClientDBErrorPerson'");
"Payload binding failed: required field 'weight' not present in JSON");
} else {
test:assertFail(msg = "Found unexpected output type: ClientDBErrorPerson");
}
Expand All @@ -828,7 +826,7 @@ function testDBRecordArrayNegative() {
ClientDBErrorPerson[]|error response = clientDBBackendClient->post("/backend/getRecordArr", "want record arr");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'http_client_tests:ClientDBErrorPerson[]'");
"Payload binding failed: required field 'weight' not present in JSON");
} else {
test:assertFail(msg = "Found unexpected output type: ClientDBErrorPerson[]");
}
Expand All @@ -852,7 +850,7 @@ function testMapOfStringDataBindingWithJsonPayload() {
map<string>|error response = clientDBBackendClient->get("/backend/getJson");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'map<json>' value cannot be converted to 'map<string>'");
"Payload binding failed: incompatible expected type 'string' for value '{\"a\":2,\"b\":45,\"c\":{\"x\":\"mnb\",\"y\":\"uio\"}}'");
} else {
test:assertFail(msg = "Found unexpected output type: map<string>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function testIntMapDatabindingByType() returns error? {
test:assertEquals(response, {"name": 11, "team": 22}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testIntTableDatabinding() returns error? {
table<map<int>> tbl = check clientDBBackendClient->get("/anydataTest/intTableType");
object {
Expand All @@ -355,7 +355,7 @@ function testIntTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableOrMapofIntArrayDatabinding() returns error? {
map<int>[]|table<map<int>> response = check clientDBBackendClient->get("/anydataTest/intTableType");
if response is map<int>[] {
Expand All @@ -369,7 +369,7 @@ function testIntTableOrMapofIntArrayDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableOrXmlArrayDatabinding() returns error? {
table<map<int>>|xml tbl = check clientDBBackendClient->get("/anydataTest/intTableType");
if tbl is table<map<int>> {
Expand All @@ -387,7 +387,7 @@ function testIntTableOrXmlArrayDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableDatabindingByType() returns error? {
table<map<int>> tbl = check clientDBBackendClient->get("/anydataTest/intTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -448,7 +448,7 @@ function testStringMapDatabindingByType() returns error? {
test:assertEquals(response, {name: "hello", team: "ballerina"}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testStringTableDatabinding() returns error? {
table<map<string>> tbl = check clientDBBackendClient->get("/anydataTest/stringTableType");
object {
Expand All @@ -462,7 +462,7 @@ function testStringTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testStringTableDatabindingByType() returns error? {
table<map<string>> tbl = check clientDBBackendClient->get("/anydataTest/stringTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -508,7 +508,7 @@ function testRecordMapDatabindingByType() returns error? {
test:assertEquals(response.get("1"), {name: "hello", age: 23}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testRecordTableDatabinding() returns error? {
table<ClientAnydataDBPerson> tbl = check clientDBBackendClient->get("/anydataTest/recordTableType");
object {
Expand All @@ -522,7 +522,7 @@ function testRecordTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable: false}
function testRecordTableDatabindingByType() returns error? {
table<ClientAnydataDBPerson> tbl = check clientDBBackendClient->get("/anydataTest/recordTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -574,7 +574,7 @@ function testByteArrMapDatabindingByType() returns error? {
test:assertEquals(check strings:fromBytes(val), "STDLIB", msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable: false}
function testByteArrTableDatabinding() returns error? {
table<map<byte[]>> response = check clientDBBackendClient->get("/anydataTest/byteArrTableType");
object {
Expand All @@ -589,7 +589,7 @@ function testByteArrTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testByteArrTableDatabindingByType() returns error? {
table<map<byte[]>> response = check clientDBBackendClient->get("/anydataTest/byteArrTableTypeWithInvalidMimeType");
object {
Expand All @@ -609,7 +609,7 @@ function testXmlArrDatabinding() {
xml[]|error response = clientDBBackendClient->get("/anydataTest/xmlArrType");
if response is error {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'xml<");
"Payload binding failed: invalid type 'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>' expected 'anydata'");
} else {
test:assertEquals(response[0], xml `<name>WSO2</name>`, msg = "Found unexpected output");
}
Expand All @@ -620,7 +620,7 @@ function testXmlArrDatabindingByType() {
xml[]|error response = clientDBBackendClient->get("/anydataTest/xmlArrTypeWithInvalidMimeType");
if response is error {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'xml");
"Payload binding failed: invalid type 'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>' expected 'anydata'");
} else {
test:assertEquals(response[0], xml `<name>WSO2</name>`, msg = "Found unexpected output");
}
Expand Down
30 changes: 20 additions & 10 deletions ballerina-tests/http-client-tests/tests/sc_res_binding_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,21 @@ service /api on new http:Listener(statusCodeBindingPort2) {
}

resource function get v1/albums/[string id]() returns AlbumFoundWithNamedHeaders|AlbumNotFoundWithNamedHeaders {
if albums.hasKey(id) {
return {
body: albums.get(id),
headers: {userId: "user-1", reqId: 1}
};
}
if albums.hasKey(id) {
return {
body: {albumId: id, message: "Album not found"},
body: albums.get(id),
headers: {userId: "user-1", reqId: 1}
};
}
return {
body: {albumId: id, message: "Album not found"},
headers: {userId: "user-1", reqId: 1}
};
}

resource function get album/auther() returns OKPerson {
return {body: {firstName: "Potter", personAge: "40"}};
}
}

final http:StatusCodeClient albumClient = check new (string `localhost:${statusCodeBindingPort2}/api`);
Expand Down Expand Up @@ -309,7 +313,7 @@ function testGetSuccessStatusCodeResponse() returns error? {
if res2 is error {
test:assertTrue(res2 is http:StatusCodeResponseBindingError);
test:assertEquals(res2.message(), "incompatible type: AlbumNotFound found for the response with status code: 200",
"Invalid error message");
"Invalid error message");
error? cause = res2.cause();
if cause is error {
test:assertEquals(cause.message(), "no 'anydata' type found in the target type", "Invalid cause error message");
Expand Down Expand Up @@ -467,8 +471,8 @@ function testUnionPayloadBindingWithStatusCodeResponse() returns error? {
AlbumFoundInvalid|AlbumFound|AlbumNotFound|error res5 = albumClient->/albums/'1;
if res5 is error {
test:assertTrue(res5 is http:PayloadBindingError);
test:assertTrue(res5.message().includes("Payload binding failed: 'map<json>' value cannot be" +
" converted to 'http_client_tests:AlbumInvalid"), "Invalid error message");
test:assertTrue(res5.message().includes("Payload binding failed: required field 'invalidField' not present in JSON"),
"Invalid error message");
} else {
test:assertFail("Invalid response type");
}
Expand Down Expand Up @@ -670,3 +674,9 @@ function testStatusCodeBindingWithNamedHeaders() returns error? {
test:assertFail("Invalid response type");
}
}

@test:Config {}
function testOverwriteName() returns error? {
OKPerson res = check albumClient->/album/auther;
test:assertEquals(res.body, {firstName: "Potter", personAge: "40"});
}
13 changes: 12 additions & 1 deletion ballerina-tests/http-dispatching-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,16 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.3.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -76,6 +86,7 @@ dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
Loading
Loading