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

fix(oas): combine PathItemObject and OperationObject parameters #249

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions packages/oas/src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -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);

ostridm marked this conversation as resolved.
Show resolved Hide resolved
const combinedParams = new Map<string, ParameterObject>(
pathParams.map((x) => [`${x.in}:${x.name}`, x])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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"
ostridm marked this conversation as resolved.
Show resolved Hide resolved
}
]
39 changes: 17 additions & 22 deletions packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Loading