Skip to content

Commit 1bdcf49

Browse files
committed
'clean_json_schema' will now also remove the 'str' docstring, in pydantic<2 this can unintentionally be applied to Literal types
1 parent daf1835 commit 1bdcf49

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pydantic2ts/cli/script.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
LOG = logging.getLogger("pydantic2ts")
3636

37+
_USELESS_ENUM_DESCRIPTION = "An enumeration."
38+
_USELESS_STR_DESCRIPTION = inspect.getdoc(str)
39+
3740

3841
def _import_module(path: str) -> ModuleType:
3942
"""
@@ -159,15 +162,20 @@ def _clean_json_schema(schema: Dict[str, Any], model: Any = None) -> None:
159162
"""
160163
Clean up the resulting JSON schemas via the following steps:
161164
162-
1) Get rid of the useless "An enumeration." description applied to Enums
163-
which don't have a docstring.
165+
1) Get rid of descriptions that are auto-generated and just add noise:
166+
- "An enumeration." for Enums
167+
- `inspect.getdoc(str)` for Literal types
164168
2) Remove titles from JSON schema properties.
165169
If we don't do this, each property will have its own interface in the
166170
resulting typescript file (which is a LOT of unnecessary noise).
167171
3) If it's a V1 model, ensure that nullability is properly represented.
168172
https://github.com/pydantic/pydantic/issues/1270
169173
"""
170-
if "enum" in schema and schema.get("description") == "An enumeration.":
174+
description = schema.get("description")
175+
176+
if "enum" in schema and description == _USELESS_ENUM_DESCRIPTION:
177+
del schema["description"]
178+
elif description == _USELESS_STR_DESCRIPTION:
171179
del schema["description"]
172180

173181
properties: Dict[str, Dict[str, Any]] = schema.get("properties", {})

0 commit comments

Comments
 (0)