add identity-based lookup support in spec selectors
This commit is contained in:
@@ -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 || []
|
||||
|
||||
@@ -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"
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user