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] [Dart] Object.mapFromJson method not found error in Map<String, Object> attribute deserialization #9272

Open
4 of 6 tasks
fromlabs opened this issue Apr 15, 2021 · 0 comments

Comments

@fromlabs
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generation produces a compiler error because Map<String, Object> attributes are deserialized with Object.mapFromJson method that doesn't exist:

class Post {
  
  Map<String, Object> customData;
  
  static Post fromJson(Map<String, dynamic> json) => json == null
    ? null
    : Post(
        title: json[r'title'],
        customData: json[r'customData'] == null
          ? null
          : Object.mapFromJson(json[r'customData']),
    );
  
}
openapi-generator version

Version 5.1.0

OpenAPI declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: >
    A sample API that uses a petstore as an example
    to demonstrate features in the swagger-2.0 specification
consumes:
  - application/json
produces:
  - application/json
paths:
  /post:
    get:
      responses:
        "200":
          description: pet response
          schema:
            type: array
            items:
              $ref: "#/definitions/Post"
definitions:
  Post:
    type: object
    properties:
      title:
        type: string
      customData:
        type: object
        additionalProperties:
          type: object
Generation Details
java -jar openapi-generator-cli-5.1.0.jar \
    generate -g dart \
    -i post.yml \
    -o post
Steps to reproduce
Related issues/PRs

#7850
#8179

Suggest a fix

I found that adding Object as a primitive type in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java:114 produces the correct behaviour:

languageSpecificPrimitives = Sets.newHashSet(
                "Object", // added as a primitive type
                "String",
                "bool",
                "int",
                "num",
                "double",
                "dynamic"
        );

And generated model:

class Post {
  
  Map<String, Object> customData;
  
  static Post fromJson(Map<String, dynamic> json) => json == null
      ? null
      : Post(
          title: json[r'title'],
          customData: json[r'customData'] == null
              ? null
              : (json[r'customData'] as Map).cast<String, Object>(),
        );
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant