Skip to content
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

Open
robbieaverill opened this issue Oct 17, 2022 · 1 comment · May be fixed by #31
Open

Field with default value becomes optional on the TypeScript interface #28

robbieaverill opened this issue Oct 17, 2022 · 1 comment · May be fixed by #31

Comments

@robbieaverill
Copy link

Example:

class MyModel(BaseModel):
  my_bool_property: bool = False

Convert to TypeScript and see output:

interface MyModel {
  my_bool_property?: bool;
}

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.:

newModel = MyModel()
assert newModel.my_bool_property is False  # assertion passes

Expected TypeScript interface:

interface MyModel {
  my_bool_property: bool;
}
@hay-kot
Copy link

hay-kot commented Oct 18, 2022

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.

hay-kot added a commit to mealie-recipes/mealie that referenced this issue Oct 18, 2022
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.
bibermann added a commit to bibermann/pydantic-to-typescript that referenced this issue Dec 28, 2022
@bibermann bibermann linked a pull request Dec 28, 2022 that will close this issue
bibermann added a commit to bibermann/pydantic-to-typescript that referenced this issue Dec 28, 2022
bibermann added a commit to bibermann/pydantic-to-typescript that referenced this issue Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants