Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null in enum not handled correctly #63

Open
dickermoshe opened this issue Mar 12, 2024 · 4 comments
Open

Null in enum not handled correctly #63

dickermoshe opened this issue Mar 12, 2024 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dickermoshe
Copy link
Contributor

Details

components:
  schemas:
    AnyOfTest:
      anyOf:
      - type: string
      - $ref: '#/components/schemas/EnumString'
      - $ref: '#/components/schemas/EnumNullString'
      - $ref: '#/components/schemas/EnumNull'
      description: to test anyOf (string, enum string)
    EnumNull:
      enum:
      - null
      type: string
    EnumNullString:
      enum:
      - 'null'
      type: string
    EnumString:
      enum:
      - A
      - B
      type: string
info:
  license:
    name: MIT
  title: Example
  version: 1.0.0
openapi: 3.0.1
paths:
  /person/display/{personId}:
    get:
      operationId: list
      parameters:
      - description: The id of the person to retrieve
        in: path
        name: personId
        required: true
        schema:
          type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnyOfTest'
          description: OK
servers:
- url: http://api.example.xyz/v1

@walsha2
Copy link
Contributor

walsha2 commented Mar 12, 2024

EnumNull:
  enum:
  - null
  type: string

I am not sure I follow, what does this even represent as a Dart class? How is this a proper enum definition?

I am not sure this edge case is even worth spending time coding up and protecting for... Unless you have a real world example that is not an edge case test scheme from OpenAPI repo?

@dickermoshe
Copy link
Contributor Author

It's more common than you think.
The example I posted is stupid, but this update in 3.1 added full support with the json schema which does

EnumNull:
  enum:
  - null
  - ice_cream
  - yogurt
  type: string

It used to be

EnumNull:
  enum:
  - ice_cream
  - yogurt
  type: string
  nullable: true

OAI/OpenAPI-Specification#1900 (comment)

@dickermoshe
Copy link
Contributor Author

Proposal:
swagger-parser add an unknown value that makes the api backwards compatible.
So even if this isn't fixed, it will still wont throw when null is returned
what do you think?

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, unused_import

import 'package:freezed_annotation/freezed_annotation.dart';

@JsonEnum()
enum Status {
  @JsonValue('available')
  available('available'),
  @JsonValue('pending')
  pending('pending'),
  @JsonValue('sold')
  sold('sold'),
  /// Default value for all unparsed values, allows backward compatibility when adding new values on the backend.
  $unknown(null);

  const Status(this.json);

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

  final String? json;
}

@walsha2
Copy link
Contributor

walsha2 commented Mar 12, 2024

Ok so it just means that the enum itself is nullable, not that the individual enum value is null. I dont really like that spec definition.

There is a "simple" fix for this, I believe. I can take care of it.

@walsha2 walsha2 self-assigned this Mar 12, 2024
@walsha2 walsha2 added the bug Something isn't working label Mar 12, 2024
@walsha2 walsha2 added this to the 0.8.0 milestone Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants