OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Dart] Object.mapFromJson method not found error in Map<String, Object> attribute deserialization

Open
#9,272 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
https://github.com/OpenAPITools/openapi-generator/issues/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>(),
        );
  
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java around line 114, then reproduce with the provided OpenAPI YAML and generation command. Done means generated Dart deserialization for Map<String, Object> uses the shown map cast rather than calling the nonexistent Object.mapFromJson method.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, java, openapi
Domain
cli, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.