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] additionalProperties key from schema is not handled correctly #295

Closed
dJani97 opened this issue Jan 3, 2022 · 2 comments · Fixed by #315
Closed

[BUG] additionalProperties key from schema is not handled correctly #295

dJani97 opened this issue Jan 3, 2022 · 2 comments · Fixed by #315
Assignees
Labels
enhancement New feature or request

Comments

@dJani97
Copy link

dJani97 commented Jan 3, 2022

Describe the bug
Using "additionalProperties" in a definition causes the generated field to be of type Object.

To Reproduce
The swagger json can be found in this example on swaggerhub.

Running swagger-dart-code-generator on this json results in the ExampleDTO having a generic Object? examples field instead of Map<String, List<EmbeddedExampleDTO>> examples described in the swagger file.

Here is a snippet from the generated Dart class:

@JsonSerializable(explicitToJson: true)
class ExampleDTO {
  ExampleDTO({
    this.examples,
  });

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

  @JsonKey(name: 'examples')
  final Object? examples;

  // (...)
}

Expected behavior
The example field should be generated as Map<String, List<EmbeddedExampleDTO>> examples.

For example, the Export > Client SDK > Dart function on SwaggerHub seems to generate a proper implementation:

class ExampleDTO {
  Map<String, List<EmbeddedExampleDTO>> examples = {};
  
  // (...)
}

Library version used:
swagger_dart_code_generator: ^2.3.2

Additional context
I noticed that this problem was already referenced in #58 which was closed with the message "fixed all issues in 1.2.0 version".

@fryette
Copy link
Contributor

fryette commented Jan 6, 2022

I think that feature can be quite easily implemented.

We will add it to our roadmap and will try to deliver it this month.

Documentation: https://swagger.io/docs/specification/data-models/dictionaries/

@fryette fryette added enhancement New feature or request and removed bug Something isn't working Triage needed labels Jan 6, 2022
@fryette
Copy link
Contributor

fryette commented Jan 10, 2022

So, we double-checked this issue and seems Expected result is not achievable.
If a property has additional properties it means that it's a dictionary.
For example for that case(see below), we see 2 properties, "default": "string", and a list of Models and because it's the dictionary we cannot generate a regular model or Map<String, List<EmbeddedExampleDTO>>. It should be simple Map<String, dynamic> in my understanding.

  ExampleDTO:
    type: object
    required:
      - examples
    properties:
      examples:
        type: object
        properties:
          default:
            type: string
        additionalProperties:
          type: array
          items:
            $ref: '#/definitions/EmbeddedExampleDTO'

We are going to change generated field from Object to Map<String, dynamic> and let users parse it as they want.

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

Successfully merging a pull request may close this issue.

3 participants