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

Fix extra fields behavior for TypedDict and make additionalProperties explicit in JSON schema #6115

Merged
merged 3 commits into from Jun 13, 2023

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Jun 13, 2023

Closes #6082

Selected Reviewer: @Kludex

@adriangb adriangb changed the title Fix extra fields behavior for TypedDict and make additionalProperties… Fix extra fields behavior for TypedDict and make additionalProperties explicit in JSON schema Jun 13, 2023
@cloudflare-pages
Copy link

cloudflare-pages bot commented Jun 13, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: efd91df
Status: ✅  Deploy successful!
Preview URL: https://e93c944c.pydantic-docs2.pages.dev
Branch Preview URL: https://extra-json-schema.pydantic-docs2.pages.dev

View logs

@adriangb
Copy link
Member Author

please review

}


def test_dataclass_with_extra_forbid():
Copy link
Contributor

@dmontagu dmontagu Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are at least a couple cases with mismatched test names and contents (including at least this one and test_typeddict_with_extra_forbid); I would suggest parametrizing to make it easier to review:

@pytest.mark.parametrize(
    'extra,additional_properties', [('allow', {}), ('ignore', None), ('forbid', False), (None, None)]
)
def test_basemodel_extra(extra, additional_properties):
    class A(BaseModel):
        if extra is not None:
            model_config = dict(extra=extra)
        data: str

    json_schema = A.model_json_schema()
    if additional_properties is None:
        assert 'additionalProperties' not in json_schema
    else:
        assert json_schema['additionalProperties'] == additional_properties

and similar for typeddict, dataclass

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just fixed the one name

if extra == 'forbid':
additional_properties = False
elif extra == 'allow':
additional_properties = True
Copy link
Contributor

@dmontagu dmontagu Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see somewhere saying that this should be set to True when additionalProperties are allowed? Looking at https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties I only see it saying it should be False or a valid JSON schema. (That's why in my dmontagu/6082 branch I set it to {}, which is the JSON schema for Any.)

If True means the same thing as {} here then this is fine, I just haven't seen that before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This could also be handled in _update_class_schema, I just see it was passed through without modification down there)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True seems ot be valid, at least according to SwaggerUI

@adriangb adriangb merged commit 38fc35b into main Jun 13, 2023
53 checks passed
@adriangb adriangb deleted the extra-json-schema branch June 13, 2023 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

explicit additionalProperties even when model_config["extra"] != "forbid"
3 participants