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

Typed dict JSON schema does not pay attention to extra_behavior argument #7809

Closed
1 task done
me-and opened this issue Oct 12, 2023 · 2 comments · Fixed by #7810
Closed
1 task done

Typed dict JSON schema does not pay attention to extra_behavior argument #7809

me-and opened this issue Oct 12, 2023 · 2 comments · Fixed by #7810
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@me-and
Copy link
Contributor

me-and commented Oct 12, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When creating a JSON schema from a typed dict core schema, the core schema uses the attribute extra_behavior to determine whether additional unspecified fields are allowed. However the code for generating JSON schemas, at https://github.com/pydantic/pydantic/blob/v2.4.2/pydantic/json_schema.py#L1194, instead checks for the key extra, so the setting is ignored.

Example Code

from pydantic.json_schema import GenerateJsonSchema
import pydantic.version
import pydantic_core.core_schema as core_schema

schema = core_schema.typed_dict_schema(
        {'a': core_schema.typed_dict_field(core_schema.str_schema())},
        extra_behavior='forbid',
        )

json_schema = GenerateJsonSchema().generate(schema)

try:
    assert json_schema['additionalProperties'] == False
except (KeyError, AssertionError):
    print('Generated JSON schema missing correct extra_behavior output')
    print('Input schema:')
    print(repr(schema))
    print('Generated schema:')
    print(repr(json_schema))
    print('Version info:')
    print(pydantic.version.version_info())

Python, Pydantic & OS Version

pydantic version: 2.4.2
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=false
                 install path: /home/adam/.local/lib/python3.11/site-packages/pydantic
               python version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
                     platform: Linux-6.1.21-v8+-aarch64-with-glibc2.36
             related packages: typing_extensions-4.8.0 email-validator-2.0.0.post2 typing_extensions-4.4.0 mypy-1.0.1
@me-and me-and added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 12, 2023
me-and added a commit to me-and/pydantic that referenced this issue Oct 12, 2023
The core schema for typed dicts uses the "extra_behaviour" key to determine whether additional unspecified keys are allowed in the dict; update the JSON schema generation to expect that, rather than to look for the non-existent "extra" key.

Fixes pydantic#7809.
@me-and
Copy link
Contributor Author

me-and commented Oct 12, 2023

Hmm. My naive fix at #7810 might not be sufficient here; this is complicated by the current setup using extra in ConfigDict to mean the same thing, so it might be that this wants to be fixed by changing pydantic_core to expect an extra argument here, instead of or as well as the extra_behavior argument…

me-and added a commit to me-and/pydantic that referenced this issue Oct 12, 2023
The core schema for typed dicts has an "extra_behavior" key for checking
whether additional unspecified keys are allowed, but the JSON schema
generator ignores this and only checks the configuration dict.  Have the
JSON schema generator check both places, and prefer the value set in the
core schema over the one in the config dictionary if both are present.

Fixes pydantic#7809.
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Oct 12, 2023
@sydney-runkle sydney-runkle self-assigned this Oct 12, 2023
@sydney-runkle
Copy link
Member

@me-and,

Thanks for reporting this and for the contribution! I'll get back to you on Monday with a more detailed review of your PR 😄.

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

Successfully merging a pull request may close this issue.

2 participants