You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat: add `all_fields_required` option
* docs: update README for new `--all-fields-required` flag
* feat: account for serialization aliases
* fix: ensure all reachable models are processed under --all-fields-required
Previously, some Pydantic models that were only indirectly referenced
(e.g. buried inside an Annotated[Union[...]] field) weren't processed
before final schema generation. As a result, their default-valued fields
would be optional in the TypeScript output, even when
all_fields_required was set.
We now recursively walk and collect all reachable models before calling
`_clean_json_schema`, ensuring all schemas are properly processed.
* fix: use List[str] instead of list[str] for 3.8 compatibility just in case
* fix: import Annotated, get_args, and get_origin from typing_extensions for python 3.8 compatibility
* feat: mark all fields as required in a simpler way (now works for both v1 and v2 models)
- revert last 4 commits (serialization aliases and walking all models)
- just add any property names from the schema directly into `required`
instead of working with the model fields directly and needing to
account for aliases
- walking models no longer needed for all-fields-required to apply to
all models used; even if `model` is None we still handle the schema
* docs: update README now that the flag works for both v1 and v2
* docs: update readme with better named example fields
|‑‑module | name or filepath of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. |
38
-
|‑‑output | name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts' |
39
-
|‑‑exclude | name of a pydantic model which should be omitted from the resulting typescript definitions. This option can be defined multiple times, ex: `--exclude Foo --exclude Bar` to exclude both the Foo and Bar models from the output. |
40
-
|‑‑json2ts‑cmd | optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed locally (ex: 'yarn json2ts') or if the exact path to the executable is required (ex: /myproject/node_modules/bin/json2ts) |
|‑‑module | name or filepath of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. |
38
+
|‑‑output | name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts' |
39
+
|‑‑exclude | name of a pydantic model which should be omitted from the resulting typescript definitions. This option can be defined multiple times, ex: `--exclude Foo --exclude Bar` to exclude both the Foo and Bar models from the output. |
40
+
|‑‑json2ts‑cmd | optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed locally (ex: 'yarn json2ts') or if the exact path to the executable is required (ex: /myproject/node_modules/bin/json2ts) |
41
+
|‑‑all‑fields‑required | optional (off by default). Treats all fields as required (present) in the generated TypeScript interfaces. |
41
42
42
43
---
43
44
44
-
###Usage
45
+
## Usage
45
46
46
47
Define your pydantic models (ex: /backend/api.py):
If you would like to treat all fields as required in the generated TypeScript interfaces, you can use the `--all-fields-required` flag.
146
+
147
+
This is useful, for example, when representing a response from your Python backend API—since Pydantic will populate any missing fields with defaults before sending the response.
nullable_int:number|null; // optional if Pydantic V1
196
+
nullable_int_with_default?:number|null;
197
+
nullable_int_with_null_default?:number|null;
198
+
}
199
+
```
200
+
201
+
> [!NOTE]
202
+
> If you're using Pydantic V1, `nullable_int` will also be optional (`nullable_int?: number | null`) when executing without `--all-fields-required`. See [Pydantic docs](https://docs.pydantic.dev/2.10/concepts/models/#required-fields):
203
+
> > In Pydantic V1, fields annotated with `Optional` or `Any` would be given an implicit default of `None` even if no default was explicitly specified. This behavior has changed in Pydantic V2, and there are no longer any type annotations that will result in a field having an implicit default value.
0 commit comments