Skip to content

Commit

Permalink
fix(execute): skip undefined parameters in body and path request buil…
Browse files Browse the repository at this point in the history
…ders for OpenAPI 2.0 (#3439)

Refs #3438
  • Loading branch information
glowcloud authored Mar 26, 2024
1 parent 17732ef commit 1db6ba3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/execute/swagger2/parameter-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default {

// Add the body to the request
function bodyBuilder({ req, value }) {
req.body = value;
if (value !== undefined) {
req.body = value;
}
}

// Add a form data object.
Expand Down Expand Up @@ -48,7 +50,9 @@ function headerBuilder({ req, parameter, value }) {

// Replace path paramters, with values ( ie: the URL )
function pathBuilder({ req, value, parameter }) {
req.url = req.url.split(`{${parameter.name}}`).join(encodeURIComponent(value));
if (value !== undefined) {
req.url = req.url.replace(new RegExp(`{${parameter.name}}`, 'g'), encodeURIComponent(value));
}
}

// Add a query to the `query` object, which will later be stringified into the URL's search
Expand Down
57 changes: 54 additions & 3 deletions test/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,13 +1739,29 @@ describe('execute', () => {
});
});

test('should generate a request with an empty body parameter', () => {
test('should not generate a request with an empty body parameter', () => {
const req = buildRequest({ spec, operationId: 'postMe', parameters: {} });

expect(req).toEqual({
expect(req).toStrictEqual({
url: 'http://swagger.io/v1/one',
method: 'POST',
credentials: 'same-origin',
headers: {},
});
});

test('should not generate a request with an undefined body parameter', () => {
const req = buildRequest({
spec,
operationId: 'postMe',
parameters: {
bodyParam: undefined,
},
});

expect(req).toStrictEqual({
url: 'http://swagger.io/v1/one',
method: 'POST',
body: undefined,
credentials: 'same-origin',
headers: {},
});
Expand Down Expand Up @@ -1966,6 +1982,41 @@ describe('execute', () => {
});
});

test('should not replace path parameters with undefined values', () => {
const spec = {
host: 'swagger.io',
basePath: '/v1',
paths: {
'/{id}/{status}': {
get: {
operationId: 'getMe',
parameters: [
{
in: 'path',
name: 'id',
type: 'number',
},
{
in: 'path',
name: 'status',
type: 'string',
},
],
},
},
},
};

const req = buildRequest({ spec, operationId: 'getMe', parameters: { id: undefined } });

expect(req).toEqual({
url: 'http://swagger.io/v1/{id}/{status}',
method: 'GET',
credentials: 'same-origin',
headers: {},
});
});

test('should merge Path and Operation parameters', () => {
const spec = {
host: 'swagger.io',
Expand Down

0 comments on commit 1db6ba3

Please sign in to comment.