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

[BUG] [Dart-dio-next] Enums with numbers get parsed incorrectly

Open glaceon2000 opened this issue 3 years ago • 8 comments

When I declare enums with underscore and number their name comes out incorrectly.

    ExampleEnum:
      title: "exampleEnum"
      description: "Example enum"
      type: "string"
      enum:
        - "EXAMPLE_ENUM_1"
        - "EXAMPLE_ENUM_2"
        - "EXAMPLE_ENUM_NO_NUMBER"

Output:

  /// Example enum
  @BuiltValueEnumConst(wireName: EXAMPLE_ENUM_1')
  static const ExampleEnum eXAMPLEENUM1 = _$eXAMPLEENUM1;
  /// Example enum
  @BuiltValueEnumConst(wireName: EXAMPLE_ENUM_2')
  static const ExampleEnum eXAMPLEENUM1 = _$eXAMPLEENUM2;
  /// Example enum
  @BuiltValueEnumConst(wireName: EXAMPLE_ENUM_NO_NUMBER')
  static const ExampleEnum EXAMPLE_ENUM_NO_NUMBER = _$EXAMPLE_ENUM_NO_NUMBER;

glaceon2000 avatar Jun 17 '22 10:06 glaceon2000

We are seeing invalid code for enum generation i.e. code is syntactically broken. Here is an example we see:

switch (data.toString()) {
        case 0: ...
        case 1: ...
}

No wonder, this code fails to even build due to type errors. Removing toString() from all our enums fixes this issue, but is a PITA for a generated code.

Is there a workaround that we can follow here?

abhinavsingh avatar Jun 24 '22 07:06 abhinavsingh

For reference and posterity, here is a screenshot of invalid code being generated

Screen Shot 2022-06-24 at 9 26 07 PM

abhinavsingh avatar Jun 24 '22 15:06 abhinavsingh

For posterity

find . -name "*_enum.dart" -exec sed -i '' "s/data.toString()/data/g" {} \;

^^^ Currently following workaround is necessary to achieve a working client.

abhinavsingh avatar Jun 27 '22 20:06 abhinavsingh

but is a PITA for a generated code.

What do you mean by this? Removing toString() seems like the only valid solution.

0xNF avatar Jul 07 '22 00:07 0xNF

but is a PITA for a generated code.

What do you mean by this? Removing toString() seems like the only valid solution.

PITA == Pain In The Ass :)

We still have this hack in our Dart generation workflow.

abhinavsingh avatar Jul 07 '22 02:07 abhinavsingh

Yeah I know what it means lol

What I meant to say was, Removing toString from the generator seems correct. What other possible solutions could exist?

0xNF avatar Jul 07 '22 02:07 0xNF

Aah, oopsies :). No worries.

You are right. I don't know why is data.toString() even generated.

I am hoping this goes away so that we can remove the hack from our client generation workflows.

abhinavsingh avatar Jul 07 '22 03:07 abhinavsingh

Hmm... could be a dumb question... but how do you get the wireName: EXAMPLE_ENUM_1 from the enumClass... the exact case-sensitive value EXAMPLE_ENUM_1...

srix55 avatar Oct 12 '22 14:10 srix55

For posterity

find . -name "*_enum.dart" -exec sed -i '' "s/data.toString()/data/g" {} \;

^^^ Currently following workaround is necessary to achieve a working client.

Where should I run this cmd line? 'Cause the generated folder doesn't have any file matching the patter _enum.dart

glaceon2000 avatar Jun 08 '23 05:06 glaceon2000

Hmm... could be a dumb question... but how do you get the wireName: EXAMPLE_ENUM_1 from the enumClass... the exact case-sensitive value EXAMPLE_ENUM_1...

I have no idea. No one helped or fixed this

glaceon2000 avatar Jun 08 '23 17:06 glaceon2000