OpenAPITools / OpenAPITools/openapi-generator

[BUG] [dart-dio] Generated ToJson methods for models throws exception for json_serializable

Open
#20,181 4 comments 4 reactions 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

The generator produces invalid Dart code for nullable Map properties, causing compilation errors in serialization methods (ToJson) when using json_serializable.

openapi-generator version

7.10.0

OpenAPI declaration file content or url
type: object
required:
  - subject
properties:
  subject:
    type: string
  data:
    type: object
    additionalProperties:
      type: string

Steps to reproduce

Generated model ToJson code will look like that, while getting the following compilation error:

Error: Expected ')' before this.
      if (instance.data case final value?) 'data': value,
                        ^^^^
Map<String, dynamic> _$UsersMobileNotificationToJson(
        UsersMobileNotification instance) =>
    <String, dynamic>{
      'subject': instance.subject,
      if (instance.data case final value?) 'data': value,
    };
Suggest a fix

Looks like it is caused by latest update of json_serializable since its version isn't enforced.

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 with the dart-dio generator and reproduce the issue using the OpenAPI declaration in the report with json_serializable enabled. Inspect the generated model's ToJson method and compare it with the reported compilation error. Done means the nullable Map model serializes to valid Dart code and the generated output compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.