mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-22 03:00:10 +00:00
Any schema type
This commit is contained in:
parent
4731504f32
commit
05b8a30091
5 changed files with 89 additions and 5 deletions
|
@ -4,6 +4,7 @@ from enum import Enum
|
|||
|
||||
class SchemaType(Enum):
|
||||
|
||||
ANY = None
|
||||
INTEGER = 'integer'
|
||||
NUMBER = 'number'
|
||||
STRING = 'string'
|
||||
|
|
|
@ -16,7 +16,7 @@ class SchemaFactory(object):
|
|||
def create(self, schema_spec):
|
||||
schema_deref = self.dereferencer.dereference(schema_spec)
|
||||
|
||||
schema_type = schema_deref.get('type', 'object')
|
||||
schema_type = schema_deref.get('type', None)
|
||||
schema_format = schema_deref.get('format')
|
||||
model = schema_deref.get('x-model', None)
|
||||
required = schema_deref.get('required', False)
|
||||
|
|
|
@ -26,6 +26,7 @@ class Schema(object):
|
|||
"""Represents an OpenAPI Schema."""
|
||||
|
||||
DEFAULT_CAST_CALLABLE_GETTER = {
|
||||
SchemaType.ANY: lambda x: x,
|
||||
SchemaType.INTEGER: int,
|
||||
SchemaType.NUMBER: float,
|
||||
SchemaType.BOOLEAN: forcebool,
|
||||
|
@ -61,7 +62,7 @@ class Schema(object):
|
|||
schema_format=None, required=None, default=None, nullable=False,
|
||||
enum=None, deprecated=False, all_of=None, one_of=None,
|
||||
additional_properties=None):
|
||||
self.type = schema_type and SchemaType(schema_type)
|
||||
self.type = SchemaType(schema_type)
|
||||
self.model = model
|
||||
self.properties = properties and dict(properties) or {}
|
||||
self.items = items
|
||||
|
@ -137,9 +138,6 @@ class Schema(object):
|
|||
raise InvalidSchemaValue("Null value for non-nullable schema")
|
||||
return self.default
|
||||
|
||||
if self.type is None:
|
||||
return value
|
||||
|
||||
cast_mapping = self.get_cast_mapping()
|
||||
|
||||
if self.type is not SchemaType.STRING and value == '':
|
||||
|
|
|
@ -168,6 +168,12 @@ paths:
|
|||
$ref: "#/components/responses/ErrorResponse"
|
||||
components:
|
||||
schemas:
|
||||
Utctime:
|
||||
oneOf:
|
||||
- type: string
|
||||
enum: [always, now]
|
||||
- type: string
|
||||
format: date-time
|
||||
Address:
|
||||
type: object
|
||||
x-model: Address
|
||||
|
@ -202,6 +208,7 @@ components:
|
|||
type: integer
|
||||
format: int64
|
||||
PetCreate:
|
||||
type: object
|
||||
x-model: PetCreate
|
||||
allOf:
|
||||
- $ref: "#/components/schemas/PetCreatePartOne"
|
||||
|
@ -284,6 +291,8 @@ components:
|
|||
required:
|
||||
- name
|
||||
properties:
|
||||
created:
|
||||
$ref: "#/components/schemas/Utctime"
|
||||
name:
|
||||
type: string
|
||||
TagList:
|
||||
|
|
|
@ -1032,3 +1032,79 @@ class TestPetstore(object):
|
|||
assert response_result.data.message == message
|
||||
assert response_result.data.rootCause == rootCause
|
||||
assert response_result.data.additionalinfo == additionalinfo
|
||||
|
||||
def test_post_tags_created_now(
|
||||
self, spec, response_validator):
|
||||
host_url = 'http://petstore.swagger.io/v1'
|
||||
path_pattern = '/v1/tags'
|
||||
pet_name = 'Dog'
|
||||
data_json = {
|
||||
'created': 'now',
|
||||
'name': pet_name,
|
||||
}
|
||||
data = json.dumps(data_json)
|
||||
|
||||
request = MockRequest(
|
||||
host_url, 'POST', '/tags',
|
||||
path_pattern=path_pattern, data=data,
|
||||
)
|
||||
|
||||
parameters = request.get_parameters(spec)
|
||||
body = request.get_body(spec)
|
||||
|
||||
assert parameters == {}
|
||||
assert body == data_json
|
||||
|
||||
data_json = {
|
||||
'code': 400,
|
||||
'message': 'Bad request',
|
||||
'rootCause': 'Tag already exist',
|
||||
'additionalinfo': 'Tag Dog already exist',
|
||||
}
|
||||
data = json.dumps(data_json)
|
||||
response = MockResponse(data, status_code=404)
|
||||
|
||||
response_result = response_validator.validate(request, response)
|
||||
|
||||
assert response_result.errors == []
|
||||
assert response_result.data == data_json
|
||||
|
||||
def test_post_tags_created_datetime(
|
||||
self, spec, response_validator):
|
||||
host_url = 'http://petstore.swagger.io/v1'
|
||||
path_pattern = '/v1/tags'
|
||||
pet_name = 'Dog'
|
||||
data_json = {
|
||||
'created': '2016-04-16T16:06:05Z',
|
||||
'name': pet_name,
|
||||
}
|
||||
data = json.dumps(data_json)
|
||||
|
||||
request = MockRequest(
|
||||
host_url, 'POST', '/tags',
|
||||
path_pattern=path_pattern, data=data,
|
||||
)
|
||||
|
||||
parameters = request.get_parameters(spec)
|
||||
body = request.get_body(spec)
|
||||
|
||||
assert parameters == {}
|
||||
assert body == data_json
|
||||
|
||||
data_json = {
|
||||
'code': 400,
|
||||
'message': 'Bad request',
|
||||
'rootCause': 'Tag already exist',
|
||||
'additionalinfo': 'Tag Dog already exist',
|
||||
}
|
||||
data = json.dumps(data_json)
|
||||
response = MockResponse(data, status_code=404)
|
||||
|
||||
response_result = response_validator.validate(request, response)
|
||||
|
||||
assert response_result.errors == []
|
||||
assert isinstance(response_result.data, BaseModel)
|
||||
assert response_result.data.code == code
|
||||
assert response_result.data.message == message
|
||||
assert response_result.data.rootCause == rootCause
|
||||
assert response_result.data.additionalinfo == additionalinfo
|
||||
|
|
Loading…
Reference in a new issue