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(execute): explode array parameters for combined schemas #3826

Merged
merged 5 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 35 additions & 13 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import { serialize as serializeCookie } from '../helpers/cookie.js';

const arrayOrEmpty = (ar) => (Array.isArray(ar) ? ar : []);

const findObjectSchema = (schema, { recurse = true, depth = 1 } = {}) => {
const findObjectOrArraySchema = (schema, { recurse = true, depth = 1 } = {}) => {
if (!isPlainObject(schema)) return undefined;

// check if the schema is an object type
if (schema.type === 'object' || (Array.isArray(schema.type) && schema.type.includes('object'))) {
// check if the schema is an object or array type
if (
schema.type === 'object' ||
schema.type === 'array' ||
(Array.isArray(schema.type) &&
(schema.type.includes('object') || schema.type.includes('array')))
) {
return schema;
}

Expand All @@ -33,14 +38,18 @@ const findObjectSchema = (schema, { recurse = true, depth = 1 } = {}) => {
if (recurse) {
// traverse oneOf keyword first
const oneOfResult = Array.isArray(schema.oneOf)
? schema.oneOf.find((subschema) => findObjectSchema(subschema, { recurse, depth: depth + 1 }))
? schema.oneOf.find((subschema) =>
findObjectOrArraySchema(subschema, { recurse, depth: depth + 1 })
)
: undefined;

if (oneOfResult) return oneOfResult;

// traverse anyOf keyword second
const anyOfResult = Array.isArray(schema.anyOf)
? schema.anyOf.find((subschema) => findObjectSchema(subschema, { recurse, depth: depth + 1 }))
? schema.anyOf.find((subschema) =>
findObjectOrArraySchema(subschema, { recurse, depth: depth + 1 })
)
: undefined;

if (anyOfResult) return anyOfResult;
Expand All @@ -49,18 +58,18 @@ const findObjectSchema = (schema, { recurse = true, depth = 1 } = {}) => {
return undefined;
};

const parseJsonObject = ({ value, silentFail = false }) => {
const parseJsonObjectOrArray = ({ value, silentFail = false }) => {
try {
const parsedValue = JSON.parse(value);
if (typeof parsedValue === 'object') {
if (isPlainObject(parsedValue) || Array.isArray(parsedValue)) {
return parsedValue;
}
if (!silentFail) {
throw new Error('Expected JSON serialized object');
throw new Error('Expected JSON serialized object or array');
}
} catch {
if (!silentFail) {
throw new Error('Could not parse object parameter value string as JSON Object');
throw new Error('Could not parse parameter value string as JSON Object or JSON Array');
}
}
return value;
Expand Down Expand Up @@ -303,10 +312,23 @@ export function buildRequest(options) {
}

if (specIsOAS3 && typeof value === 'string') {
if (has('type', parameter.schema) && findObjectSchema(parameter.schema, { recurse: false })) {
value = parseJsonObject({ value, silentFail: false });
} else if (findObjectSchema(parameter.schema, { recurse: true })) {
value = parseJsonObject({ value, silentFail: true });
if (
has('type', parameter.schema) &&
typeof parameter.schema.type === 'string' &&
findObjectOrArraySchema(parameter.schema, { recurse: false })
) {
value = parseJsonObjectOrArray({ value, silentFail: false });
} else if (
has('type', parameter.schema) &&
Array.isArray(parameter.schema.type) &&
findObjectOrArraySchema(parameter.schema, { recurse: false })
) {
value = parseJsonObjectOrArray({ value, silentFail: true });
} else if (
!has('type', parameter.schema) &&
findObjectOrArraySchema(parameter.schema, { recurse: true })
) {
value = parseJsonObjectOrArray({ value, silentFail: true });
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/execute/openapi-3-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('given OpenAPI 3.1.0 definition', () => {
const response = await SwaggerClient.execute({
spec,
operationId: 'getUserList',
parameters: { q: 'search string' },
parameters: { q: ['search string'] },
securities: { authorized: { BearerAuth: '3492342948239482398' } },
});

Expand All @@ -150,7 +150,7 @@ describe('given OpenAPI 3.1.0 definition', () => {
spec,
pathName: '/users',
method: 'get',
parameters: { q: 'search string' },
parameters: { q: ['search string'] },
securities: { authorized: { BearerAuth: '3492342948239482398' } },
});

Expand All @@ -164,7 +164,7 @@ describe('given OpenAPI 3.1.0 definition', () => {
});
const response = await client.execute({
operationId: 'getUserList',
parameters: { q: 'search string' },
parameters: { q: ['search string'] },
});

expect(response.body).toEqual([{ id: 'value' }]);
Expand All @@ -174,7 +174,7 @@ describe('given OpenAPI 3.1.0 definition', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'getUserList',
parameters: { q: 'search string' },
parameters: { q: ['search string'] },
securities: { authorized: { BearerAuth: '3492342948239482398' } },
responseContentType: 'application/json',
});
Expand All @@ -195,7 +195,7 @@ describe('given OpenAPI 3.1.0 definition', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'getUserList',
parameters: { q: 'search string' },
parameters: { q: ['search string'] },
securities: { authorized: { BearerAuth: '3492342948239482398' } },
responseContentType: 'application/json',
baseURL,
Expand Down
76 changes: 76 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,82 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
});

describe('`schema` parameters', () => {
test('should build a request when parameter type is `string` and value is not a stringified object', () => {
// Given
const spec = {
openapi: '3.1.0',
paths: {
'/users': {
get: {
operationId: 'myOperation',
parameters: [
{
in: 'query',
name: 'parameters',
schema: {
type: 'string',
},
},
],
},
},
},
};
// when
const req = buildRequest({
spec,
operationId: 'myOperation',
parameters: {
parameters: 'test',
},
});

expect(req).toEqual({
method: 'GET',
url: `/users?parameters=test`,
credentials: 'same-origin',
headers: {},
});
});

it('should build a request when parameter type is `string` and value is a stringified object', () => {
// Given
const spec = {
openapi: '3.1.0',
paths: {
'/users': {
get: {
operationId: 'myOperation',
parameters: [
{
in: 'query',
name: 'parameters',
schema: {
type: 'string',
},
},
],
},
},
},
};
// when
const req = buildRequest({
spec,
operationId: 'myOperation',
parameters: {
parameters: '{"bar":{"baz":"qux"}}',
},
});

expect(req).toEqual({
method: 'GET',
url: `/users?parameters=${escape('{"bar":{"baz":"qux"}}')}`,
credentials: 'same-origin',
headers: {},
});
});

it('should encode JSON values provided as objects', () => {
const req = buildRequest({
spec: {
Expand Down
Loading