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

validation(openapi-3.1.0) - nullable enums? #378

Open
kazie opened this issue Nov 19, 2024 · 0 comments
Open

validation(openapi-3.1.0) - nullable enums? #378

kazie opened this issue Nov 19, 2024 · 0 comments

Comments

@kazie
Copy link

kazie commented Nov 19, 2024

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: 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? 😅

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

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

1 participant