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

[BUG] List of Enums defined as integer fail the conversion fromJson #746

Open
gabrielginter opened this issue Apr 18, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working Triage needed

Comments

@gabrielginter
Copy link

If an enum is defined as integer values in the swagger json file, and a property in your model is defined as a list of that enum, the method generated to convert an enum list from json fails to find the correct value and returns only a list of swaggerGeneratedUnknown for all values in the list, this is due to the method [enumName]ListFromJson converts the enum value property to String before sending it to [enumName]FromJson. See example below

Swagger json snippet:

"components": {
  "schemas": {
    "SomeEnum": {
      "enum": [
        0,
        1
      ],
      "type": "integer",
      "format": "int32",
      "x-enumNames": [
        "name_enum_one",
        "name_enum_two"
      ],
      "x-enum-varnames": [
        "name_enum_one",
        "name_enum_two"
      ]
    }
  }
}

This will generate the following code:

enum SomeEnum {
  @JsonValue(null)
  swaggerGeneratedUnknown(null),

  @JsonValue(0)
  nameEnumOne(0),
  @JsonValue(1)
  nameEnumTwo(1);

  final int? value;

  const SomeEnum(this.value);
}

enums.SomeEnum someEnumFromJson(
  Object? someEnum, [
  enums.SomeEnum? defaultValue,
]) {
  return enums.SomeEnum.values
          .firstWhereOrNull((e) => e.value == someEnum) ??
      defaultValue ??
      enums.SomeEnum.swaggerGeneratedUnknown;
}

List<enums.SomeEnum> someEnumListFromJson(
  List? someEnum, [
  List<enums.SomeEnum>? defaultValue,
]) {
  if (someEnum == null) {
    return defaultValue ?? [];
  }

  return someEnum.map((e) => someEnumFromJson(e.toString())).toList();
//                                             ^^^^^^^^^^^
//                                       (I believe this is the issue)
}

List<enums.SomeEnum>? someEnumNullableListFromJson(
  List? someEnum, [
  List<enums.SomeEnum>? defaultValue,
]) {
  if (someEnum == null) {
    return defaultValue;
  }

  return someEnum.map((e) => someEnumFromJson(e.toString())).toList();
//                                             ^^^^^^^^^^^
//                                       (I believe this is the issue)
}

Now, if your swagger contains a model with a property defined as List<SomeEnum>, it will generate the code as:

@JsonKey(
    name: 'propertyName',
    toJson: someEnumListToJson,
    fromJson: someEnumListFromJson,
  )
  final List<enums.SomeEnum>? propertyName;

When someEnumListFromJson is called, it converts the values of propertyName to String and send them to someEnumFromJson which fails to find the correct value due the type mismatch.

I'm temporarily manually modifying the generated code.

If I'm missing any setting I'd appreciate any guidance on getting this to work.

@gabrielginter
Copy link
Author

@fryette is this possible to fix? at the moment I'm fixing it manually on every build, not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Triage needed
Projects
None yet
Development

No branches or pull requests

3 participants