quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

[FEATURE]: On Dart with Freezed, be able to prevent conflicts with nested types on JSON Schema

Open polvallverdu opened this issue 1 year ago • 4 comments

Context (Input, Language)

Input Format: Json Schema Output Language: Dart Freezed

Description

Currently generating Freezed classes for Dart defines nested objects with their literal name, which may cause conflicts if multiple objects are needed. Some stuff is being done if it conflicts with it's own schema, but not for other schemas.

Current Behaviour / Output

bunx quicktype --lang dart --src-lang schema -t Fasfasfsa --just-types --use-freezed
{
  "type": "object",
  "properties": {
    "hello": {
      "type": "string"
    },
    "works": {
      "type": "object",
      "properties": {
        "yes": {
          "type": "boolean"
        },
        "no": {
          "type": "boolean"
        }
      },
      "required": ["yes", "no"],
      "additionalProperties": false
    }
  },
  "required": ["hello", "works"],
  "additionalProperties": false,
  "$schema": "http://json-schema.org/draft-07/schema#"
}
@freezed
class Fasfasfsa with _$Fasfasfsa {
    const factory Fasfasfsa({
        required String hello,
        required Works works,
    }) = _Fasfasfsa;
}

@freezed
class Works with _$Works {
    const factory Works({
        required bool no,
        required bool yes,
    }) = _Works;
}

Proposed Behaviour / Output

bunx quicktype --lang dart --src-lang schema -t Fasfasfsa --just-types --use-freezed
{
  "type": "object",
  "properties": {
    "hello": {
      "type": "string"
    },
    "works": {
      "type": "object",
      "properties": {
        "yes": {
          "type": "boolean"
        },
        "no": {
          "type": "boolean"
        }
      },
      "required": ["yes", "no"],
      "additionalProperties": false
    }
  },
  "required": ["hello", "works"],
  "additionalProperties": false,
  "$schema": "http://json-schema.org/draft-07/schema#"
}
@freezed
class Fasfasfsa with _$Fasfasfsa {
    const factory Fasfasfsa({
        required String hello,
        required Fasfasfsa_Works works,
    }) = _Fasfasfsa;
}

@freezed
class Fasfasfsa_Works with _$Fasfasfsa_Works {
    const factory Fasfasfsa_Works({
        required bool no,
        required bool yes,
    }) = _Fasfasfsa_Works;
}

Solution

Somehow relate nested objects with their parents, to prevent conflicts.

Alternatives

Having the ability to control those names somehow, without needing interactivity. (kinda impossible)

polvallverdu avatar Oct 25 '24 13:10 polvallverdu

I know the issue in your project and I will bring you good solutions if you can reach out me.

DmitryFrontDev avatar Nov 19 '24 18:11 DmitryFrontDev

Hello @polvallverdu

I have fixed dart freezed issue in my local system Can you please help me with do testing

himay123 avatar Nov 19 '24 19:11 himay123

Screenshot from 2024-11-20 01-38-16

check output

himay123 avatar Nov 19 '24 20:11 himay123

Screenshot from 2024-11-20 01-38-16

check output

I don't see what you fixed.

polvallverdu avatar Nov 19 '24 20:11 polvallverdu