def test_build_integer_valued_param(): graph = create_object_graph(name="example", testing=True) ns = Namespace( subject=Person, version="v1", identifier_type="int", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) assert_that( build_path_for_integer_param(ns, Operation.Update, set(["person_id"])), equal_to("/api/v1/person/{person_id}"), ) assert_that( swagger_schema, has_entries(paths=has_entries( **{ "/person/{person_id}": has_entries(patch=has_entries(parameters=has_items({ "required": True, "type": "integer", "name": "person_id", "in": "path", }), ), ), }, ), ))
def discover(): request_data = load_query_string_data(ValidateSwaggerSchema()) swagger = build_swagger(self.graph, ns, self.find_matching_endpoints(ns), **request_data) g.hide_body = True return make_response(swagger)
def test_no_prefix_no_version_path(): loader = load_from_dict( dict( # We want our routes to come directly after the root / build_route_path=dict(prefix=""), )) graph = create_object_graph(name="example", testing=True, loader=loader) ns = Namespace( subject=Person, version="", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) # Test that in a no prefix, no version case we still get a leading slash in our paths assert_that("/person" in swagger_schema["paths"]) assert_that(swagger_schema["basePath"], equal_to("/"))
def test_no_prefix_no_version_path(): loader = load_from_dict(dict( # We want our routes to come directly after the root / build_route_path=dict( prefix="" ), )) graph = create_object_graph(name="example", testing=True, loader=loader) ns = Namespace( subject=Person, version="", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) # Test that in a no prefix, no version case we still get a leading slash in our paths assert_that("/person" in swagger_schema["paths"]) assert_that(swagger_schema["basePath"], equal_to("/"))
def discover(): swagger = build_swagger(self.graph, ns, self.find_matching_endpoints(ns)) g.hide_body = True return make_response(swagger)
def test_build_swagger(): graph = create_object_graph(name="example", testing=True) ns = Namespace( subject=Person, version="v1", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) assert_that(swagger_schema, is_(equal_to({ "info": { "version": "v1", "title": "example", }, "paths": { "/person": { "post": { "tags": ["person"], "responses": { "default": { "description": "An error occurred", "schema": { "$ref": "#/definitions/Error", } }, "201": { "description": "Create a new person", "schema": { "$ref": "#/definitions/Person", }, }, }, "parameters": [ { "in": "header", "name": "X-Response-Skip-Null", "required": False, "type": "string", }, { "in": "body", "name": "body", "schema": { "$ref": "#/definitions/NewPerson", }, }, ], "operationId": "create", }, }, "/person/{person_id}": { "patch": { "tags": ["person"], "responses": { "default": { "description": "An error occurred", "schema": { "$ref": "#/definitions/Error", }, }, "200": { "description": "Update some or all of a person by id", "schema": { "$ref": "#/definitions/Person", }, }, }, "parameters": [ { "required": False, "type": "string", "name": "X-Response-Skip-Null", "in": "header", }, { "in": "body", "name": "body", "schema": { "$ref": "#/definitions/UpdatePerson", }, }, { "required": True, "type": "string", "name": "person_id", "in": "path", "format": "uuid", }, ], "operationId": "update", }, }, }, "produces": [ "application/json", ], "definitions": { "NewPerson": { "required": [ "firstName", "lastName", ], "type": "object", "properties": { "lastName": { "type": "string", }, "firstName": { "type": "string", } } }, "Person": { "required": [ "firstName", "id", "lastName", ], "type": "object", "properties": { "lastName": { "type": "string", }, "_links": { "type": "object" }, "id": { "type": "string", "format": "uuid", }, "firstName": { "type": "string", }, }, }, "UpdatePerson": { "type": "object", "properties": { "lastName": { "type": "string", }, "firstName": { "type": "string", } } }, "ErrorContext": { "required": ["errors"], "type": "object", "properties": { "errors": { "items": { "$ref": "#/definitions/SubError", }, "type": "array", }, }, }, "SubError": { "required": ["message"], "type": "object", "properties": { "message": { "type": "string", }, }, }, "Error": { "required": [ "code", "message", "retryable", ], "type": "object", "properties": { "message": { "type": "string", "default": "Unknown Error", }, "code": { "type": "integer", "format": "int32", "default": 500, }, "context": { "$ref": "#/definitions/ErrorContext", }, "retryable": { "type": "boolean", }, }, }, }, "basePath": "/api/v1", "swagger": "2.0", "consumes": [ "application/json", ], })))
def test_build_swagger(): graph = create_object_graph(name="example", testing=True) ns = Namespace( subject=Person, version="v1", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) assert_that( swagger_schema, has_entries( info={ "version": "v1", "title": "example", }, paths={ "/person": { "post": { "tags": ["person"], "responses": { "default": { "description": "An error occurred", "schema": { "$ref": "#/definitions/Error", } }, "201": { "description": "Create a new person", "schema": { "$ref": "#/definitions/Person", }, }, }, "parameters": [ { "in": "header", "name": "X-Response-Skip-Null", "required": False, "type": "string", }, { "in": "body", "name": "body", "schema": { "$ref": "#/definitions/NewPerson", }, }, ], "operationId": "create", }, }, "/person/{person_id}": { "patch": { "tags": ["person"], "responses": { "default": { "description": "An error occurred", "schema": { "$ref": "#/definitions/Error", }, }, "200": { "description": "Update some or all of a person by id", "schema": { "$ref": "#/definitions/Person", }, }, }, "parameters": [ { "required": False, "type": "string", "name": "X-Response-Skip-Null", "in": "header", }, { "in": "body", "name": "body", "schema": { "$ref": "#/definitions/UpdatePerson", }, }, { "required": True, "type": "string", "name": "person_id", "in": "path", "format": "uuid", }, ], "operationId": "update", }, }, }, produces=[ "application/json", ], definitions=has_entries( NewPerson=has_entries(required=[ "firstName", "lastName", ], type="object", properties={ "email": { "type": "string", "format": "email", }, "eyeColor": { "enum": [ "PURPLE", "TEAL", "RUBY", ], "format": "enum", "type": "string", }, "lastName": { "type": "string", }, "firstName": { "type": "string", } }), Person=has_entries( required=[ "firstName", "id", "lastName", ], type="object", properties={ "email": { "type": "string", "format": "email", }, # Response-side enums are declared as strings "eyeColor": { "type": "string", }, "lastName": { "type": "string", }, "_links": { "type": "object" }, "id": { "type": "string", "format": "uuid", }, "firstName": { "type": "string", }, }, ), PersonPubsubMessage=has_entries( type="object", properties={ "email": { "format": "email", "type": "string", }, "firstName": { "type": "string", }, }, required=[ "firstName", ], ), PersonFoo=has_entries( type="object", properties={ "email": { "format": "email", "type": "string", }, "firstName": { "type": "string", }, }, required=[ "email", ], ), UpdatePerson=dict(type="object", properties={ "lastName": { "type": "string", }, "firstName": { "type": "string", } }), ErrorContext=has_entries( required=["errors"], type="object", properties={ "errors": { "items": { "$ref": "#/definitions/SubError", }, "type": "array", }, }, ), SubError=has_entries( required=["message"], type="object", properties={ "message": { "type": "string", }, }, ), Error=has_entries( required=[ "code", "message", "retryable", ], type="object", properties={ "message": { "type": "string", "default": "Unknown Error", }, "code": { "type": "integer", "format": "int32", "default": 500, }, "context": { "$ref": "#/definitions/ErrorContext", }, "retryable": { "type": "boolean", }, }, ), ), basePath="/api/v1", swagger="2.0", consumes=[ "application/json", ], ))