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] allOf $ref not used as type #721

Open
gem85247 opened this issue Jan 31, 2024 · 0 comments
Open

[BUG] allOf $ref not used as type #721

gem85247 opened this issue Jan 31, 2024 · 0 comments
Assignees
Labels
bug Something isn't working Triage needed

Comments

@gem85247
Copy link

Describe the bug
When generating dart files the generator does not account for the provided $ref type in the allOf when using an array type (swagger snippet below):

...

"MultipleOrganizationResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "nullable": false,
              "allOf": [
                {
                  "$ref": "#\/components\/schemas\/Organization"
                }
              ],
              "description": "entity|class:OrganizationPlatformApiEntity"
            },
            "required": [
              "items"
            ]
          },

...

this results into the following dart code:

@JsonSerializable(explicitToJson: true)
class MultipleOrganizationResponse {
  const MultipleOrganizationResponse({
    this.data,
    this.meta,
  });

  factory MultipleOrganizationResponse.fromJson(Map<String, dynamic> json) =>
      _$MultipleOrganizationResponseFromJson(json);

  static const toJsonFactory = _$MultipleOrganizationResponseToJson;
  Map<String, dynamic> toJson() => _$MultipleOrganizationResponseToJson(this);

  @JsonKey(name: 'data', defaultValue: <Object>[])
  final List<Object>? data;
  @JsonKey(name: 'meta')
  final MultipleOrganizationResponse$Meta? meta;
  static const fromJsonFactory = _$MultipleOrganizationResponseFromJson;

  @override
  String toString() => jsonEncode(this);
}

Notice final List<Object>? data; should in fact be final List<Organization>? data;
My assumption is that something goes wrong because of the type: "array". Because if we look at the swagger snippet below:

...

"SingleOrganizationResponse": {
        "type": "object",
        "properties": {
          "data": {
            "nullable": false,
            "allOf": [
              {
                "$ref": "#\/components\/schemas\/Organization"
              }
            ],
            "description": "entity|class:OrganizationPlatformApiEntity"
          },
          "meta": {
            "type": "object",
            "properties": {
              "count": {
                "nullable": false,
                "type": "integer",
                "example": 123,
                "description": "int"
              }
            },
            "required": [
              "count"
            ]
          }
        },
        "required": [
          "data"
        ]
      },
...

it reproduces a an expected (& correct) dart file:

@JsonSerializable(explicitToJson: true)
class SingleOrganizationResponse {
  const SingleOrganizationResponse({
    required this.data,
    this.meta,
  });

  factory SingleOrganizationResponse.fromJson(Map<String, dynamic> json) =>
      _$SingleOrganizationResponseFromJson(json);

  static const toJsonFactory = _$SingleOrganizationResponseToJson;
  Map<String, dynamic> toJson() => _$SingleOrganizationResponseToJson(this);

  @JsonKey(name: 'data')
  final Organization data;
  @JsonKey(name: 'meta')
  final SingleOrganizationResponse$Meta? meta;
  static const fromJsonFactory = _$SingleOrganizationResponseFromJson;

  @override
  String toString() => jsonEncode(this);
}

As you can see, because the type is not array but object, it generates the correct type: final Organization data;.
So I presume something goes wrong when the type is array.

To Reproduce

using the folowing build.yaml:

targets:
  $default:
    sources:
      - lib/**
      - swagger/**
      - $package$
      - pubspec.yaml
    builders:
      swagger_dart_code_generator:
        options:
          input_folder: "swagger/"
          input_urls:
            - url: "https://api-dev.guestway.io/swagger/swagger-platform.json"
          output_folder: "lib/generated/"
          separate_models: true
          ignore_headers: true
          cut_from_model_names: apiPlatformV1
          override_equals_and_hashcode: false
          use_path_for_request_names: false

it will reproduce the issue explained above.

Expected behavior

final List<Object>? data; should in fact be final List<Organization>? data;

Swagger specification link

link to swagger:
https://api-dev.guestway.io/swagger/swagger-platform.json

link to OpenApi documentation:
https://api-dev.guestway.io/swagger/platform

Library version used:

Using: 2.15.0

Additional context

No additional context needed (I think). Happy to provide more if needed of course :)
Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Triage needed
Projects
None yet
Development

No branches or pull requests

3 participants