diff --git a/src/core/plugins/spec/selectors.js b/src/core/plugins/spec/selectors.js index c3d0ab2bded..88ac0102979 100644 --- a/src/core/plugins/spec/selectors.js +++ b/src/core/plugins/spec/selectors.js @@ -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) => { diff --git a/test/core/plugins/spec/selectors.js b/test/core/plugins/spec/selectors.js index 6f3e833561e..6a563ba5726 100644 --- a/test/core/plugins/spec/selectors.js +++ b/test/core/plugins/spec/selectors.js @@ -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 + }] + } + }) + }) + })