gibahjoe / openapi-generator-dart

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

Enum generated as string without vailidaing allowed options #8

Closed agilob closed 4 years ago

agilob commented 4 years ago

I'm either incorrectly using it, or the package doesn't generate enums correctly.

I have this component:


components:
  schemas:
    Sorting:
      type: object
      description: sorting order
      properties:
        sortingOrder:
          type: string
          enum: [asc, desc]

This is generated as:

part of openapi.api;

class Sorting {

  String sortingOrder = null;
  //enum sortingOrderEnum {  asc,  desc,  };{
  Sorting();

  @override
  String toString() {
    return 'Sorting[sortingOrder=$sortingOrder, ]';
  }

  Sorting.fromJson(Map<String, dynamic> json) {
    if (json == null) return;
    sortingOrder = json['sortingOrder'];
  }

  Map<String, dynamic> toJson() {
    Map <String, dynamic> json = {};
    if (sortingOrder != null)
      json['sortingOrder'] = sortingOrder;
    return json;
  }

  static List<Sorting> listFromJson(List<dynamic> json) {
    return json == null ? List<Sorting>() : json.map((value) => Sorting.fromJson(value)).toList();
  }

  static Map<String, Sorting> mapFromJson(Map<String, dynamic> json) {
    var map = Map<String, Sorting>();
    if (json != null && json.isNotEmpty) {
      json.forEach((String key, dynamic value) => map[key] = Sorting.fromJson(value));
    }
    return map;
  }

  // maps a json object with a list of Sorting-objects as value to a dart map
  static Map<String, List<Sorting>> mapListFromJson(Map<String, dynamic> json) {
    var map = Map<String, List<Sorting>>();
     if (json != null && json.isNotEmpty) {
       json.forEach((String key, dynamic value) {
         map[key] = Sorting.listFromJson(value);
       });
     }
     return map;
  }
}

where enum exists, but it's not used to validate if passed string is allowed.

Do I need to use:

useEnumExtension: true
supportDart2: true

If so, where do I set them? An example in documentation would be helpful.

agilob commented 4 years ago

Nvm, I thought this is repo for https://openapi-generator.tech/docs/generators/dart/ but pubdev package :D

gibahjoe commented 4 years ago

No worries.