ErikWittern / openapi-snippet

Generates code snippets for given Swagger / Open API documents
MIT License
115 stars 65 forks source link

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

Open afif-fahim opened 2 months ago

afif-fahim commented 2 months ago

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

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
michaelgwelch commented 3 weeks ago

This might be addressed by my rather old PR #90