openapi-spec-validator icon indicating copy to clipboard operation
openapi-spec-validator copied to clipboard

validation(openapi-3.1.0) - nullable enums?

Open kazie opened this issue 1 year ago • 0 comments

I have in an external projects some problems with the openapi-spec-validator.

In our project, we happen to generate an property that either has a value from an enum, or can be null.

See: https://github.com/python-openapi/openapi-spec-validator/compare/master...kazie:openapi-spec-validator:test/test-with-enum-string-or-null-property

I would believe this is valid OpenAPI specification for a field, but the validation fails, as from this project fork when running test:

openapi_spec_validator/shortcuts.py:48: in validate
    return v.validate()
        base_uri   = 'file:///home/kazie/src/github/openapi-spec-validator/tests/integration/data/v3.1/petstore.yaml'
        cls        = <class 'openapi_spec_validator.validation.validators.OpenAPIV31SpecValidator'>
        sp         = SchemaPath('')
        spec       = {'components': {'pathItems': {'PetPath': {'get': {'operationId': 'showPetById', 'parameters': [{...}], 'responses': {'...r'}}, 'summary': 'Create a pet', 'tags': ['pets']}}, '/pets/{petId}': {'$ref': '#/components/pathItems/PetPath'}}, ...}
        v          = <openapi_spec_validator.validation.validators.OpenAPIV31SpecValidator object at 0x782454500440>
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <openapi_spec_validator.validation.validators.OpenAPIV31SpecValidator object at 0x782454500440>

    def validate(self) -> None:
        for err in self.iter_errors():
>           raise err
E           openapi_spec_validator.validation.exceptions.OpenAPIValidationError: None is not one of ['ALIVE', 'DEAD']
E           
E           Failed validating 'enum' in schema:
E               {'default': None,
E                'description': 'The status of the pet. `null` if current status can '
E                               'not be confirmed.',
E                'enum': ['ALIVE', 'DEAD'],
E                'example': 'ALIVE',
E                'type': ['string', 'null']}
E           
E           On instance:
E               None

err        = <OpenAPIValidationError: "None is not one of ['ALIVE', 'DEAD']">
self       = <openapi_spec_validator.validation.validators.OpenAPIV31SpecValidator object at 0x782454500440>

openapi_spec_validator/validation/validators.py:71: OpenAPIValidationError

It is true that None is not in the enum [ ALIVE, DEAD ], but it is in the type array as ['string', 'null']


So, do you believe that my specification is just invalid, or do we have a bug on our hands in the validation here? :sweat_smile:

I came to this when migrating from OpenAPI 3.0.3 where we migrate from having nullable: true into these type arrays.

kazie avatar Nov 19 '24 16:11 kazie