|
34 | 34 |
|
35 | 35 | LOG = logging.getLogger("pydantic2ts") |
36 | 36 |
|
| 37 | +_USELESS_ENUM_DESCRIPTION = "An enumeration." |
| 38 | +_USELESS_STR_DESCRIPTION = inspect.getdoc(str) |
| 39 | + |
37 | 40 |
|
38 | 41 | def _import_module(path: str) -> ModuleType: |
39 | 42 | """ |
@@ -159,15 +162,20 @@ def _clean_json_schema(schema: Dict[str, Any], model: Any = None) -> None: |
159 | 162 | """ |
160 | 163 | Clean up the resulting JSON schemas via the following steps: |
161 | 164 |
|
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 |
164 | 168 | 2) Remove titles from JSON schema properties. |
165 | 169 | If we don't do this, each property will have its own interface in the |
166 | 170 | resulting typescript file (which is a LOT of unnecessary noise). |
167 | 171 | 3) If it's a V1 model, ensure that nullability is properly represented. |
168 | 172 | https://github.com/pydantic/pydantic/issues/1270 |
169 | 173 | """ |
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: |
171 | 179 | del schema["description"] |
172 | 180 |
|
173 | 181 | properties: Dict[str, Dict[str, Any]] = schema.get("properties", {}) |
|
0 commit comments