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

Enhancement: Generate OpenAPI parameter descriptions from handler docstrings #3726

Open
Keating950 opened this issue Sep 10, 2024 · 2 comments
Labels
Enhancement This is a new feature or request

Comments

@Keating950
Copy link

Keating950 commented Sep 10, 2024

Summary

When OpenAPIConfig.use_handler_docstrings = True, OpenAPI operation descriptions are automatically generated from handler methods' docstrings. It would be great if this feature could be expand to include parameter descriptions extracted from the reST.

Basic Example

This handler:

@get("/{a:int}/{b:str}")
async def foobar(a: int, b: str) -> str:
    """
    Retrieve the specified document.

    :param a: The first path parameter.
    :param b: The second path parameter.
    """
    ...

Would generate this OpenAPI schema:

{
    "paths": {
        "/{a}/{b}": {
            "get": {
                "summary": "Foobar",
                "description": "Retrieve the specified document.",
                "operationId": "ABFoobar",
                "parameters": [
                    {
                        "name": "a",
                        "in": "path",
                        "schema": {
                            "type": "integer",
                            "description": "The first path parameter"
                        },
                        "description": "The first path parameter",
                    },
                    {
                        "name": "b",
                        "in": "path",
                        "schema": {
                            "type": "string",
                            "description": "The second path parameter"
                        },
                        "description": "The second path parameter",
                    }
                ],
                ...

Drawbacks and Impact

Impacts:

  • Would eliminate the need to either duplicate descriptions between docstrings and type annotations or implement an ad-hoc workaround.

Drawbacks:

  • Would presumably require adding at least a restructured text parser as a dependency.

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@Keating950 Keating950 added the Enhancement This is a new feature or request label Sep 10, 2024
@provinzkraut
Copy link
Member

Interesting idea. Rst parsing is a bit tricky, but maybe for this simple case we can get away with regexing it? 👀

@JacobCoffee

@provinzkraut
Copy link
Member

We might also be able to leverage griffe. It supports most of the common docstring formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
Development

No branches or pull requests

2 participants