Skip to content

Support None / null as enum values #504

Closed

Description

@juspence

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)

Activity

  1. juspence commented on Oct 12, 2021

    @juspence
    ContributorAuthor

    WIll be fixed once #512 is merged.

  2. added this to the 0.10.6 milestone on Oct 17, 2021
  3. added a commit that references this issue on Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions