Skip to content

Commit 971c6f7

Browse files
authored
fix: path-item $ref produces/consumes inheritance (via #5049)
* implement a selector for consumes options * fix incorrect comment, test names * add `consumesOptionsFor` selector * use `consumesOptionsFor` and drop `operationConsumes`
1 parent 2977c93 commit 971c6f7

File tree

7 files changed

+400
-39
lines changed

7 files changed

+400
-39
lines changed

src/core/components/operation.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export default class Operation extends PureComponent {
8383

8484
let operation = operationProps.getIn(["op"])
8585
let responses = operation.get("responses")
86-
let produces = operation.get("produces")
8786
let parameters = getList(operation, ["parameters"])
8887
let operationScheme = specSelectors.operationScheme(path, method)
8988
let isShownKey = ["operations", tag, operationId]
@@ -216,7 +215,7 @@ export default class Operation extends PureComponent {
216215
specSelectors={ specSelectors }
217216
oas3Actions={oas3Actions}
218217
specActions={ specActions }
219-
produces={ produces }
218+
produces={specSelectors.producesOptionsFor([path, method]) }
220219
producesValue={ specSelectors.currentProducesFor([path, method]) }
221220
specPath={specPath.push("responses")}
222221
path={ path }

src/core/components/parameter-row.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export default class ParameterRow extends Component {
129129
: <ParamBody getComponent={getComponent}
130130
fn={fn}
131131
param={param}
132-
consumes={ specSelectors.operationConsumes(pathMethod) }
132+
consumes={ specSelectors.consumesOptionsFor(pathMethod) }
133133
consumesValue={ specSelectors.contentTypeValues(pathMethod).get("requestContentType") }
134134
onChange={this.onChangeWrapper}
135135
onChangeConsumes={onChangeConsumes}

src/core/plugins/spec/selectors.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,6 @@ export function contentTypeValues(state, pathMethod) {
401401
})
402402
}
403403

404-
// Get the consumes/produces by path
405-
export function operationConsumes(state, pathMethod) {
406-
pathMethod = pathMethod || []
407-
return specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "consumes"], fromJS({}))
408-
}
409-
410404
// Get the currently selected produces value for an operation
411405
export function currentProducesFor(state, pathMethod) {
412406
pathMethod = pathMethod || []
@@ -425,6 +419,48 @@ export function currentProducesFor(state, pathMethod) {
425419

426420
}
427421

422+
// Get the produces options for an operation
423+
export function producesOptionsFor(state, pathMethod) {
424+
pathMethod = pathMethod || []
425+
426+
const spec = specJsonWithResolvedSubtrees(state)
427+
const operation = spec.getIn([ "paths", ...pathMethod], null)
428+
429+
if(operation === null) {
430+
// return nothing if the operation does not exist
431+
return
432+
}
433+
434+
const [path] = pathMethod
435+
436+
const operationProduces = operation.get("produces", null)
437+
const pathItemProduces = spec.getIn(["paths", path, "produces"], null)
438+
const globalProduces = spec.getIn(["produces"], null)
439+
440+
return operationProduces || pathItemProduces || globalProduces
441+
}
442+
443+
// Get the consumes options for an operation
444+
export function consumesOptionsFor(state, pathMethod) {
445+
pathMethod = pathMethod || []
446+
447+
const spec = specJsonWithResolvedSubtrees(state)
448+
const operation = spec.getIn(["paths", ...pathMethod], null)
449+
450+
if (operation === null) {
451+
// return nothing if the operation does not exist
452+
return
453+
}
454+
455+
const [path] = pathMethod
456+
457+
const operationConsumes = operation.get("consumes", null)
458+
const pathItemConsumes = spec.getIn(["paths", path, "consumes"], null)
459+
const globalConsumes = spec.getIn(["consumes"], null)
460+
461+
return operationConsumes || pathItemConsumes || globalConsumes
462+
}
463+
428464
export const operationScheme = ( state, path, method ) => {
429465
let url = state.get("url")
430466
let matchResult = url.match(/^([a-z][a-z0-9+\-.]*):/)

0 commit comments

Comments
 (0)