Replies: 7 comments
-
You need to declare it in the decorator:
|
Beta Was this translation helpful? Give feedback.
-
Thanks...I missed that! And that "sort of" works. But that's not really the response_model. The real response model is:
But specifying that gives:
The decorator you specified works, but it's not the whole response, ResponseInfo is a substructure (see the model in my post). The hierarchy is: AuthorIndex => AuthorIndexStruct => {ResponseInfo, List[AuthorIndexItem] = []} and ResponseInfo is just the information header, and the real data is in List[AuthorIndexItem] An example return json response is this:
(I haven't suppled the publications URL field mapping yet) So it looks like a bug or design issue? It doesn't seem to support nested json structures? Or am I missing something else? ETA: If I put the lower level model in the decorator, the docs works and shows the lower model...but the API stops working (probably due to validation of the actual response). So I can't even show the lower item in the doc with this technique. |
Beta Was this translation helpful? Give feedback.
-
@nrshapiro There should be no problems with nested json structures, and I don't see anything obvious about your use case that should be preventing you from getting this to work. Since you are getting an internal server error (and it's while generating the schema), it's probably not just a validation issue. Could you share the error traceback you are getting when you try to access the docs with If you have trouble getting the traceback, try setting app = FastAPI(debug=True) # you might have other arguments too |
Beta Was this translation helpful? Give feedback.
-
I started uvicorn with --debug too. Here's the log:
|
Beta Was this translation helpful? Give feedback.
-
@nrshapiro I think I found your problem. Here's your code: class AuthorIndexStruct(BaseModel):
responseInfo: ResponseInfo(listType="authorindex")
responseSet: List[AuthorIndexItem] = [] The annotation on class AuthorIndexStruct(BaseModel):
responseInfo: ResponseInfo = ResponseInfo(listType="authorindex")
responseSet: List[AuthorIndexItem] = [] or similar. In particular, what comes after the colon (and before After making that change, I was able to generate the swagger docs without an error (I created the I'm not sure I understand your goals here, but it seems like this might not be accomplishing what you intend -- you'd probably want to set the If I'm understanding correctly, I'd probably just go with |
Beta Was this translation helpful? Give feedback.
-
Nailed it! Thank you! I'm so glad it was just my error, because now it works properly! Sorry to take your time, but I've only been working with FastAPI and Pydantic for a few days! Again, very much appreciated. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help @stefanondisponibile and @dmontagu ! 🚀 🍰 And thanks @nrshapiro for reporting back and closing the issue 🎉 |
Beta Was this translation helpful? Give feedback.
-
Description
FastAPI doc does not show structure of response (I'm using Pydantic).
I am working on porting an API to FastAPI. I have FastAPI and pydantic working nicely on my system. I import my pydantic models in a file called models.py, if that makes a difference, they're not directly in main. I like this organization. Much of the work for the endpoints themselves are done in a library file I call opasAPISupportLib.py which needs those too. In the main file, I don't need to do a lot for an endpoint, here's one:
I have finshed three endpoints and they don't show the response structure in the doc page with either the Swagger based doc or ReDoc.
Here's the relevant models for this endpoint from models.py:
I expected to see a sample json structure in the Responses 200 area. Nowhere in the doc as generated in either docs generator does it show the json return structure for API users. I would have expected to see something like this;
Additional context
I've seen online examples which also have blank areas for responses, for code 200. Shouldn't it show the response model somewhere?
Beta Was this translation helpful? Give feedback.
All reactions