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
I used the GitHub search to find a similar question and didn't find it.
I searched the FastAPI documentation, with the integrated search.
I already searched in Google "How to X in FastAPI" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to FastAPI but to Pydantic.
I already checked if it is not related to FastAPI but to Swagger UI.
I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
I commit to help with one of those options 👆
Example Code
defcustom_decorator(func: Callable) ->Callable:
asyncdefwrapper(*args: Any, **kwargs: Any) ->Any:
result=awaitfunc(*args, **kwargs)
# Inspect/modify the result here as neededreturnresultreturnwrapper
Description
Problem
I would like to apply the above custom_decorator to every route in my application. While, possible, it's cumbersome, and introduces an opportunity for errors later (by missing it).
What I've tried
Unfortunately, when I do try and apply a global decorator to routes (using approaches I've found here and through online searching), instead of getting the result of the route function itself (e.g. a pydantic model), I instead get a Response or some serialized view of the response.
What I have tried:
Modifying app.router.route_class (only have access to response)
a) modifying the endpoint in __init__
b) overriding get_route_handler
Middleware applied to the app (only have access to response)
Specifying a custom response (only have access to serialized response)
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.95.1
Python Version
3.10.3
Additional Context
The extra context here is that I want to ensure pydantic models exclude fields from being serialised only when being returned by the API. In my internal logic, I want the fields to be present. I am therefore hoping to add a decorator to each route that excludes appropriate fields depending on what is returned.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Problem
I would like to apply the above
custom_decoratorto every route in my application. While, possible, it's cumbersome, and introduces an opportunity for errors later (by missing it).What I've tried
Unfortunately, when I do try and apply a global decorator to routes (using approaches I've found here and through online searching), instead of getting the result of the route function itself (e.g. a pydantic model), I instead get a
Responseor some serialized view of the response.What I have tried:
app.router.route_class(only have access to response)a) modifying the
endpointin__init__b) overriding
get_route_handlerOperating System
macOS
Operating System Details
No response
FastAPI Version
0.95.1
Python Version
3.10.3
Additional Context
The extra context here is that I want to ensure pydantic models exclude fields from being serialised only when being returned by the API. In my internal logic, I want the fields to be present. I am therefore hoping to add a decorator to each route that excludes appropriate fields depending on what is returned.
Beta Was this translation helpful? Give feedback.
All reactions