openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] dart-dio generator using json_serialization library with `enumUnknownDefaultCase` option generates codes doesn't handle unknown enums

Open kikuchy opened this issue 1 year ago • 3 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Using the dart-dio generator with the json_serializable library enabled and the enumUnknownDefaultCase option set to true, the code generated does not include the unknownEnumValue parameter for fields with enum types. This issue leads to parsing failures and exceptions when the response contains unknown enum values, which is a significant problem as it prevents the application from handling such cases gracefully.

openapi-generator version

Version 7.4.0 is currently being used. It is unclear whether this is a regression as it has not been tested on previous versions.

OpenAPI declaration file content or url

definitions part only

definitions:
  SomeModel:
    some_field:
      type: string
      enum:
        - a
        - b
Generation Details
docker run --rm -v "${PWD}:/local" "openapitools/openapi-generator-cli:v7.4.0" generate \
-i /local/def.yaml \
--global-property models,apis,supportingFiles,modelDocs=false,modelTests=false \
--additional-properties pubName=my_api \
--additional-properties serializationLibrary=json_serializable \
--additional-properties enumUnknownDefaultCase=true \
-g dart-dio \
-o /local/my_api
Steps to reproduce

To reproduce the bug, simulate a response containing an unknown enum value "c" for the field some_field which is defined under SomeModel in the generated code. Here is how you can simulate this in Dart code:

final m = _$SomeModelFromJson({"some_field": "c"});

When this code is executed, it will throw an exception due to the lack of the unknownEnumValue parameter in the generated code, despite enumUnknownDefaultCase being set to true.

Related issues/PRs

None

Suggest a fix

The issue can be resolved by including the unknownEnumValue parameter in the @JsonKey annotation within the dart-dio generator's template. Specifically, modifications can be made to the template at this location in the repository. Proposed changes include:

  @JsonKey(
    {{#defaultValue}}defaultValue: {{{defaultValue}}},{{/defaultValue}}
    name: r'{{{baseName}}}',
    required: {{#required}}true{{/required}}{{^required}}false{{/required}},
    includeIfNull: {{#required}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/required}}{{^required}}false{{/required}},
    {{#isEnum}}{{#enumUnknownDefaultCase}}unknownEnumValue: {{{datatypeWithEnum}}}.unknownDefaultOpenApi,{{/enumUnknownDefaultCase}}{{/isEnum}}
  )

I can't find the way to refer unknownEnumValue from Mustache template. I'll make PR if I can find it.

(Almost all body texts are generated by Chat GPT, sorry for my poor english skills)

kikuchy avatar Apr 12 '24 11:04 kikuchy

hi @kikuchy Suggested fix is not working as expected due to this issue https://github.com/OpenAPITools/openapi-generator/issues/19415

vasilich6107 avatar Aug 21 '24 18:08 vasilich6107

Probably this could help https://github.com/OpenAPITools/openapi-generator/pull/13880

vasilich6107 avatar Aug 21 '24 18:08 vasilich6107

@kuhnroyal as far as we have isEumOrRef should we revisit dart-dio to check if isEnum => isEnumOrRef migration needed

vasilich6107 avatar Aug 21 '24 18:08 vasilich6107