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

const string array not being added to json payload #43748

Open
tharindu-nw opened this issue Jan 16, 2025 · 2 comments · May be fixed by #43751
Open

const string array not being added to json payload #43748

tharindu-nw opened this issue Jan 16, 2025 · 2 comments · May be fixed by #43751
Assignees
Labels
Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@tharindu-nw
Copy link
Contributor

Description

I have a string array that I need to send inside a json payload of an http request. I have declared this array a const. When I add it to the json payload and send the request, the value is not being sent in the request.

Steps to Reproduce

Declare a const string array:

const string[] CONST_ROLES = ["Admin"];

Add this value in an http request payload:

http:Request newRequest = new;
newRequest.setJsonPayload({
      roles: CONST_ROLES,
      name: "John"
});
http:Response|error newResponse = httpClient->post("/req/path", newRequest);

Trace logs show the following as the req payload:

{"roles":, "name":"John"}

Version

2201.10.0

Environment Details (with versions)

OS: MacOS Sonoma

@TharmiganK
Copy link
Contributor

Constant arrays are represented as tuple type in Ballerina. We are using JsonUtils.Serialize lang API to convert the json to a output stream which writes on the wire. Seems like the issue is with this lang API which does not consider the tuple case:

switch (TypeUtils.getImpliedType(TypeChecker.getType(json)).getTag()) {

@warunalakshitha / @HindujaB FYI

As a workaround, you could use the toJson on the actual json value to make it work:

http:Request newRequest = new;
newRequest.setJsonPayload({
      roles: CONST_ROLES,
      name: "John"
}.toJson());
http:Response|error newResponse = httpClient->post("/req/path", newRequest);

@TharmiganK TharmiganK transferred this issue from ballerina-platform/ballerina-library Jan 16, 2025
@TharmiganK TharmiganK added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Jan 16, 2025
@HindujaB
Copy link
Contributor

The JSONGenerator class has an older implementation where it assumes the object to be of explicit json type where all the elements are either from JSON primitives, json[], or map<json> types. Therefore it misses the alternative representations such as records and tuples.
Another workaround from the native side would be to use JsonUtils.convertToJson(value) before serializing the value.
We will provide a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Status: PR Sent
Development

Successfully merging a pull request may close this issue.

3 participants