Skip to content

Commit

Permalink
Exclude BaseModel docstring from JSON schema description (#8352)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Dec 12, 2023
1 parent 98965ad commit 5e80a6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pydantic/_internal/_generate_schema.py
Expand Up @@ -212,6 +212,8 @@ def modify_model_json_schema(
Returns:
JsonSchemaValue: The updated JSON schema.
"""
from ..main import BaseModel

json_schema = handler(schema_or_field)
original_schema = handler.resolve_ref_schema(json_schema)
# Preserve the fact that definitions schemas should never have sibling keys:
Expand All @@ -221,7 +223,8 @@ def modify_model_json_schema(
original_schema['allOf'] = [{'$ref': ref}]
if 'title' not in original_schema:
original_schema['title'] = cls.__name__
docstring = cls.__doc__
# BaseModel; don't use cls.__doc__ as it will contain the verbose class signature by default
docstring = None if cls is BaseModel else cls.__doc__
if docstring and 'description' not in original_schema:
original_schema['description'] = inspect.cleandoc(docstring)
return json_schema
Expand Down
7 changes: 7 additions & 0 deletions tests/test_json_schema.py
Expand Up @@ -5812,3 +5812,10 @@ class OuterModel(pydantic.BaseModel):

with pytest.raises(ValidationError):
OuterModel(x=2, y=-1, z=-1)


def test_description_not_included_for_basemodel() -> None:
class Model(BaseModel):
x: BaseModel

assert 'description' not in Model.model_json_schema()['$defs']['BaseModel']

0 comments on commit 5e80a6d

Please sign in to comment.