fix: non-typesafe spec selector (via #5121)
* add failing tests * fix things
This commit is contained in:
@@ -46,7 +46,10 @@ const spec = state => {
|
|||||||
|
|
||||||
export const definitions = onlyOAS3(createSelector(
|
export const definitions = onlyOAS3(createSelector(
|
||||||
spec,
|
spec,
|
||||||
spec => spec.getIn(["components", "schemas"]) || Map()
|
spec => {
|
||||||
|
const res = spec.getIn(["components", "schemas"])
|
||||||
|
return Map.isMap(res) ? res : Map()
|
||||||
|
}
|
||||||
))
|
))
|
||||||
|
|
||||||
export const hasHost = onlyOAS3((state) => {
|
export const hasHost = onlyOAS3((state) => {
|
||||||
|
|||||||
@@ -175,7 +175,10 @@ export const findDefinition = ( state, name ) => {
|
|||||||
|
|
||||||
export const definitions = createSelector(
|
export const definitions = createSelector(
|
||||||
spec,
|
spec,
|
||||||
spec => spec.get("definitions") || Map()
|
spec => {
|
||||||
|
const res = spec.get("definitions")
|
||||||
|
return Map.isMap(res) ? res : Map()
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export const basePath = createSelector(
|
export const basePath = createSelector(
|
||||||
|
|||||||
99
test/core/plugins/oas3/wrap-spec-selectors.js
Normal file
99
test/core/plugins/oas3/wrap-spec-selectors.js
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
import expect, { createSpy } from "expect"
|
||||||
|
import { Map, fromJS } from "immutable"
|
||||||
|
import {
|
||||||
|
definitions
|
||||||
|
} from "corePlugins/oas3/spec-extensions/wrap-selectors"
|
||||||
|
|
||||||
|
describe("oas3 plugin - spec extensions - wrapSelectors", function(){
|
||||||
|
|
||||||
|
describe("definitions", function(){
|
||||||
|
it("should return definitions by default", function () {
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
openapi: "3.0.0",
|
||||||
|
components: {
|
||||||
|
schemas: {
|
||||||
|
a: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const system = {
|
||||||
|
getSystem: () => system,
|
||||||
|
specSelectors: {
|
||||||
|
specJson: () => spec,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(() => null, system)(fromJS({
|
||||||
|
json: spec
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({
|
||||||
|
a: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it("should return an empty Map when missing definitions", function () {
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
openapi: "3.0.0"
|
||||||
|
})
|
||||||
|
|
||||||
|
const system = {
|
||||||
|
getSystem: () => system,
|
||||||
|
specSelectors: {
|
||||||
|
specJson: () => spec,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(() => null, system)(fromJS({
|
||||||
|
json: spec
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({})
|
||||||
|
})
|
||||||
|
it("should return an empty Map when given non-object definitions", function () {
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
openapi: "3.0.0",
|
||||||
|
components: {
|
||||||
|
schemas: "..."
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const system = {
|
||||||
|
getSystem: () => system,
|
||||||
|
specSelectors: {
|
||||||
|
specJson: () => spec,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(() => null, system)(fromJS({
|
||||||
|
json: spec
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
@@ -3,6 +3,7 @@ import expect from "expect"
|
|||||||
import { fromJS } from "immutable"
|
import { fromJS } from "immutable"
|
||||||
import { fromJSOrdered } from "core/utils"
|
import { fromJSOrdered } from "core/utils"
|
||||||
import {
|
import {
|
||||||
|
definitions,
|
||||||
parameterValues,
|
parameterValues,
|
||||||
contentTypeValues,
|
contentTypeValues,
|
||||||
operationScheme,
|
operationScheme,
|
||||||
@@ -21,6 +22,70 @@ import {
|
|||||||
|
|
||||||
describe("spec plugin - selectors", function(){
|
describe("spec plugin - selectors", function(){
|
||||||
|
|
||||||
|
describe("definitions", function(){
|
||||||
|
it("should return definitions by default", function(){
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
json: {
|
||||||
|
swagger: "2.0",
|
||||||
|
definitions: {
|
||||||
|
a: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(spec)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({
|
||||||
|
a: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it("should return an empty Map when missing definitions", function(){
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
json: {
|
||||||
|
swagger: "2.0"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(spec)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({})
|
||||||
|
})
|
||||||
|
it("should return an empty Map when given non-object definitions", function(){
|
||||||
|
|
||||||
|
// Given
|
||||||
|
const spec = fromJS({
|
||||||
|
json: {
|
||||||
|
swagger: "2.0",
|
||||||
|
definitions: "..."
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// When
|
||||||
|
let res = definitions(spec)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(res.toJS()).toEqual({})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("parameterValue", function(){
|
describe("parameterValue", function(){
|
||||||
|
|
||||||
it("should return Map({}) if no path found", function(){
|
it("should return Map({}) if no path found", function(){
|
||||||
|
|||||||
Reference in New Issue
Block a user