-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field with default value becomes optional on the TypeScript interface #28
Comments
I think that can go either way, so the result should be optional. For example, if I'm writing a client for an API the body of the request is going to look like this interface MyModel {
my_bool_property?: bool;
} But the response would not be optional because Pydantic handles to null types for us interface MyModel {
my_bool_property: bool;
} Really, I need both of them when build an API client, or I'd need to strictly define the request body and the response body with different defaults on the backend as two different models. |
This PR does too many things :( 1. Major refactoring of the dev/scripts and dev/code-generation folders. Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 2. Re-Implement using the generated api routes for testing This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 3. Minor Perf improvements for the All Recipes endpoint A. Removed redundant loops B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default jsonable_encoder. 4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR. See this issue phillipdupuis/pydantic-to-typescript#28 Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
Example:
Convert to TypeScript and see output:
I believe that the interface shouldn't define the property as optional, since Pydantic will ensure it is hydrated with the default value of false if it's not provided when creating models, e.g.:
Expected TypeScript interface:
The text was updated successfully, but these errors were encountered: