Skip to content

Commit c821eac

Browse files
authored
Revert "Respect config in TypeAdapter.json_schemas() (#13890)" (#13951)
1 parent a656b75 commit c821eac

3 files changed

Lines changed: 6 additions & 35 deletions

File tree

‎pydantic/json_schema.py‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -372,35 +372,23 @@ def generate_definitions(
372372
Raises:
373373
PydanticUserError: Raised if the JSON schema generator has already been used to generate a JSON schema.
374374
"""
375-
return self._generate_definitions([(key, mode, schema, None) for key, mode, schema in inputs])
376-
377-
def _generate_definitions(
378-
self, inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema, ConfigDict | None]]
379-
) -> tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]:
380-
"""Same as `generate_definitions()`, but with an additional config for each input.
381-
382-
This config is used for core schemas that don't carry their own config (e.g. the core schema
383-
of a type adapter).
384-
"""
385375
if self._used:
386376
raise PydanticUserError(
387377
'This JSON schema generator has already been used to generate a JSON schema. '
388378
f'You must create a new instance of {type(self).__name__} to generate a new JSON schema.',
389379
code='json-schema-already-used',
390380
)
391381

392-
for _, mode, schema, config in inputs:
382+
for _, mode, schema in inputs:
393383
self._mode = mode
394-
with self._config_wrapper_stack.push(config):
395-
self.generate_inner(schema)
384+
self.generate_inner(schema)
396385

397386
definitions_remapping = self._build_definitions_remapping()
398387

399388
json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {}
400-
for key, mode, schema, config in inputs:
389+
for key, mode, schema in inputs:
401390
self._mode = mode
402-
with self._config_wrapper_stack.push(config):
403-
json_schema = self.generate_inner(schema)
391+
json_schema = self.generate_inner(schema)
404392
json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema(json_schema)
405393

406394
json_schema = {'$defs': self.definitions}

‎pydantic/type_adapter.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,9 @@ def json_schemas(
761761
assert not isinstance(adapter.core_schema, _mock_val_ser.MockCoreSchema), (
762762
'this is a bug! please report it'
763763
)
764-
# The configuration provided to the type adapter (if any) is not part of the core schema,
765-
# so we need to explicitly make it available to the JSON Schema generator:
766-
inputs_.append((key, mode, adapter.core_schema, adapter._config))
764+
inputs_.append((key, mode, adapter.core_schema))
767765

768-
json_schemas_map, definitions = schema_generator_instance._generate_definitions(inputs_)
766+
json_schemas_map, definitions = schema_generator_instance.generate_definitions(inputs_)
769767

770768
json_schema: dict[str, Any] = {}
771769
if definitions:

‎tests/test_type_adapter.py‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -561,21 +561,6 @@ def test_ta_config_used_for_json_schema() -> None:
561561
assert ta.json_schema() == {'type': 'array', 'items': {'type': 'string', 'format': 'base64url'}}
562562

563563

564-
def test_ta_config_used_for_json_schemas() -> None:
565-
ta_bytes = TypeAdapter(Annotated[bytes, Field(default=b'\xff')], config=ConfigDict(ser_json_bytes='base64'))
566-
ta_str = TypeAdapter(str, config=ConfigDict(str_max_length=10))
567-
ta_no_config = TypeAdapter(bytes)
568-
569-
schemas, _ = TypeAdapter.json_schemas(
570-
[('bytes', 'validation', ta_bytes), ('str', 'validation', ta_str), ('no_config', 'validation', ta_no_config)]
571-
)
572-
assert schemas == {
573-
('bytes', 'validation'): {'type': 'string', 'format': 'base64url', 'default': '_w=='},
574-
('str', 'validation'): {'type': 'string', 'maxLength': 10},
575-
('no_config', 'validation'): {'type': 'string', 'format': 'binary'},
576-
}
577-
578-
579564
def defer_build_test_models(config: ConfigDict) -> list[Any]:
580565
class Model(BaseModel):
581566
model_config = config

0 commit comments

Comments
 (0)