OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Dart-Dio] Uppercase enums can't be serialized

Open
#22,399 0 comments 0 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?
  • 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

When having an enum with uppercase values, serialization does not work.

In this generated piece of code, the name arrives as uppercase ("ValA"), but the code matches to lowercase in the switch statement. Therefore, we always get the ArgumentError.

TestItemProductTypeSettingEnum _$testItemProductTypeSettingEnumValueOf(
    String name) {
  switch (name) {
    case 'valA':
      return _$testItemProductTypeSettingEnum_valA;
    case 'valB':
      return _$testItemProductTypeSettingEnum_valB;
    default:
      throw new ArgumentError(name);
  }
}

When I change switch (name) to switch (name.toLowerCase()), it works again.

openapi-generator version

7.17.0, no regression

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Simple
  version: 0.0.1
paths: {}
components:
  schemas:
    TestItem:
      properties:
        productTypeSetting:
          anyOf:
            - type: string
              enum:
                - ValA
                - ValB
            - type: 'null'
          title: Data Origin
      type: object
      title: TestItem
tags: []
Generation Details

openapi-generator generate -i openapi.json -g dart-dio

Steps to reproduce

Generate from the given config.

Suggest a fix

I'm not sure what the best fix would look like. I saw that there is some logic generated to convert between uppercase and lowercase strings:

  static const Map<String, Object> _toWire = const <String, Object>{
    'valA': 'ValA',
    'valB': 'ValB',
  };
  static const Map<Object, String> _fromWire = const <Object, String>{
    'ValA': 'valA',
    'ValB': 'valB',
  };

But since I do not understand why we don't always use uppercase in this situation, I can not judge what the solution should look like

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 by reproducing the issue with the provided OpenAPI declaration and the openapi-generator generate -i openapi.json -g dart-dio command. Trace the dart-dio enum serialization and deserialization generation, including the shown _toWire, _fromWire, and switch mappings. Done means uppercase enum values serialize and deserialize consistently, with a regression test for ValA and ValB.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.