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

Allow enums with nullable defaults #1571

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions connexion/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from jsonschema.validators import extend as extend_validator

from .exceptions import InvalidSpecification
from .json_schema import NullableTypeValidator, resolve_refs
from .json_schema import NullableEnumValidator, NullableTypeValidator, resolve_refs
from .operations import OpenAPIOperation, Swagger2Operation
from .utils import deep_get

Expand All @@ -32,7 +32,11 @@ def create_spec_validator(spec: dict) -> Draft4Validator:
# Create an instance validator, which validates defaults against the spec itself instead of
# against the OpenAPI schema.
InstanceValidator = extend_validator(
Draft4Validator, {"type": NullableTypeValidator}
Draft4Validator,
{
"type": NullableTypeValidator,
"enum": NullableEnumValidator,
},
)
instance_validator = InstanceValidator(spec)

Expand Down
4 changes: 4 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,5 +645,9 @@ def nullable_default(test):
return


def nullable_enum(test):
return


def get_streaming_response():
return send_file(__file__)
15 changes: 15 additions & 0 deletions tests/fixtures/json_validation/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ paths:
204:
description: OK

/nullable_enum:
get:
operationId: fakeapi.hello.nullable_enum
parameters:
- name: test
in: query
schema:
type: string
enum: ["good", "bad"]
nullable: true
default: null
responses:
204:
description: OK

/multipart_form_json:
post:
operationId: fakeapi.hello.post_multipart_form
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/json_validation/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ paths:
responses:
204:
description: OK

/nullable_enum:
get:
operationId: fakeapi.hello.nullable_enum
parameters:
- name: test
in: query
type: string
x-nullable: true
default: null
responses:
204:
description: OK