Skip to content

Commit 832f5e4

Browse files
authored
Merge pull request #4004 from shockey/bug/3982-oas3-trace-method
OAS3 `TRACE` method operation display
2 parents 3d8f500 + 70ba6a9 commit 832f5e4

File tree

3 files changed

+142
-5
lines changed

3 files changed

+142
-5
lines changed

src/core/components/operations.jsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import React from "react"
22
import PropTypes from "prop-types"
33
import { createDeepLinkPath, sanitizeUrl } from "core/utils"
44

5+
const SWAGGER2_OPERATION_METHODS = [
6+
"get", "put", "post", "delete", "options", "head", "patch"
7+
]
8+
9+
const OAS3_OPERATION_METHODS = SWAGGER2_OPERATION_METHODS.concat(["trace"])
10+
11+
512
export default class Operations extends React.Component {
613

714
static propTypes = {
@@ -113,6 +120,18 @@ export default class Operations extends React.Component {
113120
const path = op.get("path")
114121
const method = op.get("method")
115122

123+
// FIXME: (someday) this logic should probably be in a selector,
124+
// but doing so would require further opening up
125+
// selectors to the plugin system, to allow for dynamic
126+
// overriding of low-level selectors that other selectors
127+
// rely on. --KS, 12/17
128+
const validMethods = specSelectors.isOAS3() ?
129+
OAS3_OPERATION_METHODS : SWAGGER2_OPERATION_METHODS
130+
131+
if(validMethods.indexOf(method) === -1) {
132+
return null
133+
}
134+
116135
return <OperationContainer
117136
key={`${path}-${method}`}
118137
op={op}

src/core/plugins/spec/selectors.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { fromJS, Set, Map, OrderedMap, List } from "immutable"
44

55
const DEFAULT_TAG = "default"
66

7-
const OPERATION_METHODS = ["get", "put", "post", "delete", "options", "head", "patch"]
8-
97
const state = state => {
108
return state || Map()
119
}
@@ -97,9 +95,6 @@ export const operations = createSelector(
9795
return {}
9896
}
9997
path.forEach((operation, method) => {
100-
if(OPERATION_METHODS.indexOf(method) === -1) {
101-
return
102-
}
10398
list = list.push(fromJS({
10499
path: pathName,
105100
method,

test/components/operations.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* eslint-env mocha */
2+
import React from "react"
3+
import expect, { createSpy } from "expect"
4+
import { render } from "enzyme"
5+
import { fromJS } from "immutable"
6+
import Operations from "components/operations"
7+
import {Collapse} from "components/layout-utils"
8+
9+
const components = {
10+
Collapse,
11+
OperationContainer: ({ path, method }) => <span className="mocked-op" id={`${path}-${method}`} />
12+
}
13+
14+
describe("<Operations/>", function(){
15+
it("should render a Swagger2 `get` method, but not a `trace` or `foo` method", function(){
16+
17+
let props = {
18+
fn: {},
19+
specActions: {},
20+
layoutActions: {},
21+
getComponent: (name)=> {
22+
return components[name] || null
23+
},
24+
getConfigs: () => {
25+
return {}
26+
},
27+
specSelectors: {
28+
isOAS3() { return false },
29+
taggedOperations() {
30+
return fromJS({
31+
"default": {
32+
"operations": [
33+
{
34+
"path": "/pets/{id}",
35+
"method": "get"
36+
},
37+
{
38+
"path": "/pets/{id}",
39+
"method": "trace"
40+
},
41+
{
42+
"path": "/pets/{id}",
43+
"method": "foo"
44+
},
45+
]
46+
}
47+
})
48+
},
49+
},
50+
layoutSelectors: {
51+
currentFilter() {
52+
return null
53+
},
54+
isShown() {
55+
return true
56+
},
57+
show() {
58+
return true
59+
}
60+
}
61+
}
62+
63+
let wrapper = render(<Operations {...props}/>)
64+
65+
expect(wrapper.find("span.mocked-op").length).toEqual(1)
66+
expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get")
67+
})
68+
69+
it("should render an OAS3 `get` and `trace` method, but not a `foo` method", function(){
70+
71+
let props = {
72+
fn: {},
73+
specActions: {},
74+
layoutActions: {},
75+
getComponent: (name)=> {
76+
return components[name] || null
77+
},
78+
getConfigs: () => {
79+
return {}
80+
},
81+
specSelectors: {
82+
isOAS3() { return true },
83+
taggedOperations() {
84+
return fromJS({
85+
"default": {
86+
"operations": [
87+
{
88+
"path": "/pets/{id}",
89+
"method": "get"
90+
},
91+
{
92+
"path": "/pets/{id}",
93+
"method": "trace"
94+
},
95+
{
96+
"path": "/pets/{id}",
97+
"method": "foo"
98+
},
99+
]
100+
}
101+
})
102+
},
103+
},
104+
layoutSelectors: {
105+
currentFilter() {
106+
return null
107+
},
108+
isShown() {
109+
return true
110+
},
111+
show() {
112+
return true
113+
}
114+
}
115+
}
116+
117+
let wrapper = render(<Operations {...props}/>)
118+
119+
expect(wrapper.find("span.mocked-op").length).toEqual(2)
120+
expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get")
121+
expect(wrapper.find("span.mocked-op").eq(1).attr("id")).toEqual("/pets/{id}-trace")
122+
})
123+
})

0 commit comments

Comments
 (0)