-
Question as title I just follow this article fastapi generate typescript clients but my class has some field with default value class ResponseMessage(BaseModel):
message: str
+ info: int = 1 after the same step in above article, the generate code belike export type ResponseMessage = {
message: string;
info?: number;
}; Reproduce step python3 -m venv venv
. venv/bin/active # In linux
pip install fastapi
pip install "uvicorn[standard]"
npm init -y add # start backend
uvicorn main:app
# run generate cmd
npm run generate-client
grep -r -A3 "ResponseMessage" src/client/types.gen.ts
export type ResponseMessage = {
message: string;
info?: number;
};
--
export type CreateItemItemsPostResponse = ResponseMessage;
export type $OpenApiTs = {
'/items/': {
--
200: ResponseMessage;
/**
* Validation Error
*/
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
info: int = 1
@app.post("/items/", response_model=ResponseMessage)
async def create_item(item: Item):
return {"message": "item received"}
@app.get("/items/", response_model=list[Item])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
pip list | grep pydantic
pydantic 2.7.1
pydantic_core 2.18.2
pip list | grep fastapi
fastapi 0.111.0
fastapi-cli 0.0.4
python3 --version
Python 3.10.12 but when frontend received the data, it must be number not optional, what can i do I searched 'default' and 'optional' both in i search 'default' only found these two, both not help for me
i think https://fastapi.tiangolo.com/zh/how-to/separate-openapi-schemas/#model-for-input-and-output-in-docs is might what i want, but it seems not worked, i'm confused what i missing? I do need default value for backend code and i hope the response data field is not optional in generate class in typescript. how can i achieve it or any easier other way to solve the problem. My current temporary solution is something like this, using a class without a default value as a description, and then specifically creating a class with a default value based on it. However, when there are many fields or nested fields, this solution will have a very poor experience. class Item(BaseModel):
name: str
description: str
code: int
class Item200(Item):
description: str = ""
code: int = 200
@app.post("/items/",response_model=Item)
def create_item(item: Item)->Item:
return Item200(name="n") |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey @CroMarmot, that's exactly how you're supposed to define it! @app.post("/items/")
def create_item(item: Item)->Item200:
return Item200(name="n") |
Beta Was this translation helpful? Give feedback.
-
I'm not familiar with openapi, I used protobuf before. Maybe this is a problem with fastapi and not openapi-ts. I don't know. But I just found another issue fastapi/fastapi#10453 this seems work for me |
Beta Was this translation helpful? Give feedback.
I'm not familiar with openapi, I used protobuf before. Maybe this is a problem with fastapi and not openapi-ts. I don't know.
But I just found another issue fastapi/fastapi#10453 this seems work for me