Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
94 stars 43 forks source link

fix: fixes negative number values in integer enums #70

Closed tsinis closed 1 year ago

tsinis commented 1 year ago

Hey @Carapacik and thanks for a great package!

In this super small patch PR, I'm trying to fix duplicate field names in integer-based enums (provided by Open-API definition JSON). For example, if we have a model defined like this:

"Foo": { "enum": [ -1, 0, 1, 2 ], "type": "number" },

The package will code generate a model like this:

@JsonEnum()
enum Foo {
  @JsonValue(-1)
  value1, // <--- Error, duplicated field.
  @JsonValue(0)
  value0,
  @JsonValue(1)
  value1, // <--- Error, duplicated field.
  @JsonValue(2)
  value2;

  num toJson() => _$FooEnumMap[this]!;
}

And in this PR package will generate a proper model like this:

@JsonEnum()
enum Foo {
  @JsonValue(-1)
  valueMinus1, // <--- Yey! No errors here.
  @JsonValue(0)
  value0,
  @JsonValue(1)
  value1, // <--- And no errors here.
  @JsonValue(2)
  value2;

  num toJson() => _$FooEnumMap[this]!;
}

Because of modifications in the code generation part on the Dart side that will remove "-" from the value name. Thanks one more time!

Carapacik commented 1 year ago

LGTM