From 60bc8fcf64c54919441ca3dd0685d91987defee1 Mon Sep 17 00:00:00 2001 From: Tim Petricola Date: Wed, 22 Nov 2023 10:07:30 +0100 Subject: [PATCH] Add test --- .../tests/map-openapi-endpoints.test.ts | 52 +++++++++++++++++++ .../tests/samples/parameters.yaml | 27 ++++++++++ 2 files changed, 79 insertions(+) create mode 100644 packages/typed-openapi/tests/samples/parameters.yaml diff --git a/packages/typed-openapi/tests/map-openapi-endpoints.test.ts b/packages/typed-openapi/tests/map-openapi-endpoints.test.ts index 042aece..910dec3 100644 --- a/packages/typed-openapi/tests/map-openapi-endpoints.test.ts +++ b/packages/typed-openapi/tests/map-openapi-endpoints.test.ts @@ -2612,4 +2612,56 @@ describe("map-openapi-endpoints", () => { } `); }); + + test("path and operation parameters", async ({ expect }) => { + const openApiDoc = (await SwaggerParser.parse("./tests/samples/parameters.yaml")) as OpenAPIObject; + expect(mapOpenApiEndpoints(openApiDoc).endpointList).toMatchInlineSnapshot(` + [ + { + "meta": { + "alias": "get_UsersId", + "areParametersRequired": true, + "hasParameters": true, + }, + "method": "get", + "operation": { + "parameters": [ + { + "description": "If true, the endpoint returns only the user metadata.", + "in": "query", + "name": "metadata", + "required": false, + "schema": { + "type": "boolean", + }, + }, + ], + "responses": { + "200": { + "description": "OK", + }, + }, + "summary": "Gets a user by ID", + }, + "parameters": { + "path": { + "id": { + "type": "keyword", + "value": "number", + }, + }, + "query": { + "type": "ref", + "value": "Partial<{ metadata: boolean }>", + }, + }, + "path": "/users/{id}", + "response": { + "type": "keyword", + "value": "unknown", + }, + }, + ] + `); + }); }); diff --git a/packages/typed-openapi/tests/samples/parameters.yaml b/packages/typed-openapi/tests/samples/parameters.yaml new file mode 100644 index 0000000..0ffdd06 --- /dev/null +++ b/packages/typed-openapi/tests/samples/parameters.yaml @@ -0,0 +1,27 @@ +# https://swagger.io/docs/specification/describing-parameters/#common-for-path +openapi: 3.0.3 +info: + title: Spec with both path-level and operation-level parameters +paths: + /users/{id}: + parameters: + - in: path + name: id + schema: + type: integer + required: true + description: The user ID. + # GET/users/{id}?metadata=true + get: + summary: Gets a user by ID + # Note we only define the query parameter, because the {id} is defined at the path level. + parameters: + - in: query + name: metadata + schema: + type: boolean + required: false + description: If true, the endpoint returns only the user metadata. + responses: + '200': + description: OK