Skip to content

Commit 0414b65

Browse files
authored
fix(semval): sbuject OAS3 req/res schemas to schema validation (#1769)
1 parent 55059f9 commit 0414b65

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/plugins/validate-semantic/selectors.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ export const isParameterSchema = (state, node) => (sys) => {
7474
}
7575
}
7676

77+
export const isOAS3RequestBodySchema = (state, node) => () => {
78+
const [key,, gpKey, ggpKey] = node.path.slice().reverse()
79+
80+
return key === "schema"
81+
&& gpKey === "content"
82+
&& ggpKey === "requestBody"
83+
}
84+
85+
export const isOAS3ResponseSchema = (state, node) => () => {
86+
const [key,, gpKey,, gggpKey] = node.path.slice().reverse()
87+
88+
return key === "schema"
89+
&& gpKey === "content"
90+
&& gggpKey === "responses"
91+
}
92+
7793
export const isResponse = (state, node) => (sys) => {
7894
if(sys.validateSelectors.isVendorExt(node)) {
7995
return false
@@ -116,6 +132,7 @@ export const allSchemas = () => (system) => {
116132
validateSelectors.allDefinitions(),
117133
validateSelectors.allHeaders(),
118134
validateSelectors.allSubSchemas(),
135+
validateSelectors.allOAS3OperationSchemas()
119136
]
120137

121138
return Promise.all(selectors)
@@ -211,6 +228,20 @@ export const allParameterSchemas = () => (system) => {
211228
})
212229
}
213230

231+
export const allOAS3OperationSchemas = () => (system) => {
232+
return system.fn.traverseOnce({
233+
name: "allOAS3OperationSchemas",
234+
fn: (node) => {
235+
if(
236+
system.validateSelectors.isOAS3RequestBodySchema(node)
237+
|| system.validateSelectors.isOAS3ResponseSchema(node)
238+
) {
239+
return node
240+
}
241+
},
242+
})
243+
}
244+
214245
export const allHeaders = () => (system) => {
215246
return system.fn.traverseOnce({
216247
name: "allHeader",

test/plugins/selectors.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,47 @@ describe("validation plugin - selectors", function() {
245245
expect(nodes[2].key).toEqual("foo")
246246
})
247247
})
248+
it("allSchemas should pick up OAS3 request and response schemas", () => {
249+
const spec = {
250+
"openapi": "3.0.0",
251+
"paths": {
252+
"/ping": {
253+
"post": {
254+
"requestBody": {
255+
"content": {
256+
"application/myRequestMediaType": {
257+
"schema": {
258+
"type": "array"
259+
}
260+
}
261+
}
262+
},
263+
"responses": {
264+
"200": {
265+
"description": "OK",
266+
"content": {
267+
"application/myResponseMediaType": {
268+
"schema": {
269+
"type": "string"
270+
}
271+
}
272+
}
273+
}
274+
}
275+
}
276+
}
277+
}
278+
}
279+
280+
return getSystem(spec)
281+
.then(system => system.validateSelectors.allSchemas())
282+
.then(nodes => {
283+
expect(nodes.length).toEqual(2)
284+
expect(nodes[0].node).toNotBe(nodes[1].node)
285+
expect(nodes[0].key).toEqual("schema")
286+
expect(nodes[0].parent.key).toEqual("application/myRequestMediaType")
287+
expect(nodes[1].key).toEqual("schema")
288+
expect(nodes[1].parent.key).toEqual("application/myResponseMediaType")
289+
})
290+
})
248291
})

0 commit comments

Comments
 (0)