gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
112 stars 29 forks source link

enum with number type will generate invalid code #117

Closed alexpyc closed 5 months ago

alexpyc commented 7 months ago

Description of the bug

The type of value is num, however the static const constructors are all with String, i.e. 0 or 1

class OrderStatus {
  /// Instantiate a new enum with the provided [value].
  const OrderStatus._(this.value);

  /// The underlying value of this enum member.
  final num value;

  @override
  String toString() => value.toString();

  num toJson() => value;

  static const n0 = OrderStatus._('0');
  static const n1 = OrderStatus._('1');
  static const n2 = OrderStatus._('2');
  static const n3 = OrderStatus._('3');
  static const n4 = OrderStatus._('4');
  static const n6 = OrderStatus._('6');

  /// List of all possible values in this [enum][OrderStatus].
  static const values = <OrderStatus>[
    n0,
    n1,
    n2,
    n3,
    n4,
    n6,
  ];

Steps to reproduce

Define a type with enum number

Enum with type integer does not have similar issue

Expected behavior

Code generated does not have issue in syntax

Logs

No response

Screenshots

No response

Platform

Linux

Library version

4.13.1

Flutter version

3.10.6

Flutter channel

stable

Additional context

No response

alexpyc commented 5 months ago

The above issue is gone when I was resolving another issue in my work. Related issue: https://github.com/gibahjoe/openapi-generator-dart/issues/35

The solution now works for me is to specify type mapping for double as num:

typeMappings:{'double':'num'}

Not sure how it interacts with the issue I posted here, but it works for me now

gibahjoe commented 5 months ago

Glad to hear its resolved even if by accident. :)