Skip to content

Commit 991ae66

Browse files
committed
Fixes #3375 - Change order of logic for selecting requestContentType to prioritize consumes_value first
1 parent a5cab61 commit 991ae66

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

src/core/plugins/spec/selectors.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,14 @@ export function parametersIncludeType(parameters, typeValue="") {
277277
export function contentTypeValues(state, pathMethod) {
278278
let op = spec(state).getIn(["paths", ...pathMethod], fromJS({}))
279279
const parameters = op.get("parameters") || new List()
280+
280281
const requestContentType = (
281-
parametersIncludeType(parameters, "file") ? "multipart/form-data"
282-
: parametersIncludeIn(parameters, "formData") ? "application/x-www-form-urlencoded"
283-
: op.get("consumes_value")
282+
op.get("consumes_value") ? op.get("consumes_value")
283+
: parametersIncludeType(parameters, "file") ? "multipart/form-data"
284+
: parametersIncludeType(parameters, "formData") ? "application/x-www-form-urlencoded"
285+
: undefined
284286
)
285287

286-
287288
return fromJS({
288289
requestContentType,
289290
responseContentType: op.get("produces_value")

test/core/plugins/spec/selectors.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe("spec plugin - selectors", function(){
5252
})
5353

5454
describe("contentTypeValues", function(){
55-
5655
it("should return { requestContentType, responseContentType } from an operation", function(){
5756
// Given
5857
let state = fromJS({
@@ -77,6 +76,73 @@ describe("spec plugin - selectors", function(){
7776
})
7877
})
7978

79+
it("should prioritize consumes value first from an operation", function(){
80+
// Given
81+
let state = fromJS({
82+
resolved: {
83+
paths: {
84+
"/one": {
85+
get: {
86+
"consumes_value": "one",
87+
"parameters": [{
88+
"type": "file"
89+
}],
90+
}
91+
}
92+
}
93+
}
94+
})
95+
96+
// When
97+
let contentTypes = contentTypeValues(state, [ "/one", "get" ])
98+
// Then
99+
expect(contentTypes.toJS().requestContentType).toEqual("one")
100+
})
101+
102+
it("should fallback to multipart/form-data if there is no consumes value but there is a file parameter", function(){
103+
// Given
104+
let state = fromJS({
105+
resolved: {
106+
paths: {
107+
"/one": {
108+
get: {
109+
"parameters": [{
110+
"type": "file"
111+
}],
112+
}
113+
}
114+
}
115+
}
116+
})
117+
118+
// When
119+
let contentTypes = contentTypeValues(state, [ "/one", "get" ])
120+
// Then
121+
expect(contentTypes.toJS().requestContentType).toEqual("multipart/form-data")
122+
})
123+
124+
it("should fallback to application/x-www-form-urlencoded if there is no consumes value, no file parameter, but there is a formData parameter", function(){
125+
// Given
126+
let state = fromJS({
127+
resolved: {
128+
paths: {
129+
"/one": {
130+
get: {
131+
"parameters": [{
132+
"type": "formData"
133+
}],
134+
}
135+
}
136+
}
137+
}
138+
})
139+
140+
// When
141+
let contentTypes = contentTypeValues(state, [ "/one", "get" ])
142+
// Then
143+
expect(contentTypes.toJS().requestContentType).toEqual("application/x-www-form-urlencoded")
144+
})
145+
80146
it("should be ok, if no operation found", function(){
81147
// Given
82148
let state = fromJS({ })

0 commit comments

Comments
 (0)