def _to_jsonschema(type_):
if isinstance(type_, marshmallow.Schema):
return _jsonschema.dump_schema(type_)
elif type_ in six.integer_types:
return {'type': 'number', 'format': 'integer'}
elif type_ == float:
return {'type': 'number', 'format': 'float'}
elif type_ == decimal.Decimal:
return {'type': 'string', 'format': 'decimal'}
elif type_ == uuid.UUID:
return {'type': 'string', 'format': 'uuid'}
elif type_ == datetime.datetime:
return {'type': 'string', 'format': 'date-time'}
elif type_ == datetime.date:
return {'type': 'string', 'format': 'date'}
elif type_ == datetime.time:
return {'type': 'string', 'format': 'time'}
elif type_ == dict:
return {'type': 'object'}
elif type_ == six.text_type or type_ == six.binary_type:
return {'type': 'string'}
elif type_ is None:
return {'type': 'null'}
elif type_ == list:
return {'type': 'array'}
elif type_ == bool:
return {'type': 'boolean'}
elif issubclass(type_, typing.MutableSequence[typing.T]):
items_type = type_.__parameters__[0]
if issubclass(items_type, marshmallow.Schema):
items_type = items_type()
return {
'type': 'array',
'items': _to_jsonschema(items_type),
}
else:
raise ValueError('unsupported return type: %s' % type_)
评论列表
文章目录