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

"nullable: True" ignored if used together with allOf or oneOf #277

Open
idesoto opened this issue Jan 13, 2021 · 5 comments
Open

"nullable: True" ignored if used together with allOf or oneOf #277

idesoto opened this issue Jan 13, 2021 · 5 comments

Comments

@idesoto
Copy link

idesoto commented Jan 13, 2021

I have the following issue. drf-spectacular generates the following spec for enums (for example, for a building_type field which can be either "house" or "apartment" or null):

    WhateverObject:
      type: object
      properties:
        building_type:
          nullable: true
          oneOf:
          - $ref: '#/components/schemas/BuildingTypeEnum'
          - $ref: '#/components/schemas/NullEnum'
 
    BuildingTypeEnum:
      enum:
      - house
      - apartment
      type: string

    NullEnum:
      enum:
      - null

but when trying to validate data containing None for the building_type field I get this error:

Failed validating 'oneOf' in schema['properties']['building_type']:
    {'nullable': True,
     'oneOf': [{'$ref': '#/components/schemas/BuildingTypeEnum',
                'nullable': False,
                'x-scope': ['', '#/components/schemas/HomeProfile']},
               {'$ref': '#/components/schemas/NullEnum',
                'nullable': False,
                'x-scope': ['', '#/components/schemas/HomeProfile']}]}

On instance['building_type']:
    None
None is not valid under any of the given schemas

I think that None should be allowed if nullable: True is specified.

@mmerickel
Copy link

I ran into this with anyOf in my project. Is there a workaround for this?

@idesoto
Copy link
Author

idesoto commented Jan 19, 2021

@mmerickel I haven't found a workaround yet. For now I'm just evaluating whether to use this library so I haven't worked on these issues yet. Are you using drf-spectacular to generate your schema too?

@mmerickel
Copy link

@idesoto I am not, we hand-create the yaml right now.

@aumkar
Copy link

aumkar commented Apr 12, 2021

Seems like its an issue with "openapi_schema_validator" lib.
https://github.com/p1c2u/openapi-schema-validator/blob/master/openapi_schema_validator/validators.py#L69
May be it should check whether nullable property is present with anyOf, oneOf before adding nullable: false

@Wim-De-Clercq
Copy link
Contributor

Wim-De-Clercq commented Apr 28, 2023

It still does not work with this library. (openapi 3.0 validator)
The only workable solution at this moment is

field:
  nullable: true
  oneOf:
    - type: object
      nullable: true
      additionalProperties: false
    - '$ref': '#/components/schemas/Field'

The expected schema is

field:
  nullable: true
  allOf:
  - $ref: '#/components/schemas/Field'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants