From 991ae66b3951fd6455d0604b4f9f9b3c4a16204b Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Wed, 12 Jul 2017 19:56:11 -0600 Subject: [PATCH] Fixes #3375 - Change order of logic for selecting `requestContentType` to prioritize `consumes_value` first --- src/core/plugins/spec/selectors.js | 9 ++-- test/core/plugins/spec/selectors.js | 68 ++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/core/plugins/spec/selectors.js b/src/core/plugins/spec/selectors.js index a4f94349..cd7e722b 100644 --- a/src/core/plugins/spec/selectors.js +++ b/src/core/plugins/spec/selectors.js @@ -277,13 +277,14 @@ export function parametersIncludeType(parameters, typeValue="") { export function contentTypeValues(state, pathMethod) { let op = spec(state).getIn(["paths", ...pathMethod], fromJS({})) const parameters = op.get("parameters") || new List() + const requestContentType = ( - parametersIncludeType(parameters, "file") ? "multipart/form-data" - : parametersIncludeIn(parameters, "formData") ? "application/x-www-form-urlencoded" - : op.get("consumes_value") + op.get("consumes_value") ? op.get("consumes_value") + : parametersIncludeType(parameters, "file") ? "multipart/form-data" + : parametersIncludeType(parameters, "formData") ? "application/x-www-form-urlencoded" + : undefined ) - return fromJS({ requestContentType, responseContentType: op.get("produces_value") diff --git a/test/core/plugins/spec/selectors.js b/test/core/plugins/spec/selectors.js index 93abd025..7519a42e 100644 --- a/test/core/plugins/spec/selectors.js +++ b/test/core/plugins/spec/selectors.js @@ -52,7 +52,6 @@ describe("spec plugin - selectors", function(){ }) describe("contentTypeValues", function(){ - it("should return { requestContentType, responseContentType } from an operation", function(){ // Given let state = fromJS({ @@ -77,6 +76,73 @@ describe("spec plugin - selectors", function(){ }) }) + it("should prioritize consumes value first from an operation", function(){ + // Given + let state = fromJS({ + resolved: { + paths: { + "/one": { + get: { + "consumes_value": "one", + "parameters": [{ + "type": "file" + }], + } + } + } + } + }) + + // When + let contentTypes = contentTypeValues(state, [ "/one", "get" ]) + // Then + expect(contentTypes.toJS().requestContentType).toEqual("one") + }) + + it("should fallback to multipart/form-data if there is no consumes value but there is a file parameter", function(){ + // Given + let state = fromJS({ + resolved: { + paths: { + "/one": { + get: { + "parameters": [{ + "type": "file" + }], + } + } + } + } + }) + + // When + let contentTypes = contentTypeValues(state, [ "/one", "get" ]) + // Then + expect(contentTypes.toJS().requestContentType).toEqual("multipart/form-data") + }) + + 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(){ + // Given + let state = fromJS({ + resolved: { + paths: { + "/one": { + get: { + "parameters": [{ + "type": "formData" + }], + } + } + } + } + }) + + // When + let contentTypes = contentTypeValues(state, [ "/one", "get" ]) + // Then + expect(contentTypes.toJS().requestContentType).toEqual("application/x-www-form-urlencoded") + }) + it("should be ok, if no operation found", function(){ // Given let state = fromJS({ })