From 5e4b6d346badaf6fe206f8addc2327b8da8be4f3 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Mon, 18 Jun 2018 17:41:16 -0700 Subject: [PATCH] add identity-based lookup support in spec selectors --- src/core/plugins/spec/selectors.js | 44 +++-- test/core/plugins/spec/selectors.js | 261 ++++++++++++++++++++++++++++ 2 files changed, 287 insertions(+), 18 deletions(-) diff --git a/src/core/plugins/spec/selectors.js b/src/core/plugins/spec/selectors.js index f09f31b9..80632967 100644 --- a/src/core/plugins/spec/selectors.js +++ b/src/core/plugins/spec/selectors.js @@ -294,15 +294,37 @@ export const allowTryItOutFor = () => { return true } +export const parameterWithMetaByIdentity = (state, pathMethod, param) => { + const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], Map()) + const metaParams = state.getIn(["meta", "paths", ...pathMethod, "parameters"], Map()) + + const mergedParams = opParams.map((currentParam) => { + const nameInKeyedMeta = metaParams.get(`${param.get("name")}.${param.get("in")}`) + const hashKeyedMeta = metaParams.get(`${param.get("name")}.${param.get("in")}.hash-${param.hashCode()}`) + return Map().merge( + currentParam, + nameInKeyedMeta, + hashKeyedMeta + ) + }) + + return mergedParams.find(curr => curr.get("in") === param.get("in") && curr.get("name") === param.get("name"), Map()) +} + + +export const parameterWithMeta = (state, pathMethod, paramName, paramIn) => { + const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], Map()) + const currentParam = opParams.find(param => param.get("in") === paramIn && param.get("name") === paramName, Map()) + + return parameterWithMetaByIdentity(state, pathMethod, currentParam) +} + export const operationWithMeta = (state, path, method) => { const op = specJsonWithResolvedSubtrees(state).getIn(["paths", path, method], Map()) const meta = state.getIn(["meta", "paths", path, method], Map()) const mergedParams = op.get("parameters", List()).map((param) => { - return Map().merge( - param, - meta.getIn(["parameters", `${param.get("name")}.${param.get("in")}`]) - ) + return parameterWithMetaByIdentity(state, [path, method], param) }) return Map() @@ -310,20 +332,6 @@ export const operationWithMeta = (state, path, method) => { .set("parameters", mergedParams) } -export const parameterWithMeta = (state, pathMethod, paramName, paramIn) => { - const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], Map()) - const metaParams = state.getIn(["meta", "paths", ...pathMethod, "parameters"], Map()) - - const mergedParams = opParams.map((param) => { - return Map().merge( - param, - metaParams.get(`${param.get("name")}.${param.get("in")}`) - ) - }) - - return mergedParams.find(param => param.get("in") === paramIn && param.get("name") === paramName, Map()) -} - // Get the parameter value by parameter name export function getParameter(state, pathMethod, name, inType) { pathMethod = pathMethod || [] diff --git a/test/core/plugins/spec/selectors.js b/test/core/plugins/spec/selectors.js index 139d4600..250443cc 100644 --- a/test/core/plugins/spec/selectors.js +++ b/test/core/plugins/spec/selectors.js @@ -11,6 +11,11 @@ import { } from "corePlugins/spec/selectors" import Petstore from "./assets/petstore.json" +import { + operationWithMeta, + parameterWithMeta, + parameterWithMetaByIdentity +} from "../../../../src/core/plugins/spec/selectors" describe("spec plugin - selectors", function(){ @@ -451,4 +456,260 @@ describe("spec plugin - selectors", function(){ expect(result.getIn(["paths"]).keySeq().toJS()).toEqual(correctOrder) }) }) + + describe.only("operationWithMeta", function() { + it("should support merging in name+in keyed param metadata", function () { + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [ + { + name: "body", + in: "body" + } + ] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + "body.body": { + value: "abc123" + } + } + } + } + } + } + }) + + const result = operationWithMeta(state, "/", "get") + + expect(result.toJS()).toEqual({ + parameters: [ + { + name: "body", + in: "body", + value: "abc123" + } + ] + }) + }) + it("should support merging in hash-keyed param metadata", function () { + const bodyParam = fromJS({ + name: "body", + in: "body" + }) + + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [ + bodyParam + ] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + [`body.body.hash-${bodyParam.hashCode()}`]: { + value: "abc123" + } + } + } + } + } + } + }) + + const result = operationWithMeta(state, "/", "get") + + expect(result.toJS()).toEqual({ + parameters: [ + { + name: "body", + in: "body", + value: "abc123" + } + ] + }) + }) + }) + describe.only("parameterWithMeta", function() { + it("should support merging in name+in keyed param metadata", function () { + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [ + { + name: "body", + in: "body" + } + ] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + "body.body": { + value: "abc123" + } + } + } + } + } + } + }) + + const result = parameterWithMeta(state, ["/", "get"], "body", "body") + + expect(result.toJS()).toEqual({ + name: "body", + in: "body", + value: "abc123" + }) + }) + it("should give best-effort when encountering hash-keyed param metadata", function () { + const bodyParam = fromJS({ + name: "body", + in: "body" + }) + + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [ + bodyParam + ] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + [`body.body.hash-${bodyParam.hashCode()}`]: { + value: "abc123" + } + } + } + } + } + } + }) + + const result = parameterWithMeta(state, ["/", "get"], "body", "body") + + expect(result.toJS()).toEqual({ + name: "body", + in: "body", + value: "abc123" + }) + }) + + }) + describe.only("parameterWithMetaByIdentity", function() { + it("should support merging in name+in keyed param metadata", function () { + const bodyParam = fromJS({ + name: "body", + in: "body" + }) + + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [bodyParam] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + "body.body": { + value: "abc123" + } + } + } + } + } + } + }) + + const result = parameterWithMetaByIdentity(state, ["/", "get"], bodyParam) + + expect(result.toJS()).toEqual({ + name: "body", + in: "body", + value: "abc123" + }) + }) + it("should support merging in hash-keyed param metadata", function () { + const bodyParam = fromJS({ + name: "body", + in: "body" + }) + + const state = fromJS({ + json: { + paths: { + "/": { + "get": { + parameters: [ + bodyParam + ] + } + } + } + }, + meta: { + paths: { + "/": { + "get": { + parameters: { + [`body.body.hash-${bodyParam.hashCode()}`]: { + value: "abc123" + } + } + } + } + } + } + }) + + const result = parameterWithMetaByIdentity(state, ["/", "get"], bodyParam) + + expect(result.toJS()).toEqual({ + name: "body", + in: "body", + value: "abc123" + }) + }) + }) })