MCPcopy
hub / github.com/pydantic/pydantic / model_fields_schema

Method model_fields_schema

pydantic/json_schema.py:1726–1747  ·  view source on GitHub ↗

Generates a JSON schema that matches a schema that defines a model's fields. Args: schema: The core schema. Returns: The generated JSON schema.

(self, schema: core_schema.ModelFieldsSchema)

Source from the content-addressed store, hash-verified

1724 return json_schema
1725
1726 def model_fields_schema(self, schema: core_schema.ModelFieldsSchema) -> JsonSchemaValue:
1727 """Generates a JSON schema that matches a schema that defines a model's fields.
1728
1729 Args:
1730 schema: The core schema.
1731
1732 Returns:
1733 The generated JSON schema.
1734 """
1735 named_required_fields: list[tuple[str, bool, CoreSchemaField]] = [
1736 (name, self.field_is_required(field, total=True), field)
1737 for name, field in schema['fields'].items()
1738 if self.field_is_present(field)
1739 ]
1740 if self.mode == 'serialization':
1741 named_required_fields.extend(self._name_required_computed_fields(schema.get('computed_fields', [])))
1742 json_schema = self._named_required_fields_schema(named_required_fields)
1743 extras_schema = schema.get('extras_schema', None)
1744 if extras_schema is not None:
1745 schema_to_update = self.resolve_ref_schema(json_schema)
1746 schema_to_update['additionalProperties'] = self.generate_inner(extras_schema)
1747 return json_schema
1748
1749 def field_is_present(self, field: CoreSchemaField) -> bool:
1750 """Whether the field should be included in the generated JSON schema.

Calls 8

field_is_requiredMethod · 0.95
field_is_presentMethod · 0.95
resolve_ref_schemaMethod · 0.95
generate_innerMethod · 0.95
itemsMethod · 0.45
getMethod · 0.45