Skip to content

partial=True on schema issues non-unique warning #475

@mekanix

Description

@mekanix

The following code gives:

/usr/home/meka/.virtualenvs/rest/lib/python3.6/site-packages/apispec/ext/marshmallow/common.py:145: UserWarning: Multiple schemas resolved to the name Pet. The name has been modified. Either manually add each of the schemas with a different name or provide a custom schema_name_resolver.

If I remove partial=True, the warning goes away. In SwaggerUI, there are two schemas that corespond to PetSchema when partial is usedb.

import marshmallow as ma
from flask import Flask
from flask.views import MethodView
from flask_rest_api import Api, Blueprint, abort


class Pet:
    id = 1
    name = 'Cat'


class PetSchema(ma.Schema):
    id = ma.fields.Int(dump_only=True)
    name = ma.fields.String()

    class Meta:
        strict = True


app = Flask('My API')
app.config['OPENAPI_VERSION'] = '3.0.2'
api = Api(app)

blp = Blueprint('pets', 'pets')


@blp.route('/')
class Pets(MethodView):
    @blp.arguments(PetSchema, location='query')
    @blp.response(PetSchema(many=True))
    def get(self, args):
        """List pets"""
        return [Pet()]

    @blp.arguments(PetSchema)
    @blp.response(PetSchema, code=201)
    def post(self, new_data):
        """Add a new pet"""
        return Pet()


@blp.route('/<pet_id>')
class PetsById(MethodView):
    @blp.response(PetSchema)
    def get(self, pet_id):
        """Get pet by ID"""
        try:
            item = Pet()
        except ItemNotFoundError:
            abort(404, message='Item not found.')
        return item

    @blp.arguments(PetSchema(partial=True))
    @blp.response(PetSchema)
    def put(self, update_data, pet_id):
        """Update existing pet"""
        try:
            item = Pet()
        except ItemNotFoundError:
            abort(404, message='Item not found.')
        return item

    @blp.response(code=204)
    def delete(self, pet_id):
        """Delete pet"""
        pass


api.register_blueprint(blp)

if __name__ == '__main__':
    app.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions