hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 18 forks source link

[BUG] Duplicate models generated #64

Closed Prn-Ice closed 2 years ago

Prn-Ice commented 2 years ago

Is there an existing issue for this?

Current Behavior

I have the following models declared in my models.jsonc

{
        "__className": "create_wallet_address_response",
        "__path": "/lib/app/modules/deposit_withdraw/data/models/create_wallet_address_response",
        "status": "success",
        "data.walletAddress": {
            "id": "4e252595-d326-4c26-89cc-f23d001ffa4a",
            "account_id": "348e961f-899f-4790-b8e8-712616b74614",
            "address": "1Msn2kMzveVCgX7i1ZpJWHGnEmNP7LBBL1",
            "coin": "BTC"
        }
    },
{
        "__className": "wallet_addresses_response",
        "__path": "/lib/app/modules/deposit_withdraw/data/models/wallet_addresses_response",
        "status": "success",
        "data.walletAddress": [
            {
                "id": "4e252595-d326-4c26-89cc-f23d001ffa4a",
                "account_id": "348e961f-899f-4790-b8e8-712616b74614",
                "address": "1Msn2kMzveVCgX7i1ZpJWHGnEmNP7LBBL1",
                "coin": "BTC"
            }
        ]
    }

After building models, the data.walletAddress class is generated twice, even though its the exact same model.

Expected Behavior

Since the same model is declared twice, build only the first and reference it in the other.

Steps To Reproduce

No response

Version

3.3.9

Relevant JSON syntax

No response

Anything else?

No response

iamarnas commented 2 years ago

@Prn-Ice Hi 👋 By changing the path to the same object you tell to the generator that you want the same object in the different place. In your case, your jsonc should look like that:

{
        "__className": "create_wallet_address_response",
        "__path": "/lib/app/modules/deposit_withdraw/data/models/wallet_address_response",
        "status": "success",
        "data.walletAddress": {
            "id": "4e252595-d326-4c26-89cc-f23d001ffa4a",
            "account_id": "348e961f-899f-4790-b8e8-712616b74614",
            "address": "1Msn2kMzveVCgX7i1ZpJWHGnEmNP7LBBL1",
            "coin": "BTC"
        }
    },
    {
        "__className": "wallet_addresses_response",
        "__path": "/lib/app/modules/deposit_withdraw/data/models/wallet_address_response",
        "status": "success",
        "data.walletAddress": [
            {
                "id": "4e252595-d326-4c26-89cc-f23d001ffa4a",
                "account_id": "348e961f-899f-4790-b8e8-712616b74614",
                "address": "1Msn2kMzveVCgX7i1ZpJWHGnEmNP7LBBL1",
                "coin": "BTC"
            }
        ]
    }

You are welcome to describe more your opinion if I have not understood you correctly.

iamarnas commented 2 years ago

@Prn-Ice, @Miamoto-Musashi I need your opinion. I see you work a lot with the jsonc file and I need your opinion. What do you think if I move the path to the options object?

The JSON object options should look like this:

"__options": {
    "codeGenerator": "Default", // ['Default', 'JSON', 'Freezed']
    "immutable": false,
    "toString": "Default", // ['Default', 'Auto', 'Stringify', 'Dart']
    "copyWith": false,
    "equality": "Default", // ['Default', 'Equatable', 'Dart']
    "targetDirectory": "/lib/src/models", // <- override path here
    "runBuilder": false,
    "sortConstructorsFirst": false,
    "includeIfNull": false
},

Then you can use it in this way:

{
    "__className": "user",
    "__options": {
        "toString": "Dart", 
        "equality": "Dart", 
        "targetDirectory": "/lib/src/models"
    },
    "name": "Name",
    "id": 1010
}

This setup would allow configuring different output options for every JSON object.

Prn-Ice commented 2 years ago

@iamarnas Looks good.

Prn-Ice commented 2 years ago

@iamarnas thanks, your solution worked, I'll leave the issue open while we wait for @Miamoto-Mushashi to respond.

Miamoto-Musashi commented 2 years ago

Hi @Prn-Ice and @iamarnas, in my experience you can have two patterns:

Best option should be to have a global setting overwritten, just in case, in the jsonc file.

I wouldn't pollute the jsonc file with options, but this is my opinion

iamarnas commented 2 years ago

@Miamoto-Musashi It should be two patterns. First if you not put options to the json object the default options automatically will read from settings. Second you can override by adding options to the object. User can decide how to owerride.

iamarnas commented 2 years ago

@Miamoto-Musashi I think if user have possibility create path to every module his can build clean architecture patterns self.

iamarnas commented 2 years ago

I made another issue that originates from this issue. Since this particular one is finished I will close the issue.

Thank you for your recommendations: @Miamoto-Musashi @Prn-Ice