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

fields2jsonschema warning about Shema Object #95

Closed
khorolets opened this issue Oct 31, 2016 · 3 comments

Comments

@khorolets
Copy link
Contributor

commented Oct 31, 2016

Hi! Can you help me to understand the warning I'm getting.

... swagger.py:450: UserWarning: Only explicitly-declared fields will be included in the Schema Object. Fields defined in Meta.fields or Meta.additional are ignored.
  "Only explicitly-declared fields will be included in the Schema Object. "

My Schema

class BaseUserSchema(ModelSchema):
    """
    Base user schema exposes only the most general fields.
    """

    class Meta:
        # pylint: disable=missing-docstring
        model = User
        fields = (
            User.id.key,
            User.username.key,
            User.first_name.key,
            User.middle_name.key,
            User.last_name.key,
        )
        dump_only = (
            User.id.key,
        )

And here is the sample of code where I'm getting the warning.

from apispec.ext.marshmallow.swagger import schema2jsonschema
pprint(schema2jsonschema(BaseUserSchema))

... swagger.py:450: UserWarning: Only explicitly-declared fields will be included in the Schema Object. Fields defined in Meta.fields or Meta.additional are ignored.
  "Only explicitly-declared fields will be included in the Schema Object. "
{'properties': {'first_name': {'maxLength': 30, 'type': 'string'},
                'id': {'format': 'int32', 'type': 'integer'},
                'last_name': {'maxLength': 30, 'type': 'string'},
                'middle_name': {'maxLength': 30, 'type': 'string'},
                'username': {'maxLength': 80, 'type': 'string'}},
 'required': ['username'],
 'type': 'object'}

The result of function is as expected. But if I understand the warning right my fields declared in Meta.fields should be ignored.

Can you help me to understand what is this warning about?

/cc @frol

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 1, 2016

Ah, you can safely ignore the warning. It is meant to warn that implicitly-created fields (see https://marshmallow.readthedocs.io/en/latest/quickstart.html#refactoring-implicit-field-creation) set in Meta.fields will not be translated into swagger.

ModelSchema works a bit differently; marshmallow fields get generated based on Meta.model by ModelSchema's metaclass upon class declaration. Therefore, swagger fields do get generated.

We'll need to find a better condition for emitting the warning. PRs are always welcome =)

@frol

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2016

@sloria What is the rationale behind the decision of ignoring those implicitly-created fields?

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 1, 2016

@frol When using Meta.fields with vanilla marshmallow, the fields are created implicitly upon invoking dump or load, at which point marshmallow infers which field types to use based upon the objects being dumped. Prior to invoking dump/load, apispec cannot inspect and translate those fields to swagger.

ModelSchemas infer and create fields upon class declaration, so apispec is able to translate those fields.

khorolets added a commit to khorolets/apispec that referenced this issue Nov 2, 2016

Closes marshmallow-code#95: fields2jsonschema warning about Shema Object
Update the condition to show warning about Meta.fields

khorolets added a commit to khorolets/apispec that referenced this issue Nov 3, 2016

Closes marshmallow-code#95: fields2jsonschema warning about Shema Object
Update the condition to show warning about Meta.fields

khorolets added a commit to khorolets/apispec that referenced this issue Nov 13, 2016

Closes marshmallow-code#95: fields2jsonschema warning about Shema Object
Add extra check to show warning about fields ignoring only if there are some of them

khorolets added a commit to khorolets/apispec that referenced this issue Nov 13, 2016

Closes marshmallow-code#95: fields2jsonschema warning about Shema Object
Add extra check to show warning about fields ignoring only if there are some of them

khorolets added a commit to khorolets/apispec that referenced this issue Nov 14, 2016

Closes marshmallow-code#95: fields2jsonschema warning about Shema Object
Add extra check to show warning about fields ignoring only if there are some of them

@sloria sloria closed this in #96 Nov 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.