-
-
Notifications
You must be signed in to change notification settings - Fork 177
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
Marshmallow’s Nested fields with a restricted schema steal the full schema’s registry spot #591
Comments
In most cases you should not rely on
|
As I explained, option 1 isn’t possible, as A custom |
Does it work if you register mini bar schema like this first? spec.components.schema("MiniBarSchema", BarSchema(only=("id", "name"))) |
Also, if you register |
Even if it does, that means figuring out for which schemas I need the mini one beforehand, instead of being able to rely on auto registering: My suggestion still stands:
|
That is |
OK, so how about we
Helpful error messages are always better than having to jump into a debugger and googling some inscrutable message |
Hey there 👋🏻 Context:Just stumbled into this issue as I am encountering the same problem that @flying-sheep described. I think he explained it perfectly in his original message, but just to reiterate: Problem:When users use the One could say: "Ok then, do not register a schema that has already been register, duh". Sadly, there are use cases where schemas are defined within a list, and the short-and-nice registration loop fails... 😞 Example:from apispec.core import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from marshmallow import Schema
from marshmallow import fields
class Children(Schema):
children_id = fields.String(required=True)
class Parent(Schema):
parent_id = fields.String(required=True)
children = fields.Nested(
required=True,
nested=Children,
many=True,
)
# NOTE:
# This structure could be handy when trying to register all project-defined schemas,
# probably to be defined on a folder's __init__.py module, for instance.
all_schemas = [
Parent,
Children,
# ...
]
spec = APISpec(
title="Example",
version="0.1.0",
openapi_version="3.0.2",
plugins=[MarshmallowPlugin()],
)
# Fails...
for schema in all_schemas:
spec.components.schema(schema.__name__, schema=schema) Proposal:I can think of two solutions, either:
|
Just to clarify (given the age of the issue creation): this is still a problem on the current version ( |
@Sinclert To avoid registering nested schemas, you can pass a |
Thanks for the quick reply! I see... that seems to avoid the automatic registration of nested schemas ✅ Not sure how to avoid people stepping into this confusion again though. I reviewed both the Nested schemas documentation, and the @Bangertm could you include what you said on your previous message somewhere in the docs? I would also consider closing this issue so people see that there is actually an official way to solve it. |
The documentation under the Nested Schema documentation you reference includes this sentence:
Can you suggest a way to clarify further? |
Maybe another "❕ NOTE" block 🤷🏻♂️
|
If we e.g. have a marshmallow Schema like this:
and call
spec.components.schema('Foo', schema=FooSchema)
, then “BarSchema” gets registered as having only the fieldsid
andname
, and subsequent tries to register the real thing fail.The problem lies here:
apispec/src/apispec/ext/marshmallow/openapi.py
Line 108 in d418e3e
As that function somehow doesn’t give us something useful. Also it should first modify names for schemas where
schema.only is not None
before it modifies schemas whereschema.only is None
.The text was updated successfully, but these errors were encountered: