Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12167
Closed
Open
Issue created Apr 19, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [Dart] Non-string enum decode uses data.toString() without casting value to string

Created by: 0xNF

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

Enums that aren't strings are decoded with a check that includes toString(), but doesn't cast the enum value to a string, resulting in a compile error.

openapi-generator version

6.0.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: "1.1"
  title: Dart IntegerEnum Fail
servers:
  - url: 'localhost'
    variables:
      host:
        default: localhost
components:
  schemas:
    NonStringEnum:
      type: integer
      enum:
      - 1
      - 2
paths:
  /items:
    get:
      operationId: GetItems
      responses:
        "200":
          description: A list of Items
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NonStringEnum'
Generation Details
  NonStringEnum? decode(dynamic data, {bool allowNull = true}) {
    if (data != null) {
      switch (data.toString()) {
        case 1: return NonStringEnum.number1;
        case 2: return NonStringEnum.number2;
        default:
          if (!allowNull) {
            throw ArgumentError('Unknown enum value to decode: $data');
          }
      }
    }
    return null;
  }

Compile error is at case 1: and case 2:. Because the raw values are used, even though the data was cast toString(), the types are mismatched and the compiler wont compile.

Steps to reproduce
  1. java -jar ..\openapi-generator\modules\openapi-generator-cli\target\openapi-generator-cli.jar generate -i .\shadowspec.yaml -g dart -o string_enums

And then checkout string_enums/lib/model/non_string_enum.dart, line 71

Suggest a fix
  1. Ensure that non-string enums are quoted when generating the decode function
Assignee
Assign to
Time tracking