Skip to content

Commit fbc817e

Browse files
shockeydeepjia
authored andcommitted
fix: non-typesafe spec selector (via swagger-api#5121)
* add failing tests * fix things
1 parent b330c1a commit fbc817e

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
lines changed

src/core/plugins/oas3/spec-extensions/wrap-selectors.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ const spec = state => {
4646

4747
export const definitions = onlyOAS3(createSelector(
4848
spec,
49-
spec => spec.getIn(["components", "schemas"]) || Map()
49+
spec => {
50+
const res = spec.getIn(["components", "schemas"])
51+
return Map.isMap(res) ? res : Map()
52+
}
5053
))
5154

5255
export const hasHost = onlyOAS3((state) => {

src/core/plugins/spec/selectors.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ export const findDefinition = ( state, name ) => {
175175

176176
export const definitions = createSelector(
177177
spec,
178-
spec => spec.get("definitions") || Map()
178+
spec => {
179+
const res = spec.get("definitions")
180+
return Map.isMap(res) ? res : Map()
181+
}
179182
)
180183

181184
export const basePath = createSelector(
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-env mocha */
2+
import expect, { createSpy } from "expect"
3+
import { Map, fromJS } from "immutable"
4+
import {
5+
definitions
6+
} from "corePlugins/oas3/spec-extensions/wrap-selectors"
7+
8+
describe("oas3 plugin - spec extensions - wrapSelectors", function(){
9+
10+
describe("definitions", function(){
11+
it("should return definitions by default", function () {
12+
13+
// Given
14+
const spec = fromJS({
15+
openapi: "3.0.0",
16+
components: {
17+
schemas: {
18+
a: {
19+
type: "string"
20+
},
21+
b: {
22+
type: "string"
23+
}
24+
}
25+
}
26+
})
27+
28+
const system = {
29+
getSystem: () => system,
30+
specSelectors: {
31+
specJson: () => spec,
32+
}
33+
}
34+
35+
// When
36+
let res = definitions(() => null, system)(fromJS({
37+
json: spec
38+
}))
39+
40+
// Then
41+
expect(res.toJS()).toEqual({
42+
a: {
43+
type: "string"
44+
},
45+
b: {
46+
type: "string"
47+
}
48+
})
49+
})
50+
it("should return an empty Map when missing definitions", function () {
51+
52+
// Given
53+
const spec = fromJS({
54+
openapi: "3.0.0"
55+
})
56+
57+
const system = {
58+
getSystem: () => system,
59+
specSelectors: {
60+
specJson: () => spec,
61+
}
62+
}
63+
64+
// When
65+
let res = definitions(() => null, system)(fromJS({
66+
json: spec
67+
}))
68+
69+
// Then
70+
expect(res.toJS()).toEqual({})
71+
})
72+
it("should return an empty Map when given non-object definitions", function () {
73+
74+
// Given
75+
const spec = fromJS({
76+
openapi: "3.0.0",
77+
components: {
78+
schemas: "..."
79+
}
80+
})
81+
82+
const system = {
83+
getSystem: () => system,
84+
specSelectors: {
85+
specJson: () => spec,
86+
}
87+
}
88+
89+
// When
90+
let res = definitions(() => null, system)(fromJS({
91+
json: spec
92+
}))
93+
94+
// Then
95+
expect(res.toJS()).toEqual({})
96+
})
97+
})
98+
99+
})

test/core/plugins/spec/selectors.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import expect from "expect"
33
import { fromJS } from "immutable"
44
import { fromJSOrdered } from "core/utils"
55
import {
6+
definitions,
67
parameterValues,
78
contentTypeValues,
89
operationScheme,
@@ -21,6 +22,70 @@ import {
2122

2223
describe("spec plugin - selectors", function(){
2324

25+
describe("definitions", function(){
26+
it("should return definitions by default", function(){
27+
28+
// Given
29+
const spec = fromJS({
30+
json: {
31+
swagger: "2.0",
32+
definitions: {
33+
a: {
34+
type: "string"
35+
},
36+
b: {
37+
type: "string"
38+
}
39+
}
40+
}
41+
})
42+
43+
// When
44+
let res = definitions(spec)
45+
46+
// Then
47+
expect(res.toJS()).toEqual({
48+
a: {
49+
type: "string"
50+
},
51+
b: {
52+
type: "string"
53+
}
54+
})
55+
})
56+
it("should return an empty Map when missing definitions", function(){
57+
58+
// Given
59+
const spec = fromJS({
60+
json: {
61+
swagger: "2.0"
62+
}
63+
})
64+
65+
// When
66+
let res = definitions(spec)
67+
68+
// Then
69+
expect(res.toJS()).toEqual({})
70+
})
71+
it("should return an empty Map when given non-object definitions", function(){
72+
73+
// Given
74+
const spec = fromJS({
75+
json: {
76+
swagger: "2.0",
77+
definitions: "..."
78+
}
79+
})
80+
81+
// When
82+
let res = definitions(spec)
83+
84+
// Then
85+
expect(res.toJS()).toEqual({})
86+
})
87+
})
88+
2489
describe("parameterValue", function(){
2590

2691
it("should return Map({}) if no path found", function(){

0 commit comments

Comments
 (0)