def test_api_extra_spec_plugins(self, app, schemas, openapi_version): """Test extra plugins can be passed to internal APISpec instance""" app.config['OPENAPI_VERSION'] = openapi_version class MyPlugin(apispec.BasePlugin): def schema_helper(self, name, definition, **kwargs): return {'dummy': 'whatever'} api = Api(app, spec_kwargs={'extra_plugins': (MyPlugin(), )}) api.schema('Pet')(schemas.DocSchema) assert get_schemas(api.spec)['Pet']['dummy'] == 'whatever'
def test_api_schema(self, app, schemas): DocSchema = schemas.DocSchema api = Api(app) with mock.patch.object(apispec.core.Components, 'schema') as mock_def: ret = api.schema('Document')(DocSchema) assert ret is DocSchema mock_def.assert_called_once_with('Document', schema=DocSchema)
def test_blueprint_response_schema(self, app, openapi_version, schemas): """Check response schema is correctly documented. More specifically, check that: - plural response is documented as array in the spec - schema is document in the right place w.r.t. OpenAPI version """ app.config['OPENAPI_VERSION'] = openapi_version api = Api(app) blp = Blueprint('test', 'test', url_prefix='/test') api.schema('Doc')(schemas.DocSchema) @blp.route('/schema_many_false') @blp.response(schemas.DocSchema(many=False)) def many_false(): pass @blp.route('/schema_many_true') @blp.response(schemas.DocSchema(many=True)) def many_true(): pass api.register_blueprint(blp) paths = api.spec.to_dict()['paths'] response = paths['/test/schema_many_false']['get']['responses']['200'] if openapi_version == '2.0': schema = response['schema'] assert schema == {'$ref': '#/definitions/Doc'} else: schema = (response['content']['application/json']['schema']) assert schema == {'$ref': '#/components/schemas/Doc'} response = paths['/test/schema_many_true']['get']['responses']['200'] if openapi_version == '2.0': schema = response['schema']['items'] assert schema == {'$ref': '#/definitions/Doc'} else: schema = ( response['content']['application/json']['schema']['items']) assert schema == {'$ref': '#/components/schemas/Doc'}