diff --git a/packages/oas/src/utils/params.ts b/packages/oas/src/utils/params.ts index 24a6b1a5..9b2fd2b4 100644 --- a/packages/oas/src/utils/params.ts +++ b/packages/oas/src/utils/params.ts @@ -1,19 +1,28 @@ -import type { ParameterObject } from '../types'; -import type { OpenAPI } from '@har-sdk/core'; +import { ParameterObject } from '../types'; +import { getOperation } from './operation'; +import { OpenAPI } from '@har-sdk/core'; -const isParameter = (param: unknown): param is ParameterObject => - param && typeof param === 'object' && 'in' in param && 'name' in param; +const assertDereferencedParam = (param: unknown): param is ParameterObject => { + const isReference = !!param && typeof param === 'object' && '$ref' in param; -const toParameters = (param: unknown): ParameterObject[] => - Array.isArray(param) ? param.filter(isParameter) : []; + if (isReference) { + throw new Error('Specification document is expected to be dereferenced.'); + } + + return !isReference; +}; export const getParameters = ( spec: OpenAPI.Document, path: string, method: string ): ParameterObject[] => { - const pathParams = toParameters(spec.paths[path]?.parameters); - const operationParams = toParameters(spec.paths[path]?.[method]?.parameters); + const pathParams = [...(spec.paths[path]?.parameters ?? [])].filter( + assertDereferencedParam + ); + const operationParams = [ + ...(getOperation(spec, path, method)?.parameters ?? []) + ].filter(assertDereferencedParam); const combinedParams = new Map( pathParams.map((x) => [`${x.in}:${x.name}`, x]) diff --git a/packages/oas/tests/fixtures/path-item.params.resolution.oas.result.json b/packages/oas/tests/fixtures/path-item.params.resolution.oas.result.json index 32b8bec3..94d4609f 100644 --- a/packages/oas/tests/fixtures/path-item.params.resolution.oas.result.json +++ b/packages/oas/tests/fixtures/path-item.params.resolution.oas.result.json @@ -6,17 +6,8 @@ "headersSize": 0, "httpVersion": "HTTP/1.1", "method": "GET", - "queryString": [ - { - "name": "lat", - "value": "42" - }, - { - "name": "lng", - "value": "42" - } - ], - "url": "https://brokencrystals.com/locations?lat=42&lng=42" + "queryString": [], + "url": "https://brokencrystals.com/users/42" }, { "bodySize": 0, @@ -25,17 +16,8 @@ "headersSize": 0, "httpVersion": "HTTP/1.1", "method": "POST", - "queryString": [ - { - "name": "lat", - "value": "lorem" - }, - { - "name": "lng", - "value": "lorem" - } - ], - "url": "https://brokencrystals.com/locations?lat=lorem&lng=lorem" + "queryString": [], + "url": "https://brokencrystals.com/users/lorem" }, { "bodySize": 0, @@ -44,20 +26,7 @@ "headersSize": 0, "httpVersion": "HTTP/1.1", "method": "PUT", - "queryString": [ - { - "name": "lat", - "value": "42" - }, - { - "name": "lng", - "value": "42" - }, - { - "name": "type", - "value": "lorem" - } - ], - "url": "https://brokencrystals.com/locations?lat=42&lng=42&type=lorem" + "queryString": [], + "url": "https://brokencrystals.com/users/lorem" } ] diff --git a/packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml b/packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml index 1d166024..281a3796 100644 --- a/packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml +++ b/packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml @@ -1,18 +1,15 @@ openapi: 3.0.3 info: - title: locations + title: users/{id} version: '1.0' servers: - url: https://brokencrystals.com paths: - /locations: + /users/{id}: parameters: - - in: query - name: lat - schema: - type: number - - in: query - name: lng + - in: path + name: id + required: true schema: type: number get: @@ -21,27 +18,25 @@ paths: description: '' post: parameters: - - in: query - name: lat - schema: - type: string - - in: query - name: lng - schema: - type: string - in: path - name: lng + name: id + required: true schema: - type: integer + type: string responses: '200': description: '' put: parameters: - - in: query - name: type - schema: - type: string + - $ref: '#/components/parameters/userid' responses: '200': description: '' +components: + parameters: + userid: + in: path + name: id + required: true + schema: + type: string