OpenAPITools / OpenAPITools/openapi-generator

[BUG] dart-dio: deserialize Enum from {insert type here}. With partial solution(code snippet that can be inserted[needs to fill in filename, enum name, value type])

Open
#18,807 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

Description of the bug

When the return type is a enum the deserializer is skipped. the body in the switch is empty. I implemented a fromValue for me. This shouldn't be that hard to generate. Maybe something like this would be easier.

Solution

everything within {} should be filled in by the code generator.

{filename}.dart

part '{filename}.g.dart';

@JsonEnum(alwaysCreate: true)

...

factory {enum}.fromValue({valueType} value) {
    if (_${enum}EnumMap.containsValue(value)) {
      return _${enum}EnumMap.entries
          .firstWhere((element) => element.value == value)
          .key;
    } else {
      throw ArgumentError('Invalid value for {enum} enum: $value');
    }
  }

deserialize.dart

case '{enum}':
          return {enum}.fromValue(value as  {valueType}) as ReturnType;
Steps to reproduce
  1. route with a enum as return type
  2. openapi-generator generate -i http://127.0.0.1:8080/openapi-docs.yml -g dart-dio -o api --additional-properties=serializationLibrary=json_serializable
Expected behavior

First attempt to fix the deserialize. Does the same as solution above

//
// AUTO-GENERATED FILE, DO NOT MODIFY!
//

// ignore_for_file: unused_element
import 'package:json_annotation/json_annotation.dart';

enum Success {
      @JsonValue(r'True')
      true_(r'True'),
      @JsonValue(r'False')
      false_(r'False');

  const Success(this.value);

  factory Success.fromValue(String value) {
    switch (value) {
      case 'True':
        return Success.true_;
      case 'False':
        return Success.false_;
      default:
        throw ArgumentError('Invalid value for Success enum: $value');
    }
  }

  final String value;
  @override
  String toString() => value;
}

case 'Success':
          return Success.fromValue(value as String) as ReturnType;

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 with the dart-dio generator's deserialize.dart handling and the generated enum example, then reproduce the issue with the provided openapi-generator command and json_serializable option. Done means generated enums provide the requested fromValue behavior and deserialize.dart returns enum values instead of skipping them.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.