[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
- 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
- Ensure that non-string enums are quoted when generating the decode function