Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
singersbalm opened this issue May 31, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@singersbalm
Copy link

singersbalm commented May 31, 2024

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.

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;

Logs

No response

Screenshots

No response

Platform

Linux

Library version

latest

Flutter version

latest

Flutter channel

stable

Additional context

No response

@singersbalm singersbalm added the bug Something isn't working label May 31, 2024
@singersbalm singersbalm changed the title deserialize Enum from String deserialize Enum from {insert type here}. With partial solution(code snippet that can be inserted[needs to fill in filename, enum name, value type]) May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant