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
88 stars 33 forks source link

Enums failing #197

Closed dickermoshe closed 4 months ago

dickermoshe commented 4 months ago
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, unused_import

import 'package:freezed_annotation/freezed_annotation.dart';

/// Days of week
@JsonEnum()
enum DaysOfWeek {
  @JsonValue(MONDAY) //<MONDAY isn't declared anywhere
  monday(MONDAY),
  @JsonValue(TUESDAY)
  tuesday(TUESDAY),
  @JsonValue(WEDNESDAY)
  wednesday(WEDNESDAY),
  @JsonValue(THURSDAY)
  thursday(THURSDAY),
  @JsonValue(FRIDAY)
  friday(FRIDAY),
  @JsonValue(SATURDAY)
  saturday(SATURDAY),
  @JsonValue(SUNDAY)
  sunday(SUNDAY),
  @JsonValue(WEEKDAYS)
  weekdays(WEEKDAYS),
  @JsonValue(WEEKEND)
  weekend(WEEKEND),
  @JsonValue(EVERYDAY)
  everyday(EVERYDAY),
  /// Default value for all unparsed values, allows backward compatibility when adding new values on the backend.
  $unknown(null);

  const DaysOfWeek(this.json);

  factory DaysOfWeek.fromJson(Object json) => values.firstWhere(
        (e) => e.json == json,
        orElse: () => $unknown,
      );

  final Object? json;
}

Schema

openapi: 3.0.0
info:
  title: 'Issue 10591 Enum default value'
  version: latest
paths:
  '/':
    get:
      operationId: operation
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ModelWithEnumPropertyHavingDefault'
components:
  schemas:
    ModelWithEnumPropertyHavingDefault:
      required:
        - propertyName
      properties:
        propertyName:
          type: string
          default: VALUE
          enum:
            - VALUE
    PropertyOfDay:
      required:
      - name
      type: object
      properties:
        name:
          type: string
          description: Name of property of day
          example: Monday
        description:
          type: string
          description: Description of the property of day
          example: Days family discount is available
        daysOfWeek:
          type: object
          description: Days of week
          example:
          - MONDAY
          - TUESDAY
          enum:
          - MONDAY
          - TUESDAY
          - WEDNESDAY
          - THURSDAY
          - FRIDAY
          - SATURDAY
          - SUNDAY
          - WEEKDAYS
          - WEEKEND
          - EVERYDAY
        monthOfYear:
          type: integer
          description: Month of year
          format: int32
          enum:
          - 1
          - 2
          - 3
          - 4
          - 5
          - 6
          - 7
          - 8
          - 9
          - 10
          - 11
          - 12
        dayOfYear:
          type: integer
          description: Day of year
          format: int32
        holidayTypes:
          type: object
          description: Holiday types
          example:
          - NOT_HOLIDAY
          - LOCAL_HOLIDAY
          enum:
          - NOT_HOLIDAY
          - LOCAL_HOLIDAY
          - NATIONAL_HOLIDAY
          - ANY_HOLIDAY
          - WORKING_DAY
          - ANY_DAY
          - NEW_YEARS_DAY
          - PALM_SUNDAY
          - MAUNDY_THURSDAY
          - GOOD_FRIDAY
          - EASTER_SUNDAY
          - EASTER_MONDAY
          - LABOUR_DAY
          - CONSTITUTION_DAY
          - ASCENSION_DAY
          - WHIT_SUNDAY
          - WHIT_MONDAY
          - CHRISTMAS_DAY
          - BOXING_DAY
      description: The DayType describe during which the assignment applies
dickermoshe commented 4 months ago

This is caused by a bad schema. Although this is "technically" valid. it declares a string as an object.

maybe we should warn users that they should validate their schema on https://editor.swagger.io/ before posting issues