openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

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

Open idesoto opened this issue 5 years ago • 5 comments

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.

idesoto avatar Jan 13 '21 10:01 idesoto

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

mmerickel avatar Jan 18 '21 23:01 mmerickel

@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?

idesoto avatar Jan 19 '21 16:01 idesoto

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

mmerickel avatar Jan 19 '21 16:01 mmerickel

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

aumkar avatar Apr 12 '21 03:04 aumkar

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'

Wim-De-Clercq avatar Apr 28 '23 10:04 Wim-De-Clercq