"nullable: True" ignored if used together with allOf or oneOf
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.
I ran into this with anyOf in my project. Is there a workaround for this?
@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 I am not, we hand-create the yaml right now.
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
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'