Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What to do about multiple nested schemas warning? #444

Open
revmischa opened this issue May 3, 2019 · 9 comments
Open

What to do about multiple nested schemas warning? #444

revmischa opened this issue May 3, 2019 · 9 comments
Labels

Comments

@revmischa
Copy link

Originally reported here: marshmallow-code/flask-smorest#57 - see there for more input

--

I'm using marshmallow v3-rc5 and using two-way nesting
Using this technique I get the following error if I attempt to use something like @blp.response(CreatorSchema(many=True, exclude=('follower_count',))):

/Users/cyber/.virtualenvs/luve-pim5aQIP/lib/python3.7/site-packages/apispec/ext/marshmallow/common.py:143: 
UserWarning: Multiple schemas resolved to the name Creator. The name has been modified. Either manually add each of the schemas with a different name or provide a custom schema_name_resolver.

and see multiple versions of the schema in swagger (Creator, Creator1).

If I remove the exclude arg to my schemas and make new schemas then everything works perfectly. Something about exclude causes it to think there are multiple versions of the schema and it all explodes.

@lafrech
Copy link
Member

lafrech commented May 3, 2019

As I explained in marshmallow-code/flask-smorest#57, since a field is excluded, these are indeed different schemas, so apispec is doing the right thing by creating separate schemas in the spec.

We added a warning because we figured the user might not be aware of this (your example proves us right) and he would probably want to set explicit names himself (manually or with a custom name resolver) rather than relying on the default name+increment.

Since you seem to think the default automatic naming is fine, there are two things we could do

  • make this a custom warning to make it easier to filter globally (not sure how I'd do that, but with a custom warning, it should be easier)
  • add an opt-in parameter to prevent emitting the warning

@revmischa
Copy link
Author

I'm fine with the behavior I just want to shut it up.

@mekanix
Copy link

mekanix commented Jul 15, 2019

I used marshmallow-code/flask-smorest#38 (comment) as a base for the custom schema resolver, but I don't know how to check if partial is passed or not. I assume the same goes for exclude.

@mdantonio
Copy link

Since you seem to think the default automatic naming is fine, there are two things we could do

  • make this a custom warning to make it easier to filter globally (not sure how I'd do that, but with a custom warning, it should be easier)
  • add an opt-in parameter to prevent emitting the warning

Any decision made about this? In most cases a custom resolver would do exactly the same things as the base resolver except for the warning.. so an option to silent the warning would be appealing

@azzamsa
Copy link

azzamsa commented Jul 20, 2020

any update about this?

@mdantonio
Copy link

For now I have silenced the warning and I forgot about it. Not a very elegant solution, but it seems to work:

import warnings
warnings.filterwarnings(
    "ignore",
    message="Multiple schemas resolved to the name "
)

Probably an official option to silence the warning would be better, but for the moment this very rough approach is enough for me

@azzamsa
Copy link

azzamsa commented Jul 20, 2020

@mdantonio I've tried to put it under my flask create_app functions, unfortunately it doesn't work.

def create_app(config_name):
    app = Flask("FOO")
    app.config.from_object(config_by_name[config_name])
    register_extensions(app)
    register_blueprints(app)
    configure_logger(app)

    warnings.filterwarnings(
        "ignore",
        message="Multiple schemas resolved to the name Gateway. The name has been modified. Either manually add each of the schemas with a different name or provide a custom schema_name_resolver.",
    )
    return app

I also tried to use only some part of the warning message "Multiple schemas resolved to the name ", but doesn't work too

warnings.filterwarnings(
    "ignore",
    message="Multiple schemas resolved to the name "
)

@mdantonio
Copy link

Try to move it before the loading of the endpoints, probably after the blueprints registration the app already raised such warnings

@azzamsa
Copy link

azzamsa commented Jul 20, 2020

Nice idea, worked like a charm. Now I can have clean log. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants