def test_collection_format(fmt): """ collectionFormat should be accepted. """ data = { "type": "array", "items": { "type": "string" }, "collectionFormat": fmt } assert Schema(data).to_primitive() == data
def swagger(result_schema): return Schema({ "title": "SuccessObject", "type": "object", "properties": { "result": result_schema, "success": { "type": "boolean" }, "code": { "type": "number" }, }, "required": ["success", "result", "code"], })
def get_swagger_operation(self, context=default_context): """ get the swagger_schema operation representation. """ consumes = produces = context.contenttype_serializers.keys() parameters = get_swagger_parameters(self.parameters, context) responses = { "400": Response({ "description": "invalid input received", "schema": Schema({ "title": "FailureObject", "type": "object", "properties": { "success": { "type": "boolean" }, "result": { "type": "string" } }, "required": ["success", "result"] }) }) } for code, details in self.response_types.items(): responses[str(code)] = Response({ "description": details.get("description"), "schema": context.response_shape.swagger( context.serializers.to_json_schema(details["type"])) }) return Operation({ "summary": self.summary, "description": self.description, "consumes": consumes, "produces": produces, "parameters": parameters, "responses": responses, "operationId": self.raw_func.__name__ })
def schema(): return Schema({"type": "number"})
def test_type_required(): with pytest.raises(Exception): Schema({}).validate()
def test_schema(name, data): assert Schema(data).to_primitive() == data
def test_full_example(): swagger = Swagger({ "info": Info({ "title": "example", "version": "1.0" }), "paths": { "/test": PathItem({ "get": Operation({ "summary": "this is a test", "consumes": ["application/json"], "produces": ["application/json"], "parameters": [ QueryParameter({ "name": "foo", "description": "a foo parameter.", "required": False, "type": "string" }) ], "responses": { "200": Response({ "description": "a list of pets", "schema": Schema({ "type": "object", "properties": { "name": { "type": "string" }, "petType": { "type": "string" } }, "required": ["name", "petType"] }) }) } }), }), "/z": PathItem({ "get": Operation({ "summary": "this is a test", "consumes": ["application/json"], "produces": ["application/json"], "responses": { "200": Response({ "description": "a list of pets", "schema": Schema({ "type": "string", }) }) } }) }) } }) full = { "swagger": "2.0", "info": { "title": "example", "version": "1.0" }, "paths": { "/z": { "get": { "summary": "this is a test", "consumes": ["application/json"], "produces": ["application/json"], "responses": { "200": { "description": "a list of pets", "schema": { "type": "string", } } } } }, "/test": { "get": { "summary": "this is a test", "consumes": ["application/json"], "produces": ["application/json"], "parameters": [ { "in": "query", "name": "foo", "description": "a foo parameter.", "required": False, "type": "string" } ], "responses": { "200": { "description": "a list of pets", "schema": { "type": "object", "properties": { "name": { "type": "string" }, "petType": { "type": "string" } }, "required": ["name", "petType"] } } } } } } } assert swagger.to_primitive() == full result = Swagger(full) output = result.to_primitive() assert output == full # assert the sorted order is correct. assert list(output["paths"].keys()) == ["/test", "/z"]