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

jsondata:parseAsType() return a error when the given type is readonly #7253

Open
lnash94 opened this issue Oct 11, 2024 · 3 comments · May be fixed by ballerina-platform/module-ballerina-data.jsondata#45

Comments

@lnash94
Copy link
Member

lnash94 commented Oct 11, 2024

Description:
The below sample returns an error while parsing using jsondata:parserAsType(), this error occurred when given data type has data type with readonly ,

import ballerina/io;
import ballerina/data.jsondata;

public type RestaurantNew record {|
    *Restaurant;
    [int, string, decimal, float, User] address;
|};

public enum MenuType {
    BREAK_FAST,
    LUNCH,
    DINNER
}

public type MenuItem record {|
    int _id;
    string name;
    string description;
    decimal price;
|};

public type Menu record {|
    int _id;
    MenuType 'type;
    MenuItem[] items;
|};

public type Restaurant record {|
    string name;
    string city;
    string description;
    Menu[] menus;
|};

public type User readonly & record {|
    int id;
    int age;
|};


public function main() returns jsondata:Error? {
    json restaurant = {
        name: "name1",
        city: "city1",
        address: [10, "street", 9, 12.45, {id: 4012, age: 36}],
        description: "description1",
        menus: [
            {
                _id: 5,
                'type: "LUNCH",
                items: []
            }
        ]
    };
    RestaurantNew r = check jsondata:parseAsType(restaurant);
    
    io:println(r);
}

Steps to reproduce:
Run the above given sample

Affected Versions:
2201.10.1
OS, DB, other environment details and versions:

Related Issues (optional):

Suggested Labels (optional):

Suggested Assignees (optional):

@lnash94
Copy link
Member Author

lnash94 commented Oct 11, 2024

This issue is a blocker for the module http , jsondata APIs migration in u11. ballerina-platform/module-ballerina-http#2168
And provide this support : #4870 (comment)

@SasinduDilshara
Copy link

The simplest version of the error

import ballerina/io;
import ballerina/data.jsondata;
public type User readonly & record {|
    int id;
|};

public function main() returns jsondata:Error? {
    json user2 = {id: 4012};
    User r2 = check jsondata:parseAsType(user2);
    io:println(r2);
}

@lnash94
Copy link
Member Author

lnash94 commented Oct 14, 2024

We received an error for the below scenario as well

type DDPerson record {|
    readonly int id;
|};

DDPerson rs = check jsondata:parseAsType({id: 10});
error: {ballerina}TypeCastError {"message":"incompatible types: 'error' cannot be cast to '(anydata|data.jsondata:Error)'"}
        at ballerina.data.jsondata.0:parseAsType(json_api.bal:24)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants