fix: gracefully handle malformed global tags array in taggedOperations selector (via #5159)

* fix: handle malformed global tags array in taggedOperations

* handle non-array global tags as well

* update test imports

* remove stray brackets
This commit is contained in:
kyle
2019-02-05 20:10:18 -06:00
committed by GitHub
parent 52ce2871a2
commit b716ed2515
2 changed files with 174 additions and 9 deletions

View File

@@ -225,7 +225,10 @@ export const operationsWithRootInherited = createSelector(
export const tags = createSelector(
spec,
json => json.get("tags", List())
json => {
const tags = json.get("tags", List())
return List.isList(tags) ? tags.filter(tag => Map.isMap(tag)) : List()
}
)
export const tagDetails = (state, tag) => {

View File

@@ -9,18 +9,15 @@ import {
operationScheme,
specJsonWithResolvedSubtrees,
producesOptionsFor,
} from "corePlugins/spec/selectors"
import Petstore from "./assets/petstore.json"
import {
operationWithMeta,
parameterWithMeta,
parameterWithMetaByIdentity,
parameterInclusionSettingFor,
consumesOptionsFor
} from "../../../../src/core/plugins/spec/selectors"
consumesOptionsFor,
taggedOperations
} from "corePlugins/spec/selectors"
describe("spec plugin - selectors", function(){
import Petstore from "./assets/petstore.json"
describe("definitions", function(){
it("should return definitions by default", function(){
@@ -1049,4 +1046,169 @@ describe("spec plugin - selectors", function(){
])
})
})
})
describe("taggedOperations", function () {
it("should return a List of ad-hoc tagged operations", function () {
const system = {
getConfigs: () => ({})
}
const state = fromJS({
json: {
// tags: [
// "myTag"
// ],
paths: {
"/": {
"get": {
produces: [],
tags: ["myTag"],
description: "my operation",
consumes: [
"operation/one",
"operation/two",
]
}
}
}
}
})
const result = taggedOperations(state)(system)
const op = state.getIn(["json", "paths", "/", "get"]).toJS()
expect(result.toJS()).toEqual({
myTag: {
tagDetails: undefined,
operations: [{
id: "get-/",
method: "get",
path: "/",
operation: op
}]
}
})
})
it("should return a List of defined tagged operations", function () {
const system = {
getConfigs: () => ({})
}
const state = fromJS({
json: {
tags: [
{
name: "myTag"
}
],
paths: {
"/": {
"get": {
produces: [],
tags: ["myTag"],
description: "my operation",
consumes: [
"operation/one",
"operation/two",
]
}
}
}
}
})
const result = taggedOperations(state)(system)
const op = state.getIn(["json", "paths", "/", "get"]).toJS()
expect(result.toJS()).toEqual({
myTag: {
tagDetails: {
name: "myTag"
},
operations: [{
id: "get-/",
method: "get",
path: "/",
operation: op
}]
}
})
})
it("should gracefully handle a malformed global tags array", function () {
const system = {
getConfigs: () => ({})
}
const state = fromJS({
json: {
tags: [null],
paths: {
"/": {
"get": {
produces: [],
tags: ["myTag"],
description: "my operation",
consumes: [
"operation/one",
"operation/two",
]
}
}
}
}
})
const result = taggedOperations(state)(system)
const op = state.getIn(["json", "paths", "/", "get"]).toJS()
expect(result.toJS()).toEqual({
myTag: {
tagDetails: undefined,
operations: [{
id: "get-/",
method: "get",
path: "/",
operation: op
}]
}
})
})
it("should gracefully handle a non-array global tags entry", function () {
const system = {
getConfigs: () => ({})
}
const state = fromJS({
json: {
tags: "asdf",
paths: {
"/": {
"get": {
produces: [],
tags: ["myTag"],
description: "my operation",
consumes: [
"operation/one",
"operation/two",
]
}
}
}
}
})
const result = taggedOperations(state)(system)
const op = state.getIn(["json", "paths", "/", "get"]).toJS()
expect(result.toJS()).toEqual({
myTag: {
tagDetails: undefined,
operations: [{
id: "get-/",
method: "get",
path: "/",
operation: op
}]
}
})
})
})