Skip to content
Open
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
8 changes: 6 additions & 2 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ export default class ParameterRow extends Component {
let { onChange, rawParam } = this.props
let valueForUpstream

// Coerce empty strings and empty Immutable objects to null
if(value === "" || (value && value.size === 0)) {
// Coerce empty strings, empty Immutable objects, and empty arrays to null
if(
value === "" ||
(value && value.size === 0) ||
(Array.isArray(value) && value.length === 0)
) {
valueForUpstream = null
} else {
valueForUpstream = value
Expand Down
8 changes: 6 additions & 2 deletions src/core/plugins/spec/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,12 @@ export const executeRequest = (req) =>
req.parameters = req.parameters || {}
const paramValue = paramToValue(param, req.parameters)

// if the value is falsy or an empty Immutable iterable...
if(!paramValue || (paramValue && paramValue.size === 0)) {
// if the value is falsy, an empty Immutable iterable, or an empty array...
if(
!paramValue ||
(paramValue && paramValue.size === 0) ||
(Array.isArray(paramValue) && paramValue.length === 0)
) {
// set it to empty string, so Swagger Client will treat it as
// present but empty.
req.parameters[param.get("name")] = ""
Expand Down
17 changes: 17 additions & 0 deletions test/unit/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ describe("<ParameterRow/>", () => {
expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("boolean")
})

it("coerces empty array parameter values to null", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace this unit test with an e2e test (located in test/e2e-cypress/e2e/features) covering:

  1. Checking Send empty value and executing the request before selecting anything
  2. Checking Send empty value and executing the request after selecting and deselecting a value?

const param = fromJS({
name: "status",
in: "query",
value: ["available"],
})
const props = {
...createProps({ param, isOAS3: true }),
onChange: jest.fn(),
}
const parameterRow = new ParameterRow(props)

parameterRow.onChangeWrapper([])

expect(props.onChange).toHaveBeenCalledWith(param, null, false)
})
})

describe("bug #5573: zero default and example values", function () {
Expand Down
59 changes: 59 additions & 0 deletions test/unit/core/plugins/spec/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,65 @@ describe("spec plugin - actions", function(){
expect(system.specActions.setMutatedRequest.mock.calls.length).toEqual(1)
expect(system.specActions.setRequest.mock.calls.length).toEqual(1)
})

it("should send explicitly included empty array parameters as empty strings", async () => {
// Given
const system = {
fn: {
buildRequest: jest.fn(req => req),
execute: jest.fn().mockImplementation(() => Promise.resolve({}))
},
specActions: {
setMutatedRequest: jest.fn(),
setRequest: jest.fn(),
setResponse: jest.fn()
},
specSelectors: {
parameterInclusionSettingFor: () => true,
url: () => "http://example.com/openapi.json",
isOAS3: () => false
},
getConfigs: () => ({
requestInterceptor: jest.fn(),
responseInterceptor: jest.fn()
})
}
// When
let executeFn = executeRequest({
pathName: "/pets",
method: "GET",
operation: fromJS({
operationId: "findPets",
parameters: [
{
name: "status",
in: "query",
allowEmptyValue: true
}
]
}),
parameters: {
status: []
}
})
await executeFn(system)

// Then
expect(system.fn.buildRequest).toHaveBeenCalledWith(
expect.objectContaining({
parameters: {
status: ""
}
})
)
expect(system.fn.execute).toHaveBeenCalledWith(
expect.objectContaining({
parameters: {
status: ""
}
})
)
})
})

xit("should call specActions.setResponse, when fn.execute resolves", function(){
Expand Down