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

[BUG] <title> #86

Open
1 task done
madatr opened this issue Mar 18, 2022 · 5 comments
Open
1 task done

[BUG] <title> #86

madatr opened this issue Mar 18, 2022 · 5 comments

Comments

@madatr
Copy link

madatr commented Mar 18, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Hi, Firstly thank you so much for this super package/extension, its very useful.

I think I came across a strange bug when using r@ annotation in the .jsonc file.

When trying to create a class that has a required value object the script fails (does not generate the corresponding file) and return the following error:
Screenshot 2022-03-18 at 10 00 59

Strangely when trying to create a class that has a required list of objects it works flawlessly.

Please find the attached screenshots of some trials that I did:

Screenshot 2022-03-18 at 10 03 13

Screenshot 2022-03-18 at 10 16 04

Screenshot 2022-03-18 at 10 01 12

Screenshot 2022-03-18 at 10 05 09

Expected Behavior

that the required object files are generated

Steps To Reproduce

I am using Json Serialisable.

Version

latest

Relevant JSON syntax

{
        "__className": "location", // <- The base class name of the object.
        "__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
        "r@latitude": 0.1,
        "r@longitude": 0.1
    },
    {
        "__className": "customer_shop", // <- The base class name of the object.
        "__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
        "r@shopId": 5,
        "r@name": "shopName",
        "[email protected]": {},
        "phoneNumber": "0787383510",
        "r@shopCategory": "shopcategory"
    },
    {
        "__className": "customer_profile", // <- The base class name of the object.
        "__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
        "r@applicationUserId": 18,
        "r@block": false,
        "r@username": "+962700000000",
        "r@name": "TESTUSER",
        "r@phoneNumber": "+962700000000",
        "[email protected]_shop": [
            {}
        ]
    }

Anything else?

No response

@madatr
Copy link
Author

madatr commented Mar 18, 2022

ps. I tried changing the name of the file from "[email protected]": {} to "[email protected]": {} just in case it was confusing the script and it still resulted in the same error.

If it is useful here is the generated CustomerShop class whenever the error happens:

class CustomerShop {
  CustomerShop();

  factory CustomerShop.fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    throw UnimplementedError('CustomerShop.fromJson($json) is not implemented');
  }

  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    throw UnimplementedError();
  }
}

@iamarnas
Copy link
Collaborator

iamarnas commented Mar 18, 2022

@madatr Hi 👋 and thank you for your very detailed report.

By working with multiple JSON in one file you need to be careful. I see some bad practice. Given your example, you need to wrap your JSON to the list 👇

[ // <- Start of the list body.
    {
        "__className": "customer_shop",
        //...
    },
    {
        "__className": "customer_profile",
        //...
    }
] // // <- End of the list body.

Don't do that 👇

/** BAD */
[
    {
        "__className": "location", // <- Location object
        "__path": "/lib/models/",
        "laitidude": 0.1,
        "longitude": 0.1
    },
    {
        "__className": "customer_profile",
        "__path": "/lib/models/",
        "location": {} // <- same Location object in the same map.
        //...
    }
]

/** GOOD */
[
    {
        "__className": "customer_profile",
        "__path": "/lib/models/",
        "location": {  // <- put here instead Location.
            "laitidude": 0.1,
            "longitude": 0.1
        }
        //...
    }
]

Object naming 👇

/** BAD */
[
    {
        "__className": "customer_profile", // <- Required class name because the JSON object does not have a keyword.
        "location": { // <- This object has a keyword.
            "__className": "location", // <- Do not add a class name to an object with keys.
            "laitidude": 0.1,
            "longitude": 0.1
        }
        //...
    }
]

/** GOOD */
[
    {
        "__className": "customer_profile",  // <- There's only one name per object.
        "location": {
            "laitidude": 0.1,
            "longitude": 0.1
        }
        //...
    }
]

Try to upgrade your JSON and tell us about the problems you are facing 😉
I will check the required annotation for objects.

@madatr
Copy link
Author

madatr commented Mar 18, 2022

Hi @iamarnas thank you for the quick reply and for the nice tips, I just started using your package and in fact jsonc only today. If I find the time I may contribute some time to add to the documentation of this package for newbies like me.

I followed your recommendations for the json but it still gives me the error and not generate any class file whenever I set the location to required. (I did try both ways) so I guess it may be a bug after all.

Screenshot 2022-03-18 at 14 00 14

Here is the snippet if you'd like to try and replicate the issue:

[{
        "__className": "customer_profile", // <- The base class name of the object.
        "__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
        "r@applicationUserId": 18,
        "r@block": false,
        "r@username": "+962700000000",
        "r@name": "TESTUSER",
        "r@phoneNumber": "+962700000000",
        "[email protected]_shop": [
            {
                "r@shopId": 5,
                "r@name": "shopName",
                "r@location": {
                    "r@latitude": 0.1,
                    "r@longitude": 0.1
                },
                "phoneNumber": "0787383510",
                "r@shopCategory": "shopcategory"
            }
        ]
    }]

@iamarnas
Copy link
Collaborator

@madatr I will look at this when I will find a time. For now you can generate without r@ and add manually required keyword.

@madatr
Copy link
Author

madatr commented Mar 18, 2022

Take your time, already did :)

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

No branches or pull requests

2 participants