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

getParameterValues throws error with optional enum query param and default value (FastAPI 0.91.0) #102

Open
afif-fahim opened this issue Apr 28, 2024 · 1 comment

Comments

@afif-fahim
Copy link

afif-fahim commented Apr 28, 2024

Summary

When using FastAPI, attempting to utilize the getEndpointSnippets function on an endpoint with an optional query parameter of enum type, which has a default value set, results in an error.

Reproduction Steps

  • Create an endpoint in a FastAPI application with an optional query parameter of enum type and assign a default value to this parameter.
  • Obtain the OpenAPI schema generated by FastAPI for this endpoint.
  • Try to generate snippets using getEndpointSnippets.

Example Scenario

Consider the following API endpoint:

class SortOrder(str, Enum):
    ASCENDING = "asc"
    DESCENDING = "desc"

@router.get(
    "/v1/items",
    tags=["Catalog Integration"],
    summary="Get a list of item details",
    response_model=Success,
)
async def items(
    request: Request,
    start: int = Query(default=None, ge=0),
    size: int = Query(default=None, ge=1, le=1000),
    sort: SortOrder = Query(default=SortOrder.DESCENDING),
):

The generated OpenAPI schema for this endpoint might resemble the following:

{
    "paths": {
        "/v1/items": {
            "get": {
                "tags": [
                    "Catalog Integration"
                ],
                "summary": "Get a list of item details",
                "operationId": "items_v1_items_get",
                "parameters": [
                    {
                        "required": false,
                        "schema": {
                            "title": "Start",
                            "minimum": 0.0,
                            "type": "integer"
                        },
                        "name": "start",
                        "in": "query"
                    },
                    {
                        "required": false,
                        "schema": {
                            "title": "Size",
                            "maximum": 1000.0,
                            "minimum": 1.0,
                            "type": "integer"
                        },
                        "name": "size",
                        "in": "query"
                    },
                    {
                        "required": false,
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/components/schemas/SortOrder"
                                }
                            ],
                            "default": "desc"
                        },
                        "name": "sort",
                        "in": "query"
                    }
                ]
            }
        }
    }
}

Error Stacktrace

/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:441
          'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE';
                                                      ^

TypeError: Cannot read properties of undefined (reading 'toUpperCase')
    at getParameterValues (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:441:49)
    at parseParametersToQuery (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:515:34)
    at getParameterCollectionIn (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:570:27)
    at getQueryStrings (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:603:10)
    at Object.createHar [as getEndpoint] (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/openapi-to-har.js:45:18)
    at Object.getEndpointSnippets (/home/user/redoc_generation/node_modules/snippet-enricher-cli/node_modules/openapi-snippet/index.js:31:29)
    at enrichSchema (/home/user/redoc_generation/node_modules/snippet-enricher-cli/index.js:18:39)
    at /home/user/redoc_generation/node_modules/snippet-enricher-cli/index.js:52:12
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)

Node.js v18.19.1
@afif-fahim afif-fahim changed the title getQueryStrings throws error with optional enum query param and default value (FastAPI 0.91.0) getParameterValues throws error with optional enum query param and default value (FastAPI 0.91.0) Apr 29, 2024
@michaelgwelch
Copy link
Contributor

michaelgwelch commented Jun 25, 2024

This might be addressed by my rather old PR #90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants