Comments (3)
It's possible to document multiple responses with the current version, but it's up to the developer to do it manually in the docstring of the endpoint:
@app_.route("/endpoint/", methods=["GET"])
async def get(self) -> Puppy:
"""
description: Puppy.
responses:
200:
description: The puppy.
404:
description: Puppy not found
"""
...
So I have been working on adding a bit of control over errors and it could be useful for documenting other responses (or at least error responses). I have defined a standard response for errors that includes the error type, status code and a detailed message; the schema used for these responses can be found in starlette_api.responses
and it's automatically included in OpenAPI schema as a component, and by default all endpoints will generate a default response that specifies this schema is used for unexpected errors. An example using this schema could be:
@app_.route("/endpoint/", methods=["GET"])
async def get(self) -> Puppy:
"""
description: Puppy.
responses:
200:
description: The puppy.
404:
description: Puppy not found
content:
application/json:
schema:
$ref: #/components/schemas/APIError
"""
try:
return get_puppy()
except PuppyNotFound:
raise HTTPException(status_code=404, detail="Puppy not found")
Again, it's a bit handcrafted but at least you have an standardize response for all errors in your API.
As a note, when the OpenAPI schema is generated the output schema of the view will be added automatically to the first response code found.
I personally haven't faced the case where your endpoint will return more than one positive response so I didn't even think that it could be useful. If you feel that it would be nice to have it, I'm open to any suggestion :)
from flama.
Thanks for the quick response! I am on board with just the single positive response. I don't see any good reason to have multiple success response types.
I am interested in your standard response for errors. Have you thought of any other way to specify the error types? My first thought was something like this:
@app_.route("/endpoint/", methods=["GET"])
@app_.error_response(404, HttpException, description='Puppy not found')
async def get(self) -> Puppy:
"""
description: Puppy.
responses:
200:
description: The puppy.
"""
try:
return get_puppy()
except PuppyNotFound:
raise HTTPException(status_code=404, detail="Puppy not found")
I'm not super familiar with this code base so not sure how much effort this would take, but if you had a use for something like this, I'd be up for helping out where I could. Also, maybe this is overkill, but I do like the idea of something similar.
from flama.
It's possible to implement something like you proposed but my concern is that the first idea in mind I had when started the project was to make the code as much expressive as possible and the schema generation is based on the information of the code itself. What you propose is a decorator that simply adds metadata to the code, but doesn't modify the functionality of the API itself so, under my perspective it adds complexity to the code but doesn't modify it behavior so I'm keen on using the docstring as the source of the endpoints metadata for everything that cannot be inferred from the code.
from flama.
Related Issues (20)
- Discover nested schemas
- Support for domain-driven-design (DDD)
- Refactor SQLAlchemy Repository for Multiple Tables
- Add support to Python 3.12
- Allow to define requestBody and parameters in OpenAPI docstrings
- Allow bulk creation and deletion on SQLAlchemy repositories
- Single type injection
- Component for injecting request's cookies
- Routes resolution is not working properly on resources under nested applications
- Include templates building as part of poetry build
- Include nested applications as part of Lifespan process
- Integrate dependabot
- Cache mechanism for routing resolution
- Connections manager for SQLAlchemy module
- Authentication mechanism
- Ordering for repositories list
- Enhanced operations for CRUD resources
- Core installation throws ModuleNotFoundError: `sqlalchemy`
- Flama logo is missing when serving
- Domain-driven design for HTTP resources
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flama.