Skip to content

Support None / null as enum values #504

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

Closed
juspence opened this issue Sep 27, 2021 · 1 comment · Fixed by #516
Closed

Support None / null as enum values #504

juspence opened this issue Sep 27, 2021 · 1 comment · Fixed by #516
Labels
🐞bug Something isn't working
Milestone

Comments

@juspence
Copy link
Contributor

juspence commented Sep 27, 2021

Describe the bug
Similar to #357, partially fixed in #500.
Client generation fails if an enum like below is present in the generated OpenAPI schema.

NullEnum:
  enum:
  - null

The context here is that we have a Django model with char / text fields that have null=True, blank=True set. We use Django Rest Framework / drf-spectacular to generate an OpenAPI schema from our models and REST API endpoints. This generated schema automatically includes logic like below:

ModelName:
  type: object
  description: ModelName serializer
  properties:
    field_name:
      nullable: true
      oneOf:
      - $ref: '#/components/schemas/FieldNameEnum'
      - $ref: '#/components/schemas/BlankEnum'
      - $ref: '#/components/schemas/NullEnum'

FieldNameEnum, BlankEnum, and NullEnum are like below:

FieldNameEnum:
  enum:
  - stringvalue1
  - stringvalue2
  type: string
BlankEnum:
  enum:
  - ''
NullEnum:
  enum:
  - null

NullEnum accepts a literal null / None, not a string "null" (it's not a string enum).

To Reproduce
Steps to reproduce the behavior:

  1. Make a new OpenAPI Spec File matching the one below.
  2. Run "openapi-python-client generate --path your_spec_file.yaml"
  3. See an error that null / None is not allowed.

Expected behavior
API client generation succeeds. An enum with None / null as an option is valid based on #500 (comment), so we would like to see this supported in openapi-python-client if easily possible.

In version 0.10.4, we were able to generate the clients successfully by changing the enum_property.py file. If value was None / not a string, it would skip calling remove_string_escapes(value) and just store None directly into output[sanitized_key].

OpenAPI Spec File

openapi: 3.0.3
info:
  title: title
  version: 1.0.0
paths:
  /path/to/something/:
    get:
      responses:
        '200':
          description: ''
components:
  schemas:
    ValueEnum:
      enum:
      - value
    NullEnum:
      enum:
      - null

Desktop (please complete the following information):

OS: Linux 4.18
Python Version: 3.6.8
openapi-python-client version 0.10.5

Additional context
Error(s) encountered while generating, client was not created

Failed to parse OpenAPI document

3 validation errors for OpenAPI
components -> schemas -> NullEnum -> $ref
field required (type=value_error.missing)
components -> schemas -> NullEnum -> enum -> 0
none is not an allowed value (type=type_error.none.not_allowed)
components -> schemas -> NullEnum -> enum -> 0
none is not an allowed value (type=type_error.none.not_allowed)

@juspence juspence added the 🐞bug Something isn't working label Sep 27, 2021
@juspence
Copy link
Contributor Author

WIll be fixed once #512 is merged.

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
2 participants