Skip to content

Commit c4e9faf

Browse files
author
Doug Borg
committed
feat: improve model docstring header with title fallback + add test
1 parent 630ffe9 commit c4e9faf

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/openapi_python_generator/language_converters/python/templates/models.jinja2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ from pydantic import BaseModel, Field
1010

1111
class {{ schema_name }}(BaseModel):
1212
"""
13-
{{ schema.title }} model
14-
{% if schema.description != None %}
13+
{% if schema.title %}{{ schema.title }}{% else %}{{ schema_name }}{% endif %} model
14+
{% if schema.description %}
1515
{{ schema.description }}
1616
{% endif %}
17-
17+
{% if parent_comment %}
18+
{{ parent_comment }}
19+
{% endif %}
1820
"""
1921
{% for property in properties %}
2022

src/openapi_python_generator/language_converters/python/templates/models_pydantic_2.jinja2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ from pydantic import BaseModel, Field
1010

1111
class {{ schema_name }}(BaseModel):
1212
"""
13-
{{ schema.title }} model
14-
{% if schema.description != None %}
13+
{% if schema.title %}{{ schema.title }}{% else %}{{ schema_name }}{% endif %} model
14+
{% if schema.description %}
1515
{{ schema.description }}
1616
{% endif %}
17-
17+
{% if parent_comment %}
18+
{{ parent_comment }}
19+
{% endif %}
1820
"""
1921
model_config = {
2022
"populate_by_name": True,

tests/test_model_docstring.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from openapi_pydantic.v3 import Schema, Components, DataType
2+
3+
from openapi_python_generator.language_converters.python.model_generator import generate_models
4+
from openapi_python_generator.common import PydanticVersion
5+
6+
7+
def test_model_docstring_title_used_when_present_and_fallback_to_name():
8+
"""Ensure we prefer schema.title when present, fallback to schema name, and never emit 'None model'."""
9+
widget_schema = Schema( # type: ignore[arg-type]
10+
type=DataType.OBJECT,
11+
title="Fancy Widget",
12+
properties={"id": Schema(type=DataType.INTEGER)}, # type: ignore[arg-type]
13+
)
14+
no_title_schema = Schema( # type: ignore[arg-type]
15+
type=DataType.OBJECT,
16+
properties={"name": Schema(type=DataType.STRING)}, # type: ignore[arg-type]
17+
)
18+
19+
components = Components(schemas={"Widget": widget_schema, "NoTitle": no_title_schema}) # type: ignore[arg-type]
20+
models = {m.file_name: m for m in generate_models(components, PydanticVersion.V2)}
21+
22+
widget_content = models["Widget"].content
23+
notitle_content = models["NoTitle"].content
24+
25+
assert "Fancy Widget model" in widget_content # title used
26+
assert "NoTitle model" in notitle_content # fallback used
27+
assert "None model" not in widget_content
28+
assert "None model" not in notitle_content

0 commit comments

Comments
 (0)