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

JSON schema: fix extra parameter handling #7810

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pydantic/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,10 @@ def typed_dict_schema(self, schema: core_schema.TypedDictSchema) -> JsonSchemaVa
with self._config_wrapper_stack.push(config):
json_schema = self._named_required_fields_schema(named_required_fields)

extra = config.get('extra', 'ignore')
try:
extra = schema['extra_behavior']
except KeyError:
extra = config.get('extra', 'ignore')
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
if extra == 'forbid':
json_schema['additionalProperties'] = False
elif extra == 'allow':
Expand Down
50 changes: 50 additions & 0 deletions tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,23 @@ class Model(TypedDict):
}


def test_typeddict_with_extra_behavior_allow():
class Model:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema:
return core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(core_schema.str_schema())},
extra_behavior='allow',
)

assert TypeAdapter(Model).json_schema() == {
'type': 'object',
'properties': {'a': {'title': 'A', 'type': 'string'}},
'required': ['a'],
'additionalProperties': True,
}


def test_typeddict_with_extra_ignore():
class Model(TypedDict):
__pydantic_config__ = ConfigDict(extra='ignore') # type: ignore
Expand All @@ -2419,6 +2436,22 @@ class Model(TypedDict):
}


def test_typeddict_with_extra_behavior_ignore():
class Model:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema:
return core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(core_schema.str_schema())},
extra_behavior='ignore',
)

assert TypeAdapter(Model).json_schema() == {
'type': 'object',
'properties': {'a': {'title': 'A', 'type': 'string'}},
'required': ['a'],
}


def test_typeddict_with_extra_forbid():
@pydantic.dataclasses.dataclass
class Model:
Expand All @@ -2434,6 +2467,23 @@ class Model:
}


def test_typeddict_with_extra_behavior_forbid():
class Model:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema:
return core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(core_schema.str_schema())},
extra_behavior='forbid',
)

assert TypeAdapter(Model).json_schema() == {
'type': 'object',
'properties': {'a': {'title': 'A', 'type': 'string'}},
'required': ['a'],
'additionalProperties': False,
}


@pytest.mark.parametrize(
'annotation,kwargs,field_schema',
[
Expand Down