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 2 commits
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
20 changes: 15 additions & 5 deletions packages/oas/src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import type { ParameterObject } from '../types';
import { getOperation } from './operation';
import type { OpenAPI } from '@har-sdk/core';

const isParameter = (param: unknown): param is ParameterObject =>
param && typeof param === 'object' && 'in' in param && 'name' in param;
ostridm marked this conversation as resolved.
Show resolved Hide resolved

const toParameters = (param: unknown): ParameterObject[] =>
ostridm marked this conversation as resolved.
Show resolved Hide resolved
Array.isArray(param) ? param.filter(isParameter) : [];

export const getParameters = (
spec: OpenAPI.Document,
path: string,
method: string
): ParameterObject[] => {
const pathObj = getOperation(spec, path, method);
const pathParams = toParameters(spec.paths[path]?.parameters);
const operationParams = toParameters(spec.paths[path]?.[method]?.parameters);
ostridm marked this conversation as resolved.
Show resolved Hide resolved

const combinedParams = new Map<string, ParameterObject>(
pathParams.map((x) => [`${x.in}:${x.name}`, x])
);

operationParams.forEach((x) => combinedParams.set(`${x.in}:${x.name}`, x));

return Array.isArray(pathObj.parameters)
? (pathObj.parameters as ParameterObject[])
: [];
return [...combinedParams.values()];
};

export const filterLocationParams = (
Expand Down
14 changes: 14 additions & 0 deletions packages/oas/tests/DefaultConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,19 @@ describe('DefaultConverter', () => {
// assert
expect(result).toStrictEqual(expectedDoc);
});

it('should correctly resolve path-item parameters', async () => {
// arrange
const { inputDoc, expectedDoc } = await createFixture({
inputFile: `./fixtures/path-item.params.resolution.oas.yaml`,
expectedFile: `./fixtures/path-item.params.resolution.oas.result.json`
});

// act
const result: Request[] = await oas2har(inputDoc as any);

// assert
expect(result).toStrictEqual(expectedDoc);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[
{
"bodySize": 0,
"cookies": [],
"headers": [],
"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"
},
{
"bodySize": 0,
"cookies": [],
"headers": [],
"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"
},
{
"bodySize": 0,
"cookies": [],
"headers": [],
"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"
}
]
47 changes: 47 additions & 0 deletions packages/oas/tests/fixtures/path-item.params.resolution.oas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
openapi: 3.0.3
info:
title: locations
version: '1.0'
servers:
- url: https://brokencrystals.com
paths:
/locations:
ostridm marked this conversation as resolved.
Show resolved Hide resolved
parameters:
- in: query
name: lat
schema:
type: number
- in: query
name: lng
schema:
type: number
get:
responses:
'200':
description: ''
post:
parameters:
- in: query
name: lat
schema:
type: string
- in: query
name: lng
schema:
type: string
- in: path
name: lng
schema:
type: integer
responses:
'200':
description: ''
put:
parameters:
- in: query
name: type
schema:
type: string
responses:
'200':
description: ''
Loading