Exemplo n.º 1
0
    def test_api_extra_spec_plugins(self, app, schemas):
        """Test extra plugins can be passed to internal APISpec instance"""

        class MyPlugin(apispec.BasePlugin):
            def definition_helper(self, name, definition, **kwargs):
                return {'dummy': 'whatever'}

        api = Api(app, spec_plugins=(MyPlugin(), ))
        api.definition('Pet')(schemas.DocSchema)
        spec = api.spec.to_dict()
        assert spec['definitions']['Pet']['dummy'] == 'whatever'
Exemplo n.º 2
0
 def test_api_definition(self, app, schemas):
     DocSchema = schemas.DocSchema
     api = Api(app)
     if APISPEC_VERSION_MAJOR < 1:
         with mock.patch.object(apispec.APISpec, 'definition') as mock_def:
             ret = api.definition('Document')(DocSchema)
     else:
         with mock.patch.object(
                 apispec.core.Components,
                 'schema'
         ) as mock_def:
             ret = api.definition('Document')(DocSchema)
     assert ret is DocSchema
     mock_def.assert_called_once_with('Document', schema=DocSchema)
Exemplo n.º 3
0
 def test_api_definition(self, app, schemas):
     DocSchema = schemas.DocSchema
     api = Api(app)
     with mock.patch.object(apispec.APISpec, 'definition') as mock_def:
         ret = api.definition('Document')(DocSchema)
     assert ret is DocSchema
     mock_def.assert_called_once_with('Document', schema=DocSchema)
Exemplo n.º 4
0
 def test_api_definition(self, app, schemas):
     """Compatibility: definition is an alias for schema"""
     DocSchema = schemas.DocSchema
     api = Api(app)
     with mock.patch.object(apispec.core.Components, 'schema') as mock_def:
         ret = api.definition('Document')(DocSchema)
     assert ret is DocSchema
     mock_def.assert_called_once_with('Document', schema=DocSchema)
Exemplo n.º 5
0
    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.definition('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'}