Мое определение OpenAPI YAML:
openapi: 3.0.0
info:
title: DBaaS API
description: API for interacting with CNE DBaaS
version: v1
paths:
/test:
post:
description: Create a database instance with the next instance ID
operationId: controller.display
parameters:
- in: query
name: dryrun
description: Perform input validaiton, return instance id without creating
Database.
schema:
type: boolean
requestBody:
content:
application/json:
schema:
x-body-name: params
oneOf:
- $ref: "#/components/schemas/dog"
- $ref: "#/components/schemas/cat"
required: true
responses:
"201":
description: Instance created successfully
content:
application/json:
schema:
title: ID of the database instance
type: string
"400":
description: Bad Request
"401":
description: Unauthorized
"500":
description: Internal Error
servers:
- url: /api/v1
components:
schemas:
dog:
type: object
additionalProperties: false
#default:
#dance: "DANCE"
#sing: "SING"
properties:
dance:
type: string
default: "DANCE"
sing:
type: string
default: "SING"
cat:
type: object
additionalProperties: false
properties:
jump:
type: string
default: "JUMP"
bark:
type: string
default: "BARK"
В моем файле Python установлено принудительное применение по умолчанию:
def extend_with_set_default(validator_class):
'''
Extend swagger validation to set default values
'''
validate_properties = validator_class.VALIDATORS['properties']
def set_defaults(validator, properties, instance, schema):
print("INSTANCE::::::::::::::{}".format(instance))
for property, subschema in six.iteritems(properties): # pylint: disable=redefined-builtin
if 'default' in subschema:
#print("ERROR::::::::::::{}".format(instance))
instance.setdefault(property, subschema['default'])
for error in validate_properties(validator, properties, instance, schema):
yield error
return jsonschema.validators.extend(validator_class, {'properties': set_defaults})
DefaultsEnforcingDraft4Validator = extend_with_set_default(Draft4RequestValidator)
class DefaultsEnforcingRequestBodyValidator(RequestBodyValidator):
'''
Default Enforcing Validator
'''
def __init__(self, *args, **kwargs):
super(DefaultsEnforcingRequestBodyValidator, self).__init__(
*args, validator=DefaultsEnforcingDraft4Validator, **kwargs)
VALIDATOR_MAP = {
'body': DefaultsEnforcingRequestBodyValidator
}
Даже если я даю значения только cat
spe c, он также принимает значения по умолчанию dog
. Кто-нибудь знает, как это ограничить?