Skip to content

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

@bw-flagship

Description

@bw-flagship

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions