Added Swagger
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import { pathForPosition, positionRangeForPath } from "plugins/ast/ast"
|
||||
|
||||
describe("ASTManager", function() {
|
||||
describe("#pathForPosition", function () {
|
||||
describe("out of range", function () {
|
||||
it("returns empty array for out of range row", function () {
|
||||
let position = { line: 3, column: 0 }
|
||||
let path = pathForPosition("swagger: 2.0", position)
|
||||
|
||||
expect(path).toEqual([])
|
||||
})
|
||||
|
||||
it("returns empty array for out of range column", function () {
|
||||
let position = { line: 0, column: 100 }
|
||||
let path = pathForPosition("swagger: 2.0", position)
|
||||
|
||||
expect(path).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is a simple hash `swagger: 2.0`", function () {
|
||||
it("should return empty array when pointer is at middle of the hash key", function () {
|
||||
let position = { line: 0, column: 3 }
|
||||
let path = pathForPosition("swagger: 2.0", position)
|
||||
|
||||
expect(path).toEqual([])
|
||||
})
|
||||
|
||||
it("should return ['swagger'] when pointer is at the value", function () {
|
||||
let position = { line: 0, column: 10 }
|
||||
let path = pathForPosition("swagger: 2.0", position)
|
||||
|
||||
expect(path).toEqual(["swagger"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is an array: ['abc', 'cde']", function () {
|
||||
let yaml = [
|
||||
/*
|
||||
0
|
||||
01234567 */
|
||||
/* 0 */ "- abc",
|
||||
/* 1 */ "- def"
|
||||
].join("\n")
|
||||
|
||||
it("should return empty array when pointer is at array dash", function () {
|
||||
let position = { line: 0, column: 0 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual([])
|
||||
})
|
||||
|
||||
it("should return ['0'] when pointer is at abc", function () {
|
||||
let position = { line: 0, column: 3 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["0"])
|
||||
})
|
||||
|
||||
it("should return ['1'] when pointer is at abc", function () {
|
||||
let position = { line: 1, column: 3 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["1"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is an array of arrays", function () {
|
||||
let yaml = [
|
||||
/*
|
||||
0 10
|
||||
0123456789012345 */
|
||||
/* 0 */ "-",
|
||||
/* 1 */ " - abc",
|
||||
/* 2 */ " - def",
|
||||
/* 3 */ "-",
|
||||
/* 4 */ " - ABC",
|
||||
/* 5 */ " - DEF"
|
||||
].join("\n")
|
||||
|
||||
it("should return ['0', '0'] when pointer is at 'abc'", function () {
|
||||
let position = { line: 1, column: 5 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["0", "0"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is an array of hashs", function () {
|
||||
let yaml = [
|
||||
/*
|
||||
0 10
|
||||
0123456789012345 */
|
||||
/* 0 */ "- key: value",
|
||||
/* 1 */ " num: 1",
|
||||
/* 2 */ "- name: Tesla",
|
||||
/* 3 */ " year: 2016"
|
||||
].join("\n")
|
||||
|
||||
it("should return ['0'] when pointer is at 'key'", function () {
|
||||
let position = { line: 0, column: 3 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["0"])
|
||||
})
|
||||
|
||||
it("should return ['0', 'key'] when pointer is at 'value'", function () {
|
||||
let position = { line: 0, column: 9 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["0", "key"])
|
||||
})
|
||||
|
||||
it("should return ['1', 'year'] when pointer is at '2016'", function () {
|
||||
let position = { line: 3, column: 10 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["1", "year"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("full document", function () {
|
||||
let yaml = [
|
||||
/*
|
||||
0 10 20 30
|
||||
012345678901234567890123456789012345678 */
|
||||
/* 0 */ "swagger: '2.0'",
|
||||
/* 1 */ "info:",
|
||||
/* 2 */ " title: Test document",
|
||||
/* 3 */ " version: 0.0.1",
|
||||
/* 4 */ " contact:",
|
||||
/* 5 */ " name: Sahar",
|
||||
/* 6 */ " url: github.com",
|
||||
/* 7 */ " email: me@example.com",
|
||||
/* 8 */ " "
|
||||
].join("\n")
|
||||
|
||||
it("should return ['info', 'contact', 'email'] when pointer is at me@", function () {
|
||||
let position = { line: 7, column: 13 }
|
||||
let path = pathForPosition(yaml, position)
|
||||
|
||||
expect(path).toEqual(["info", "contact", "email"])
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("#positionRangeForPath", function() {
|
||||
it("return {{-1, -1}, {-1, -1}} for invalid paths", function() {
|
||||
let yaml = [
|
||||
"key: value",
|
||||
"anotherKey: value"
|
||||
].join("\n")
|
||||
|
||||
let position = positionRangeForPath(yaml, ["invalid"])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: -1,
|
||||
column: -1
|
||||
})
|
||||
expect(position.end).toEqual({
|
||||
line: -1,
|
||||
column: -1
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is a simple hash `swagger: 2.0`", function() {
|
||||
let yaml = "swagger: 2.0"
|
||||
|
||||
it("return {0, 0} for start of empty array path (root)", function() {
|
||||
let position = positionRangeForPath(yaml, [])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: 0,
|
||||
column: 0,
|
||||
pointer: 0
|
||||
})
|
||||
})
|
||||
|
||||
it("return {0, 12} for end of empty array path (root)", function() {
|
||||
let position = positionRangeForPath(yaml, [])
|
||||
|
||||
expect(position.end).toEqual({
|
||||
line: 0,
|
||||
column: 12,
|
||||
pointer: 12
|
||||
})
|
||||
})
|
||||
|
||||
it("return {0, 9} for start of ['swagger']", function() {
|
||||
let position = positionRangeForPath(yaml, ["swagger"])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: 0,
|
||||
column: 9,
|
||||
pointer: 9
|
||||
})
|
||||
})
|
||||
|
||||
it("return {0, 12} for end of ['swagger']", function() {
|
||||
let position = positionRangeForPath(yaml, ["swagger"])
|
||||
|
||||
expect(position.end).toEqual({
|
||||
line: 0,
|
||||
column: 12,
|
||||
pointer: 12
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("when document is an array of primitives", function() {
|
||||
let yaml = [
|
||||
"key:",
|
||||
" - value1",
|
||||
" - value2"
|
||||
].join("\n")
|
||||
|
||||
it("returns {1, 4} for ['key', '0']", function() {
|
||||
let position = positionRangeForPath(yaml, ["key", "0"])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: 1,
|
||||
column: 4,
|
||||
pointer: 9
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("full document", function() {
|
||||
let yaml = [
|
||||
/*
|
||||
0 10 20 30
|
||||
012345678901234567890123456789012345678 */
|
||||
/* 0 */ "swagger: '2.0'",
|
||||
/* 1 */ "info:",
|
||||
/* 2 */ " title: Test document",
|
||||
/* 3 */ " version: 0.0.1",
|
||||
/* 4 */ " contact:",
|
||||
/* 5 */ " name: Sahar",
|
||||
/* 6 */ " url: github.com",
|
||||
/* 7 */ " email: me@example.com",
|
||||
/* 8 */ " "
|
||||
].join("\n")
|
||||
|
||||
it("returns {2, 2} for start of ['info']", function() {
|
||||
let position = positionRangeForPath(yaml, ["info"])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: 2,
|
||||
column: 2,
|
||||
pointer: 23
|
||||
})
|
||||
})
|
||||
|
||||
it("returns {5, 10} for start of ['info', 'contact', 'name']", function() {
|
||||
let position = positionRangeForPath(yaml, ["info", "contact", "name"])
|
||||
|
||||
expect(position.start).toEqual({
|
||||
line: 5,
|
||||
column: 10,
|
||||
pointer: 82
|
||||
})
|
||||
})
|
||||
|
||||
it("returns {5, 15} for end of ['info', 'contact', 'name']", function() {
|
||||
let position = positionRangeForPath(yaml, ["info", "contact", "name"])
|
||||
|
||||
expect(position.end).toEqual({
|
||||
line: 5,
|
||||
column: 15,
|
||||
pointer: 87
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,49 @@
|
||||
import expect, { createSpy } from "expect"
|
||||
import dedent from "dedent"
|
||||
import { getPathForPosition } from "src/plugins/editor-autosuggest/fn.js"
|
||||
|
||||
describe("Editor Autosuggest Plugin", function() {
|
||||
describe("getPathForPosition - YAML value preparation", function() {
|
||||
it.skip("should not modify a valid simple map", function() {
|
||||
// Given
|
||||
const editorValue = dedent(`
|
||||
a: "one"
|
||||
b: "two"
|
||||
`)
|
||||
const pos = { row: 0, col: 0 }
|
||||
const AST = {
|
||||
pathForPosition: createSpy()
|
||||
}
|
||||
|
||||
// When
|
||||
getPathForPosition({ editorValue, pos, prefix: "", AST })
|
||||
|
||||
// Then
|
||||
expect(AST.pathForPosition).toHaveBeenCalled()
|
||||
|
||||
const [preparedEditorValue] = AST.pathForPosition.calls[0].arguments
|
||||
expect(preparedEditorValue).toEqual(editorValue) // should be unchanged
|
||||
})
|
||||
it("should modify an array member map fragment", function() {
|
||||
// Given
|
||||
const editorValue = dedent(`
|
||||
myArray:
|
||||
- one: "abc"
|
||||
- two:
|
||||
`)
|
||||
const pos = { row: 2, col: 6 }
|
||||
const AST = {
|
||||
pathForPosition: createSpy()
|
||||
}
|
||||
|
||||
// When
|
||||
getPathForPosition({ editorValue, pos, prefix: "", AST })
|
||||
|
||||
// Then
|
||||
expect(AST.pathForPosition).toHaveBeenCalled()
|
||||
|
||||
const [preparedEditorValue] = AST.pathForPosition.calls[0].arguments
|
||||
expect(preparedEditorValue).toEqual(editorValue + " ~")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,163 @@
|
||||
import expect from "expect"
|
||||
import SwaggerUi from "swagger-ui"
|
||||
import EditorMetadataPlugin from "plugins/editor-metadata"
|
||||
|
||||
function getSystem(spec) {
|
||||
return new Promise((resolve) => {
|
||||
const system = SwaggerUi({
|
||||
spec,
|
||||
domNode: null,
|
||||
presets: [
|
||||
SwaggerUi.plugins.SpecIndex,
|
||||
SwaggerUi.plugins.ErrIndex,
|
||||
SwaggerUi.plugins.DownloadUrl,
|
||||
SwaggerUi.plugins.SwaggerJsIndex,
|
||||
],
|
||||
initialState: {
|
||||
layout: undefined
|
||||
},
|
||||
plugins: [
|
||||
EditorMetadataPlugin,
|
||||
() => ({
|
||||
statePlugins: {
|
||||
configs: {
|
||||
actions: {
|
||||
loaded: () => {
|
||||
return {
|
||||
type: "noop"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
resolve(system)
|
||||
})
|
||||
}
|
||||
|
||||
describe("editor metadata plugin", function() {
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
it("should provide a `getEditorMetadata` method", async () => {
|
||||
const spec = {}
|
||||
|
||||
const system = await getSystem(spec)
|
||||
|
||||
expect(system.getEditorMetadata).toBeA(Function)
|
||||
})
|
||||
|
||||
it("should return JS object spec content from the `getEditorMetadata` method", async () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
description: "hello there!",
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const system = await getSystem(spec)
|
||||
|
||||
expect(system.getEditorMetadata().contentString).toEqual(`{"swagger":"2.0","paths":{"/":{"get":{"description":"hello there!","responses":{"200":{"description":"ok"}}}}}}`)
|
||||
expect(system.getEditorMetadata().contentObject).toEqual(spec)
|
||||
})
|
||||
|
||||
|
||||
it("should return YAML string spec content from the `getEditorMetadata` method", async () => {
|
||||
const spec = `---
|
||||
swagger: '2.0'
|
||||
paths:
|
||||
"/":
|
||||
get:
|
||||
description: hello there!
|
||||
responses:
|
||||
'200':
|
||||
description: ok`
|
||||
|
||||
const system = await getSystem()
|
||||
|
||||
system.specActions.updateSpec(spec)
|
||||
|
||||
expect(system.getEditorMetadata().contentString).toEqual(spec)
|
||||
expect(system.getEditorMetadata().contentObject).toEqual({
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
description: "hello there!",
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it("should return isValid for a valid spec", async () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
description: "hello there!",
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const system = await getSystem(spec)
|
||||
|
||||
expect(system.getEditorMetadata().isValid).toBeA("boolean")
|
||||
expect(system.getEditorMetadata().isValid).toBe(true)
|
||||
})
|
||||
|
||||
|
||||
it("should return isValid for an invalid spec", async () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
description: "hello there!",
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const err = {
|
||||
type: "spec",
|
||||
message: "it's broken!"
|
||||
}
|
||||
|
||||
const system = await getSystem(spec)
|
||||
|
||||
system.errActions.newSpecErr(err)
|
||||
|
||||
expect(system.getEditorMetadata().isValid).toBeA("boolean")
|
||||
expect(system.getEditorMetadata().isValid).toBe(false)
|
||||
expect(system.getEditorMetadata().errors).toEqual([err])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,594 @@
|
||||
import expect, { createSpy } from "expect"
|
||||
import rewiremock from "rewiremock"
|
||||
import Enzyme, { shallow } from "enzyme"
|
||||
import Adapter from "enzyme-adapter-react-15"
|
||||
import React from "react"
|
||||
import FakeAce, { Session } from "test/unit/mocks/ace.js"
|
||||
import { fromJS } from "immutable"
|
||||
|
||||
const pause = (ms) => new Promise((res) => setTimeout(res, ms))
|
||||
|
||||
const EVENTUALLY = 900 // ms
|
||||
|
||||
/**
|
||||
* We're mocking out the editor,
|
||||
* so uses of the phrase "should see this in editor",
|
||||
* will match to the following Ace methods:
|
||||
*
|
||||
* - "what user see's in editor" => fakeAce.userSees()
|
||||
* - "user types something (end of document)" => fakeAce.userTypes("hi")
|
||||
* - "Ctrl-Z" => fakeAce.userUndo()
|
||||
* - "Ctrl-Shift-Z" => fakeAce.userRedo()
|
||||
**/
|
||||
|
||||
describe("editor", function() {
|
||||
before(function () {
|
||||
// Enzyme.configure({ adapter: new Adapter()})
|
||||
Enzyme.configure({ adapter: new Adapter()})
|
||||
|
||||
// Whole bunch of mocks!
|
||||
rewiremock.enable()
|
||||
rewiremock("brace/mode/yaml").with({})
|
||||
rewiremock("brace/theme/tomorrow_night_eighties").with({})
|
||||
rewiremock("brace/ext/language_tools").with({})
|
||||
rewiremock("brace/ext/searchbox").with({})
|
||||
rewiremock("./brace-snippets-yaml").with({})
|
||||
rewiremock("./editor.less").with({})
|
||||
})
|
||||
|
||||
after(function() {
|
||||
rewiremock.disable()
|
||||
})
|
||||
|
||||
beforeEach(function() {
|
||||
delete require.cache[require.resolve("react-ace")]
|
||||
})
|
||||
|
||||
describe("fake ace", function() {
|
||||
|
||||
it("should be an event emitter", () => {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const spy = createSpy()
|
||||
fakeAce.on("foo", spy)
|
||||
|
||||
// When
|
||||
fakeAce.emit("foo", "bar")
|
||||
|
||||
// Then
|
||||
expect(spy.calls.length).toEqual(1)
|
||||
expect(spy.calls[0].arguments[0]).toEqual("bar")
|
||||
})
|
||||
|
||||
it("should return `this`, when calling .edit", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
const res = fakeAce.edit()
|
||||
|
||||
// Then
|
||||
expect(res).toBe(fakeAce)
|
||||
})
|
||||
|
||||
|
||||
it("should keep track of setValue", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.setValue("foo")
|
||||
|
||||
// Then
|
||||
const res = fakeAce.getValue()
|
||||
expect(res).toEqual("foo")
|
||||
})
|
||||
|
||||
it("should spy on setValue", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.setValue("foo")
|
||||
|
||||
// Then
|
||||
expect(fakeAce.setValue.calls.length).toEqual(1)
|
||||
expect(fakeAce.setValue.calls[0].arguments[0]).toEqual("foo")
|
||||
})
|
||||
|
||||
it("should return a single session, with getSession", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
const res = fakeAce.getSession()
|
||||
|
||||
// Then
|
||||
expect(res).toBeA(Session)
|
||||
})
|
||||
|
||||
it("should add options via setOptions", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.setOptions({one: "uno"})
|
||||
|
||||
// Then
|
||||
const res = fakeAce.getOption("one")
|
||||
expect(res).toEqual("uno")
|
||||
})
|
||||
|
||||
describe("userUndo/Redo", function() {
|
||||
|
||||
it("should revert to last input", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.userTypes("one")
|
||||
|
||||
// When
|
||||
fakeAce.userTypes("two")
|
||||
|
||||
// Then
|
||||
fakeAce.userUndo()
|
||||
expect(fakeAce.userSees()).toEqual("one")
|
||||
})
|
||||
|
||||
it("should revert to empty document, no changes were made", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.userUndo()
|
||||
|
||||
// Then
|
||||
expect(fakeAce.userSees()).toEqual("")
|
||||
})
|
||||
|
||||
it("should revert to empty document, after arbitrary undos", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.userUndo()
|
||||
fakeAce.userUndo()
|
||||
fakeAce.userUndo()
|
||||
fakeAce.userUndo()
|
||||
|
||||
// Then
|
||||
expect(fakeAce.userSees()).toEqual("")
|
||||
})
|
||||
|
||||
it("should not extend redos after last change", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.userTypes("x")
|
||||
|
||||
// When
|
||||
fakeAce.userRedo()
|
||||
fakeAce.userRedo()
|
||||
fakeAce.userRedo()
|
||||
|
||||
// Then
|
||||
expect(fakeAce.userSees()).toEqual("x")
|
||||
})
|
||||
|
||||
it("should allow redo after single undo", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.userTypes("x")
|
||||
fakeAce.userTypes("x")
|
||||
fakeAce.userUndo()
|
||||
|
||||
// When
|
||||
fakeAce.userRedo()
|
||||
|
||||
// Then
|
||||
expect(fakeAce.userSees()).toEqual("xx")
|
||||
})
|
||||
|
||||
it("should create new thread of undo stack, after new change", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.userTypes("1")
|
||||
fakeAce.userTypes("2")
|
||||
fakeAce.userTypes("3")
|
||||
fakeAce.userTypes("4")
|
||||
fakeAce.userUndo() // 123
|
||||
fakeAce.userUndo() // 12
|
||||
fakeAce.userTypes("5") // 125
|
||||
|
||||
// When
|
||||
fakeAce.userRedo() // 125, don't extend beyond
|
||||
|
||||
// Then
|
||||
expect(fakeAce.userSees()).toEqual("125")
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("fake session", function() {
|
||||
|
||||
it("should keep add state for markers", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const fakeSession = fakeAce.getSession()
|
||||
|
||||
// When
|
||||
fakeSession.addMarker({one: 1})
|
||||
|
||||
// Then
|
||||
const res = fakeSession.getMarkers()
|
||||
expect(res).toBeAn("array")
|
||||
expect(res.length).toEqual(1)
|
||||
expect(res[0]).toEqual({id: 0, one: 1})
|
||||
})
|
||||
|
||||
it("should keep remove state for markers", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const fakeSession = fakeAce.getSession()
|
||||
fakeSession.addMarker({one: 1})
|
||||
|
||||
// When
|
||||
fakeSession.removeMarker(0)
|
||||
|
||||
// Then
|
||||
const res = fakeSession.getMarkers()
|
||||
expect(res).toBeAn("array")
|
||||
expect(res.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("should spy on addMarker", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const fakeSession = fakeAce.getSession()
|
||||
|
||||
// When
|
||||
fakeSession.addMarker({one: 1})
|
||||
|
||||
// Then
|
||||
expect(fakeSession.addMarker.calls.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("should spy on setMode", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const fakeSession = fakeAce.getSession()
|
||||
|
||||
// When
|
||||
fakeSession.setMode()
|
||||
|
||||
// Then
|
||||
expect(fakeSession.setMode.calls.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("should have a .selection which includes toJSON, fromJSON", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
const fakeSession = fakeAce.getSession()
|
||||
|
||||
// Then
|
||||
expect(fakeSession.selection).toIncludeKey("toJSON")
|
||||
expect(fakeSession.selection).toIncludeKey("fromJSON")
|
||||
})
|
||||
|
||||
|
||||
describe("userTypes", function() {
|
||||
it("should emit 'change'", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const spy = createSpy()
|
||||
fakeAce.on("change", spy)
|
||||
|
||||
// When
|
||||
fakeAce.userTypes("hello")
|
||||
|
||||
// Then
|
||||
expect(spy.calls.length).toBeGreaterThan(1)
|
||||
})
|
||||
|
||||
it("should change the value", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.userTypes("hello")
|
||||
|
||||
// Then
|
||||
expect(fakeAce.getValue()).toEqual("hello")
|
||||
})
|
||||
})
|
||||
|
||||
describe("userSees", function() {
|
||||
it("should match userTypes", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.userTypes("hi")
|
||||
|
||||
// Then
|
||||
const res = fakeAce.userSees()
|
||||
expect(res).toEqual("hi")
|
||||
})
|
||||
|
||||
it("should match setValue", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.setValue("hello")
|
||||
|
||||
// Then
|
||||
const res = fakeAce.userSees()
|
||||
expect(res).toEqual("hello")
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("renderer", function() {
|
||||
it("should have a stub for setShowGutter", function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
|
||||
// When
|
||||
fakeAce.renderer.setShowGutter("foo")
|
||||
|
||||
// Then
|
||||
expect(fakeAce.renderer.setShowGutter.calls.length).toEqual(1)
|
||||
expect(fakeAce.renderer.setShowGutter.calls[0].arguments[0]).toEqual("foo")
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("editor component", function() {
|
||||
|
||||
it("should EVENTUALLY call onChange when user enters input", (done) => {
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const spy = createSpy()
|
||||
const wrapper = shallow(
|
||||
<Editor onChange={spy} />
|
||||
)
|
||||
wrapper
|
||||
.find("ReactAce").shallow()
|
||||
|
||||
// When
|
||||
// Simulate user input
|
||||
fakeAce.userTypes("hello")
|
||||
|
||||
// Then
|
||||
setTimeout(() => {
|
||||
expect(spy.calls.length).toEqual(1)
|
||||
expect(spy.calls[0].arguments[0]).toEqual("hello")
|
||||
done()
|
||||
}, EVENTUALLY)
|
||||
|
||||
})
|
||||
|
||||
it("should EVENTUALLY put the contents of `value` prop into editor, without regard to `origin` property", (done) => {
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
|
||||
// When
|
||||
const wrapper = shallow(
|
||||
<Editor value={"original value"} />
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
|
||||
// Then
|
||||
setTimeout(() => {
|
||||
expect(fakeAce.userSees()).toEqual("original value")
|
||||
done()
|
||||
}, EVENTUALLY)
|
||||
})
|
||||
|
||||
it("should EVENTUALLY put the contents of `value` prop into editor, with `foo` origin property ", (done) => {
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
|
||||
// When
|
||||
const wrapper = shallow(
|
||||
<Editor value={"original value"} origin="foo" />
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
|
||||
// Then
|
||||
setTimeout(() => {
|
||||
expect(fakeAce.userSees()).toEqual("original value")
|
||||
done()
|
||||
}, EVENTUALLY)
|
||||
})
|
||||
|
||||
it("should NEVER update ace if the yaml originated in editor", async () => {
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
|
||||
// When
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" />
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
wrapper.setProps({value: "new value", origin: "editor"})
|
||||
|
||||
// Then
|
||||
await pause(EVENTUALLY)
|
||||
expect(fakeAce.userSees()).toEqual("original value")
|
||||
})
|
||||
|
||||
// SKIPPED: Does this have any value at this level? And not editor-container?
|
||||
it.skip("SKIP: should EVENTUALLY call onChange ONCE if the user types/pauses/types", async function() {
|
||||
this.timeout(10000)
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const spy = createSpy()
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" onChange={spy}/>
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
|
||||
// When
|
||||
fakeAce.userTypes(" one")
|
||||
await pause(EVENTUALLY / 2)
|
||||
fakeAce.userTypes("two")
|
||||
await pause(EVENTUALLY / 2)
|
||||
fakeAce.userTypes("three")
|
||||
await pause(EVENTUALLY / 2)
|
||||
|
||||
await pause(EVENTUALLY * 2)
|
||||
expect(fakeAce.userSees()).toEqual("original value onetwothree")
|
||||
expect(spy.calls.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("should EVENTUALLY call onChange when ctrl-z", async function() {
|
||||
this.timeout(10000)
|
||||
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const spy = createSpy()
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" onChange={spy}/>
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
fakeAce.userTypes("one")
|
||||
|
||||
// When
|
||||
fakeAce.userUndo()
|
||||
|
||||
await pause(EVENTUALLY)
|
||||
expect(fakeAce.userSees()).toEqual("original value")
|
||||
expect(spy.calls.length).toEqual(1)
|
||||
})
|
||||
|
||||
describe("markers", function() {
|
||||
|
||||
it("should place markers into editor", async function() {
|
||||
// Given
|
||||
const fakeAce = new FakeAce()
|
||||
const spy = createSpy()
|
||||
rewiremock("brace").with(fakeAce)
|
||||
rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const dummy = fromJS({one: 1})
|
||||
const wrapper = shallow(
|
||||
<Editor markers={dummy} />
|
||||
)
|
||||
|
||||
// When
|
||||
wrapper.find("ReactAce").shallow()
|
||||
await pause(EVENTUALLY)
|
||||
|
||||
// Then
|
||||
expect(spy.calls.length).toEqual(1)
|
||||
expect(spy.calls[0].arguments[0]).toInclude({markers: {one: 1}})
|
||||
})
|
||||
|
||||
it("should place markers after yaml", async function() {
|
||||
// Given
|
||||
const order = []
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.setValue.andCall(() => order.push("setValue"))
|
||||
const spy = createSpy().andCall(() => order.push("placeMarkers"))
|
||||
rewiremock("brace").with(fakeAce)
|
||||
rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" markers={{}} />
|
||||
)
|
||||
|
||||
// When
|
||||
wrapper.find("ReactAce").shallow()
|
||||
await pause(EVENTUALLY)
|
||||
|
||||
// Then
|
||||
expect(order).toEqual(["setValue", "placeMarkers"])
|
||||
})
|
||||
|
||||
|
||||
it.skip("should Test for markers being disabled/enabled during a yaml update", async function() {
|
||||
// Given
|
||||
const order = []
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.setValue.andCall(() => order.push("setValue"))
|
||||
const spy = createSpy().andCall(() => {
|
||||
order.push("placeMarkers")
|
||||
return () => order.push("removeMarkers")
|
||||
})
|
||||
rewiremock("brace").with(fakeAce)
|
||||
rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" markers={{}} />
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
|
||||
// When
|
||||
wrapper.setProps({value: "new value", origin: "bob"})
|
||||
await pause(EVENTUALLY)
|
||||
|
||||
// Then
|
||||
expect(order).toEqual(["setValue", "placeMarkers", "removeMarkers", "setValue", "placeMarkers"])
|
||||
})
|
||||
|
||||
it.skip("should Test for markers being disabled/enabled during ctrl-z", async function() {
|
||||
// Given
|
||||
const order = []
|
||||
const fakeAce = new FakeAce()
|
||||
fakeAce.setValue.andCall(() => order.push("setValue"))
|
||||
const spy = createSpy().andCall(() => {
|
||||
order.push("placeMarkers")
|
||||
return () => order.push("removeMarkers")
|
||||
})
|
||||
rewiremock("brace").with(fakeAce)
|
||||
rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})
|
||||
const makeEditor = require("plugins/editor/components/editor.jsx").default
|
||||
const Editor = makeEditor({})
|
||||
const wrapper = shallow(
|
||||
<Editor value="original value" markers={{}} />
|
||||
)
|
||||
wrapper.find("ReactAce").shallow()
|
||||
|
||||
// When
|
||||
fakeAce.userUndo()
|
||||
await pause(EVENTUALLY)
|
||||
|
||||
// Then
|
||||
expect(order).toEqual(["setValue", "placeMarkers", "removeMarkers", "setValue", "placeMarkers"])
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
@@ -0,0 +1,43 @@
|
||||
import SwaggerUi from "swagger-ui"
|
||||
import EditorSpecPlugin from "src/plugins/editor/spec"
|
||||
import expect from "expect"
|
||||
|
||||
describe("editor state plugins", function() {
|
||||
describe("specSelectors.specOrigin", function() {
|
||||
it("should default to 'non-editor'", function() {
|
||||
// Given
|
||||
const system = SwaggerUi({plugins: [EditorSpecPlugin]})
|
||||
|
||||
// When
|
||||
const res = system.specSelectors.specOrigin()
|
||||
|
||||
// Then
|
||||
expect(res).toEqual("not-editor")
|
||||
})
|
||||
})
|
||||
describe("specActions.updateSpec", function() {
|
||||
it("should add a parameter - origin - to state", function() {
|
||||
// Given
|
||||
const system = SwaggerUi({plugins: [EditorSpecPlugin]})
|
||||
|
||||
// When
|
||||
system.specActions.updateSpec("one: 1", "editor")
|
||||
|
||||
// Then
|
||||
const res = system.specSelectors.specOrigin()
|
||||
expect(res).toEqual("editor")
|
||||
})
|
||||
|
||||
it("should default to 'non-editor'", function() {
|
||||
// Given
|
||||
const system = SwaggerUi({plugins: [EditorSpecPlugin]})
|
||||
|
||||
// When
|
||||
system.specActions.updateSpec("one: 1")
|
||||
|
||||
// Then
|
||||
const res = system.specSelectors.specOrigin()
|
||||
expect(res).toEqual("not-editor")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,90 @@
|
||||
import YAML from "js-yaml"
|
||||
import JSONSchemaValidator from "src/plugins/json-schema-validator/validator/index.js"
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import expect from "expect"
|
||||
|
||||
const swagger2SchemaYaml = fs.readFileSync(path.join(__dirname, "../../../../src/plugins/json-schema-validator/swagger2-schema.yaml")).toString()
|
||||
const oas3SchemaYaml = fs.readFileSync(path.join(__dirname, "../../../../src/plugins/json-schema-validator/oas3-schema.yaml")).toString()
|
||||
|
||||
const swagger2Schema = YAML.safeLoad(swagger2SchemaYaml)
|
||||
const oas3Schema = YAML.safeLoad(oas3SchemaYaml)
|
||||
|
||||
|
||||
var testDocuments = fs
|
||||
.readdirSync(__dirname + "/test-documents")
|
||||
.filter(path => path.endsWith("yaml"))
|
||||
.map(path => ({
|
||||
path: "/test-documents/" + path,
|
||||
contentString: fs.readFileSync(__dirname + "/test-documents/" + path, "utf8"),
|
||||
}))
|
||||
.map(doc => ({
|
||||
path: doc.path,
|
||||
content: YAML.safeLoad(doc.contentString)
|
||||
}))
|
||||
|
||||
testDocuments.forEach(doc => {
|
||||
const { path, content } = doc
|
||||
const { meta = {}, cases = [] } = content
|
||||
|
||||
const validator = new JSONSchemaValidator()
|
||||
validator.addSchema(swagger2Schema, ["openapi-2.0"])
|
||||
validator.addSchema(oas3Schema, ["openapi-3.0"])
|
||||
|
||||
const rootDescribe = meta.skip ? describe.skip : describe
|
||||
|
||||
rootDescribe(`schema validation plugin - ` + (meta.title || path), function() {
|
||||
if(content.input && content.output) {
|
||||
// fold simple input/output docs into implicit cases
|
||||
cases.push({
|
||||
input: content.input,
|
||||
output: content.output,
|
||||
})
|
||||
}
|
||||
|
||||
if(cases && cases.length) {
|
||||
cases.forEach(currentCase => {
|
||||
const versionDefaultSchema = currentCase.input.openapi && !currentCase.input.swagger ? "openapi-3.0" : "openapi-2.0"
|
||||
|
||||
const result = validator.validate({
|
||||
jsSpec: currentCase.input,
|
||||
specStr: "", // not needed here
|
||||
schemaPath: versionDefaultSchema,
|
||||
source: "structural",
|
||||
})
|
||||
|
||||
if(currentCase.name) {
|
||||
// only create a new describe block if we have a name
|
||||
describe(currentCase.name || "", function () {
|
||||
assertCaseExpectations(currentCase, result)
|
||||
})
|
||||
} else {
|
||||
// else, just do the assertions under the root describe block
|
||||
assertCaseExpectations(currentCase, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function assertCaseExpectations(currentCase, result) {
|
||||
const itFn = currentCase.skip ? it.skip : it
|
||||
if (currentCase.output.match !== undefined) {
|
||||
itFn("should match expected error output", function () {
|
||||
expect(result).toMatch(currentCase.output.match)
|
||||
})
|
||||
}
|
||||
|
||||
if (currentCase.output.length !== undefined) {
|
||||
itFn("should have expected array length", function () {
|
||||
expect(result).toBeAn(Array)
|
||||
expect(result.length).toBe(currentCase.output.length)
|
||||
})
|
||||
}
|
||||
|
||||
if(currentCase.output.equal !== undefined) {
|
||||
itFn("should equal expected value", function() {
|
||||
expect(result).toEqual(currentCase.output.equal)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
meta:
|
||||
title: "#1394 - path key that does not start with `/`"
|
||||
cases:
|
||||
- name: in Swagger 2
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
version: "#1394"
|
||||
title: "bug #1394"
|
||||
paths:
|
||||
myPath:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should only have path names that start with `/`
|
||||
path: [paths]
|
||||
source: structural
|
||||
- name: in OpenAPI 3
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: "#1394"
|
||||
title: "bug #1394"
|
||||
paths:
|
||||
myPath:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should only have path names that start with `/`
|
||||
path: [paths]
|
||||
source: structural
|
||||
@@ -0,0 +1,30 @@
|
||||
meta:
|
||||
title: "#1480 - illegal `tokenUrl` in implicit OAuth Flow Object"
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
license:
|
||||
name: MIT
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
apiOAuth:
|
||||
type: oauth2
|
||||
flows:
|
||||
implicit:
|
||||
authorizationUrl: 'https://myapi.com/oauth/authorize'
|
||||
tokenUrl: 'https://myapi.com/oauth/token'
|
||||
refreshUrl: 'https://myapi.com/oauth/token/refresh'
|
||||
scopes:
|
||||
-'write:pets': "modify pets in your account"
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should NOT have additional properties
|
||||
additionalProperty: tokenUrl
|
||||
path: [components, securitySchemes, apiOAuth, flows, implicit]
|
||||
source: structural
|
||||
@@ -0,0 +1,26 @@
|
||||
meta:
|
||||
title: "#1489 - illegal presence of both `example` and `examples` in Media Type Object"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
version: "validation"
|
||||
title: Spec with both example and examples
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
text/plain:
|
||||
example: sample response
|
||||
examples:
|
||||
foo:
|
||||
value: anonther response
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should not have both `example` and `examples`, as they are mutually exclusive
|
||||
path: [paths, /, get, responses, "200", content, text/plain]
|
||||
source: structural
|
||||
@@ -0,0 +1,32 @@
|
||||
meta:
|
||||
title: "#1511 - illegal `example` in Path Parameter Object"
|
||||
input:
|
||||
swagger: '2.0'
|
||||
info:
|
||||
version: '1'
|
||||
title: oneOf validation fail
|
||||
description: ok
|
||||
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
summary: Extra `example` field in parameter
|
||||
description: ok
|
||||
parameters:
|
||||
- in: path
|
||||
name: name
|
||||
type: string
|
||||
required: true
|
||||
example: ok
|
||||
responses:
|
||||
"200":
|
||||
description: Ok
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should NOT have additional properties
|
||||
additionalProperty: example
|
||||
path: [paths, /, get, parameters, "0"]
|
||||
source: structural
|
||||
@@ -0,0 +1,72 @@
|
||||
meta:
|
||||
title: "#1519 - incorrect usage of Schema Object properties"
|
||||
cases:
|
||||
- name: "`required: true` in an object schema property subschema"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Example API
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
post:
|
||||
tags:
|
||||
- login
|
||||
summary: Login using email or nickname
|
||||
description: Returns customer info and jwt access and refresh tokens
|
||||
security:
|
||||
- apiKey: []
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
emailOrNickname:
|
||||
type: string
|
||||
required: true
|
||||
password:
|
||||
type: string
|
||||
required: true
|
||||
responses:
|
||||
"200":
|
||||
description: ok
|
||||
output:
|
||||
length: 2
|
||||
match:
|
||||
- level: error
|
||||
message: should be an array of property names required within an object schema
|
||||
path: [paths, "/", post, requestBody, content, application/json, schema, properties, emailOrNickname, required]
|
||||
source: structural
|
||||
- level: error
|
||||
message: should be an array of property names required within an object schema
|
||||
path: [paths, "/", post, requestBody, content, application/json, schema, properties, password, required]
|
||||
source: structural
|
||||
- name: "`type: bool` instead of `type: boolean`"
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: A thing
|
||||
license:
|
||||
name: AGPL-3.0
|
||||
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
ExcellentThing:
|
||||
description: "Something excellent"
|
||||
properties:
|
||||
extremelyExcellent:
|
||||
description: "If false, this item is only somewhat excellent"
|
||||
type: bool
|
||||
example: true
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
source: structural
|
||||
message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string
|
||||
path: [components, schemas, ExcellentThing, properties, extremelyExcellent, type]
|
||||
@@ -0,0 +1,25 @@
|
||||
meta:
|
||||
title: "#1672 - Header Security Scheme Object lacking a `name`"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: "#1672"
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas: {}
|
||||
securitySchemes:
|
||||
headerFoo:
|
||||
type: apiKey
|
||||
in: header
|
||||
links: {}
|
||||
callbacks: {}
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should have required property 'name'
|
||||
missingProperty: name
|
||||
path: [components, securitySchemes, headerFoo]
|
||||
source: structural
|
||||
@@ -0,0 +1,17 @@
|
||||
meta:
|
||||
title: "#1709 - relative (badly-formed) OIDC URIs"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: "#1709"
|
||||
version: "1.0"
|
||||
servers:
|
||||
- url: https://api.example.com/v2
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
openId:
|
||||
type: openIdConnect
|
||||
openIdConnectUrl: /.well-known/openid-configuration
|
||||
output:
|
||||
equal: null # no errors
|
||||
@@ -0,0 +1,74 @@
|
||||
meta:
|
||||
title: "#1711 - Misleading 2.0 Security Scheme object errors"
|
||||
cases:
|
||||
- name: oauth2 definitions with non-absolute URIs
|
||||
input:
|
||||
swagger: "2.0"
|
||||
paths: {}
|
||||
info:
|
||||
title: wow
|
||||
version: 1.0.0
|
||||
securityDefinitions:
|
||||
oauth2_Password:
|
||||
type: oauth2
|
||||
tokenUrl: /authorizationserver/oauth/token
|
||||
flow: password
|
||||
scopes:
|
||||
basic: ''
|
||||
oauth2_client_credentials:
|
||||
type: oauth2
|
||||
tokenUrl: /authorizationserver/oauth/token
|
||||
flow: application
|
||||
scopes:
|
||||
extended: ''
|
||||
output:
|
||||
length: 2
|
||||
match:
|
||||
- level: error
|
||||
message: should be an absolute URI
|
||||
path: [securityDefinitions, oauth2_Password, tokenUrl]
|
||||
source: structural
|
||||
- level: error
|
||||
message: should be an absolute URI
|
||||
path: [securityDefinitions, oauth2_client_credentials, tokenUrl]
|
||||
source: structural
|
||||
- name: apiKey definition missing `in` property
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: wow
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
securityDefinitions:
|
||||
apikey:
|
||||
type: apiKey
|
||||
name: myAuth
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should have required property 'in'
|
||||
missingProperty: in
|
||||
path: [securityDefinitions, apikey]
|
||||
source: structural
|
||||
- name: basic definition with a superfluous `in` property
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: wow
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
securityDefinitions:
|
||||
basic:
|
||||
type: basic
|
||||
in: header
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should NOT have additional properties
|
||||
additionalProperty: in
|
||||
path: [securityDefinitions, basic]
|
||||
source: structural
|
||||
@@ -0,0 +1,29 @@
|
||||
meta:
|
||||
title: "#1797 - Missing `required: true` within a 3.0 Path Parameter Object"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Foo OpenApi Spec
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
allowEmptyValue: true
|
||||
schema:
|
||||
type: integer
|
||||
format: int32
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should have required property 'required'
|
||||
missingProperty: required
|
||||
path: [paths, /, get, parameters, "0"]
|
||||
source: structural
|
||||
@@ -0,0 +1,37 @@
|
||||
meta:
|
||||
title: "#1808 - Misleading errors for a Responses Object within Operation Object"
|
||||
cases:
|
||||
- name: in OpenAPI 2.0
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
version: 0.0.0
|
||||
title: test
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses: {}
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should define at least one response
|
||||
path: [paths, /, get, responses]
|
||||
source: structural
|
||||
- name: in OpenAPI 3.0
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 0.0.0
|
||||
title: test
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses: {}
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should define at least one response
|
||||
path: [paths, /, get, responses]
|
||||
source: structural
|
||||
@@ -0,0 +1,20 @@
|
||||
meta:
|
||||
title: "#1832 - Schema Object with a non-valid type"
|
||||
input:
|
||||
swagger: '2.0'
|
||||
info:
|
||||
title: test
|
||||
version: 0.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
Model1:
|
||||
type: int
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string
|
||||
path: [definitions, Model1, type]
|
||||
source: structural
|
||||
@@ -0,0 +1,28 @@
|
||||
meta:
|
||||
title: "#1853 - OpenAPI 3.0 path parameter missing both `schema` and `content`"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Contact List API
|
||||
description: CRUD a simple Contact item.
|
||||
version: '0.1'
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
summary: Get a single contact by Id
|
||||
operationId: getContactById
|
||||
responses:
|
||||
"200":
|
||||
description: ok
|
||||
parameters:
|
||||
- name: contactId
|
||||
in: path
|
||||
description: ID of contact to return
|
||||
required: true
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- level: error
|
||||
message: should have either a `schema` or `content` property
|
||||
path: [paths, /, get, parameters, "0"]
|
||||
source: structural
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
|
||||
cases:
|
||||
- name: a valid additionalProperties schema
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
output:
|
||||
equal: null
|
||||
- name: a valid schema with an invalid additionalProperties schema
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: int
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string
|
||||
path: [definitions, mySchema, additionalProperties]
|
||||
- name: a valid schema with an invalid additionalProperties string value
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: "wow"
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: "should be either a Schema Object or a boolean value"
|
||||
path: [definitions, mySchema, additionalProperties]
|
||||
- name: a valid schema with a valid additionalProperties reference
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
$ref: "#"
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema with `additionalProperties: true`"
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema with `additionalProperties: false`"
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema without `additionalProperties`"
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
definitions:
|
||||
mySchema:
|
||||
type: object
|
||||
output:
|
||||
equal: null
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
|
||||
cases:
|
||||
- name: a valid additionalProperties schema
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
output:
|
||||
equal: null
|
||||
- name: a valid schema with an invalid additionalProperties schema
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: int
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string
|
||||
path: [components, schemas, mySchema, additionalProperties]
|
||||
- name: a valid schema with an invalid additionalProperties string value
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: "wow"
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: should be a Reference Object, Schema Object, or boolean value
|
||||
path: [components, schemas, mySchema, additionalProperties]
|
||||
- name: a valid schema with a valid additionalProperties reference
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties:
|
||||
$ref: "#"
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema with `additionalProperties: true`"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema with `additionalProperties: false`"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
output:
|
||||
equal: null
|
||||
- name: "a valid schema without `additionalProperties`"
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
mySchema:
|
||||
type: object
|
||||
output:
|
||||
equal: null
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
meta:
|
||||
title: 3.0 Parameter Object schema/content exclusivity
|
||||
cases:
|
||||
- name: both schema and content defined
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
parameters:
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many items to return at one time (max 100)
|
||||
schema:
|
||||
type: string
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: should have either a `schema` or `content` property
|
||||
- name: content defined with disallowed siblings also present
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
parameters:
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many items to return at one time (max 100)
|
||||
explode: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: "should not have `style`, `explode`, `allowReserved`, `example`, or `examples` when `content` is present"
|
||||
- name: neither schema or content defined
|
||||
input:
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
parameters:
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many items to return at one time (max 100)
|
||||
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: should have either a `schema` or `content` property
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
cases:
|
||||
- name: schema reference with additional property
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
description: ""
|
||||
schema:
|
||||
$ref: "abc"
|
||||
xyz: 123
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: |-
|
||||
should NOT have additional properties
|
||||
additionalProperty: xyz
|
||||
path: [paths, /, get, responses, 200, schema]
|
||||
|
||||
- name: schema with invalid `type`
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
description: ""
|
||||
schema:
|
||||
type: blah
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string, file
|
||||
- name: valid schema with invalid subschema type
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
description: ""
|
||||
schema:
|
||||
properties:
|
||||
a:
|
||||
type: str
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: |-
|
||||
should be equal to one of the allowed values
|
||||
allowedValues: array, boolean, integer, number, object, string
|
||||
- name: valid schema with no `type`
|
||||
input:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: ""
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
"200":
|
||||
description: ""
|
||||
schema:
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
output:
|
||||
equal: null
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
cases:
|
||||
- name: valid HTTP/bearer security scheme
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info: { title: "", version: 1.0.0 }
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
myScheme:
|
||||
type: http
|
||||
scheme: bearer
|
||||
output:
|
||||
equal: null
|
||||
- name: valid HTTP/bearer+bearerFormat security scheme
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info: { title: "", version: 1.0.0 }
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
myScheme:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: Bearer
|
||||
output:
|
||||
equal: null
|
||||
- name: valid HTTP/basic security scheme
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info: { title: "", version: 1.0.0 }
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
myScheme:
|
||||
type: http
|
||||
scheme: basic
|
||||
output:
|
||||
equal: null
|
||||
- name: invalid HTTP/basic+bearerFormat security scheme
|
||||
input:
|
||||
openapi: 3.0.0
|
||||
info: { title: "", version: 1.0.0 }
|
||||
paths: {}
|
||||
components:
|
||||
securitySchemes:
|
||||
myScheme:
|
||||
type: http
|
||||
scheme: basic
|
||||
bearerFormat: Bearer
|
||||
output:
|
||||
length: 1
|
||||
match:
|
||||
- message: "should NOT have a `bearerFormat` property without `scheme: bearer` being set"
|
||||
path: [components, securitySchemes, myScheme]
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
# The Editor's semantic validation handles this better than JSON Schema can,
|
||||
# so we want it to be disabled in the schema validator.
|
||||
|
||||
meta:
|
||||
title: "Tag Objects should not be validated for uniqueness"
|
||||
cases:
|
||||
- name: in Swagger 2.0
|
||||
input:
|
||||
swagger: '2.0'
|
||||
info:
|
||||
version: 0.0.0
|
||||
title: test
|
||||
tags:
|
||||
- name: pet
|
||||
- name: pet
|
||||
- name: pet
|
||||
description: Everything about your Pets
|
||||
paths: {}
|
||||
output:
|
||||
equal: null
|
||||
- name: in OpenAPI 3.0
|
||||
input:
|
||||
openapi: 3.0.2
|
||||
info:
|
||||
version: 0.0.0
|
||||
title: test
|
||||
tags:
|
||||
- name: pet
|
||||
- name: pet
|
||||
- name: pet
|
||||
description: Everything about your Pets
|
||||
paths: {}
|
||||
output:
|
||||
equal: null
|
||||
@@ -0,0 +1,922 @@
|
||||
import expect from "expect"
|
||||
import SwaggerUi from "swagger-ui"
|
||||
import ValidateBasePlugin from "plugins/validate-base"
|
||||
import ValidateSemanticPlugin from "plugins/validate-semantic"
|
||||
import ASTPlugin from "plugins/ast"
|
||||
|
||||
function getSystem(spec) {
|
||||
return new Promise((resolve) => {
|
||||
const system = SwaggerUi({
|
||||
spec,
|
||||
domNode: null,
|
||||
presets: [
|
||||
SwaggerUi.plugins.SpecIndex,
|
||||
SwaggerUi.plugins.ErrIndex,
|
||||
SwaggerUi.plugins.DownloadUrl,
|
||||
SwaggerUi.plugins.SwaggerJsIndex,
|
||||
],
|
||||
initialState: {
|
||||
layout: undefined
|
||||
},
|
||||
plugins: [
|
||||
ASTPlugin,
|
||||
ValidateBasePlugin,
|
||||
ValidateSemanticPlugin,
|
||||
() => ({
|
||||
statePlugins: {
|
||||
configs: {
|
||||
actions: {
|
||||
loaded: () => {
|
||||
return {
|
||||
type: "noop"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
]
|
||||
})
|
||||
resolve(system)
|
||||
})
|
||||
}
|
||||
|
||||
describe("validation plugin - selectors", function() {
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
describe("allSchemas", function() {
|
||||
describe("OpenAPI 2.0", function() {
|
||||
it("should pick up parameter schemas", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
test: {
|
||||
parameters: [{
|
||||
name: "common"
|
||||
}],
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "tags"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].path).toEqual(["paths","test","parameters","0"])
|
||||
expect(nodes[1].path).toEqual(["paths","test","get","parameters","0"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up response schemas", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
test: {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path.join(".")).toEqual("paths.test.get.responses.200.schema")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up global definitions", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
properties: {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up global definitions named 'x-' (i.e. not consider them extensions)", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
"x-fooModel": {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("x-fooModel")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up response headers", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
test: {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
headers: {
|
||||
foo: {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path.join(".")).toEqual("paths.test.get.responses.200.headers.foo")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in properties", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in additionalProperties - simple", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("additionalProperties")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in additionalProperties - complex", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(3)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("additionalProperties")
|
||||
expect(nodes[2].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in array", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("items")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in array of objects", () => {
|
||||
const spec = {
|
||||
definitions: {
|
||||
fooModel: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(3)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("items")
|
||||
expect(nodes[2].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("OpenAPI 3.0", function() {
|
||||
it("should pick up schemas in 'components'", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up schemas named 'x-' (i.e. not consider them extensions)", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
"x-fooModel": {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("x-fooModel")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up request body schemas and response schemas", () => {
|
||||
const spec = {
|
||||
"openapi": "3.0.0",
|
||||
"paths": {
|
||||
"/ping": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/myRequestMediaType": {
|
||||
"schema": {
|
||||
"type": "array"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/myResponseMediaType": {
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].key).toEqual("schema")
|
||||
expect(nodes[0].parent.key).toEqual("application/myRequestMediaType")
|
||||
expect(nodes[1].key).toEqual("schema")
|
||||
expect(nodes[1].parent.key).toEqual("application/myResponseMediaType")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up schemas in response components", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
responses: {
|
||||
MyResponse: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["components","responses","MyResponse","content","application/json","schema"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in properties", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in additionalProperties - simple", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("additionalProperties")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in additionalProperties - complex", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(3)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("additionalProperties")
|
||||
expect(nodes[2].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in array", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("items")
|
||||
})
|
||||
})
|
||||
|
||||
it("should pick up subschemas in array of objects", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
fooModel: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSchemas())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(3)
|
||||
expect(nodes[0].key).toEqual("fooModel")
|
||||
expect(nodes[1].key).toEqual("items")
|
||||
expect(nodes[2].key).toEqual("foo")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("allResponses", function() {
|
||||
it("should pick up operation responses with specific codes like 200", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["paths","/foo","get","responses","200"])
|
||||
})
|
||||
})
|
||||
it("should pick up the 'default' response", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
default: {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["paths","/foo","get","responses","default"])
|
||||
})
|
||||
})
|
||||
it("should pick up OAS3 response code ranges like 2XX", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"2XX": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["paths","/foo","get","responses","2XX"])
|
||||
})
|
||||
})
|
||||
it("should ignore x- extension keys in an operation's `responses` section", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"x-ext": {
|
||||
foo: "bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should pick up OpenAPI 2 global response definitions", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
responses: {
|
||||
OkResponse: {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["responses","OkResponse"])
|
||||
})
|
||||
})
|
||||
it("should pick up OpenAPI 2 response definitions named 'x-'", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
responses: {
|
||||
"x-MyResponse": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["responses","x-MyResponse"])
|
||||
})
|
||||
})
|
||||
it("should pick up OpenAPI 3 response components", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components : {
|
||||
responses: {
|
||||
OkResponse: {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["components","responses","OkResponse"])
|
||||
})
|
||||
})
|
||||
it("should pick up OpenAPI 3 response components named 'x-'", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components : {
|
||||
responses: {
|
||||
"x-MyResponse": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allResponses())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].path).toEqual(["components","responses","x-MyResponse"])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("allSecurityDefinitions", () => {
|
||||
it("should pick up OAS2 security schemes", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
securityDefinitions: {
|
||||
basicAuth: {
|
||||
type: "basic"
|
||||
},
|
||||
apiKeyAuth: {
|
||||
type: "apiKey"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].key).toEqual("basicAuth")
|
||||
expect(nodes[1].key).toEqual("apiKeyAuth")
|
||||
})
|
||||
})
|
||||
it("should pick up OAS3 security schemes", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
securitySchemes: {
|
||||
basicAuth: {
|
||||
type: "http"
|
||||
},
|
||||
apiKeyAuth: {
|
||||
type: "apiKey"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].key).toEqual("basicAuth")
|
||||
expect(nodes[1].key).toEqual("apiKeyAuth")
|
||||
})
|
||||
})
|
||||
it("should pick up OAS2 security schemes named x- (i.e. not consider them extensions)", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
securityDefinitions: {
|
||||
"x-auth": {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("x-auth")
|
||||
})
|
||||
})
|
||||
it("should pick up OAS3 security schemes named x- (i.e. not consider them extensions)", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
securitySchemes: {
|
||||
"x-auth": {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(1)
|
||||
expect(nodes[0].key).toEqual("x-auth")
|
||||
})
|
||||
})
|
||||
it("should not pick up arbitrary OAS2 nodes named `securityDefinitions`", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
definitions: {
|
||||
securityDefinitions: {
|
||||
type: "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should not pick up arbitrary OAS3 nodes named `securitySchemes`", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
securitySchemes: {
|
||||
type: "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityDefinitions())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("allSecurityRequirements", () => {
|
||||
it("should pick up global security requirements", () => {
|
||||
const spec = {
|
||||
security: [
|
||||
{
|
||||
auth1: []
|
||||
},
|
||||
{
|
||||
auth2: [],
|
||||
auth3: []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityRequirements())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].node).toEqual(
|
||||
{ "auth1": [] }
|
||||
)
|
||||
expect(nodes[1].node).toEqual(
|
||||
{ "auth2": [], "auth3": [] }
|
||||
)
|
||||
})
|
||||
})
|
||||
it("should pick up operation-level security requirements", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
security: [
|
||||
{
|
||||
auth1: []
|
||||
},
|
||||
{
|
||||
auth2: [],
|
||||
auth3: []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityRequirements())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].node).toEqual(
|
||||
{ "auth1": [] }
|
||||
)
|
||||
expect(nodes[1].node).toEqual(
|
||||
{ "auth2": [], "auth3": [] }
|
||||
)
|
||||
})
|
||||
})
|
||||
it("should pick up empty security requirements", () => {
|
||||
const spec = {
|
||||
security: [
|
||||
{}
|
||||
],
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
security: [
|
||||
{}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityRequirements())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(2)
|
||||
expect(nodes[0].node).toNotBe(nodes[1].node)
|
||||
expect(nodes[0].node).toEqual({})
|
||||
expect(nodes[1].node).toEqual({})
|
||||
})
|
||||
})
|
||||
it("should not pick up the `security` key inside extensions", () => {
|
||||
const spec = {
|
||||
paths: {
|
||||
"/": {
|
||||
"x-ext1": {
|
||||
security: [
|
||||
{ foo: [] }
|
||||
]
|
||||
}
|
||||
},
|
||||
"x-ext2": {
|
||||
get: {
|
||||
security: [
|
||||
{ bar: [] }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getSystem(spec)
|
||||
.then(system => system.validateSelectors.allSecurityRequirements())
|
||||
.then(nodes => {
|
||||
expect(nodes.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,94 @@
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - 2and3 operations", () => {
|
||||
describe("Operations must have unique operationIds", () => {
|
||||
describe("OpenAPI 3.0", () => {
|
||||
it("should return an error when operationId collisions exist", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
operationId: "myId"
|
||||
},
|
||||
post: {
|
||||
operationId: "myId"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Operations must have unique operationIds.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "post", "operationId"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when operationId collisions don't exist", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
operationId: "myId1"
|
||||
},
|
||||
post: {
|
||||
operationId: "myId2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
describe("Swagger 2.0", () => {
|
||||
it("should return an error when operationId collisions exist", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
operationId: "myId"
|
||||
},
|
||||
post: {
|
||||
operationId: "myId"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Operations must have unique operationIds.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "post", "operationId"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when operationId collisions don't exist", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
operationId: "myId1"
|
||||
},
|
||||
post: {
|
||||
operationId: "myId2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,997 @@
|
||||
import expect from "expect"
|
||||
import validateHelper from "../validate-helper.js"
|
||||
|
||||
describe(`validation plugin - semantic - 2and3 parameters`, () => {
|
||||
describe(`parameters must have unique name + in values`, () => {
|
||||
describe(`direct siblings`, () => {
|
||||
it("should return an error for an invalid Swagger 2 definition", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "pathLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "pathLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "opLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "opLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
const secondError = allErrors[1]
|
||||
expect(allErrors.length).toEqual(2)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "parameters", "1"])
|
||||
expect(firstError.message).toEqual("Sibling parameters must have unique name + in values")
|
||||
expect(secondError.path).toEqual(["paths", "/pets", "get", "parameters", "1"])
|
||||
expect(secondError.message).toEqual("Sibling parameters must have unique name + in values")
|
||||
})
|
||||
})
|
||||
it("should return an error for an invalid OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "pathLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "pathLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "opLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "opLevel",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
const secondError = allErrors[1]
|
||||
expect(allErrors.length).toEqual(2)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "parameters", "1"])
|
||||
expect(firstError.message).toEqual("Sibling parameters must have unique name + in values")
|
||||
expect(secondError.path).toEqual(["paths", "/pets", "get", "parameters", "1"])
|
||||
expect(secondError.message).toEqual("Sibling parameters must have unique name + in values")
|
||||
})
|
||||
})
|
||||
it("should return no errors for a valid Swagger 2 definition", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "wags",
|
||||
"in": "query",
|
||||
"description": "wags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "tags",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for a valid OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "wags",
|
||||
"in": "query",
|
||||
"description": "wags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "tags",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "string"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
describe(`inherited siblings`, () => {
|
||||
it("should return no errors for a valid Swagger 2 definition due to inheritance", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
parameters: {
|
||||
MyParam: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "three",
|
||||
in: "query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for a valid OpenAPI 3 definition due to inheritance", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
parameters: {
|
||||
MyParam: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "three",
|
||||
in: "query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should not return an error for root parameters in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
parameters: {
|
||||
MyParam: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "otherParam",
|
||||
in: "query"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should not return an error for root parameters in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
parameters: {
|
||||
MyParam: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "otherParam",
|
||||
in: "query"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for a valid Swagger 2 definition", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
parameters: {
|
||||
MyParamOne: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
},
|
||||
MyParamTwo: {
|
||||
name: "anotherParam1",
|
||||
in: "query"
|
||||
},
|
||||
},
|
||||
"paths": {
|
||||
"/pets/{one}/{two}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "anotherParam2",
|
||||
in: "query"
|
||||
},
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "two",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "three",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "anotherParam3",
|
||||
in: "query"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors for a valid OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
parameters: {
|
||||
MyParamOne: {
|
||||
name: "one",
|
||||
in: "query"
|
||||
},
|
||||
MyParamTwo: {
|
||||
name: "anotherParam1",
|
||||
in: "query"
|
||||
},
|
||||
},
|
||||
"paths": {
|
||||
"/pets/{one}/{two}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "one",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "two",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "anotherParam2",
|
||||
in: "query"
|
||||
},
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "two",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "three",
|
||||
in: "query"
|
||||
},
|
||||
{
|
||||
name: "anotherParam3",
|
||||
in: "query"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
describe(`parameter defaults must be present in enums`, () => {
|
||||
it("should return an error for an invalid Swagger 2 definition", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
enum: [1, 2, 3],
|
||||
default: 0
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0", "default"])
|
||||
expect(firstError.message).toEqual("Default values must be present in `enum`")
|
||||
})
|
||||
})
|
||||
it("should return an error for an invalid OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
schema: {
|
||||
enum: [1, 2, 3],
|
||||
default: 0
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0", "schema", "default"])
|
||||
expect(firstError.message).toEqual("Default values must be present in `enum`")
|
||||
})
|
||||
})
|
||||
it("should return an error for an invalid OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
parameters: {
|
||||
MyParam: {
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
schema: {
|
||||
enum: [1, 2, 3],
|
||||
default: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.path).toEqual(["components", "parameters", "MyParam", "schema", "default"])
|
||||
expect(firstError.message).toEqual("Default values must be present in `enum`")
|
||||
})
|
||||
})
|
||||
it("should return no errors for a Swagger 2 definition without default set", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
enum: [1, 2, 3]
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for an OpenAPI 3 definition without default set", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
schema: {
|
||||
enum: [1, 2, 3]
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for a Swagger 2 definition without enum set", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
default: 0
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should return no errors for an OpenAPI 3 definition without enum set", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "num",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
schema: {
|
||||
default: 0
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe(`path parameters must be in path definition`, () => {
|
||||
it("should return no errors for a valid Swagger 2 definition", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
info: {
|
||||
"title": "Correct path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo/{param1}/{param2}/{param3}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/parameters/param3"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
param3: {
|
||||
name: "param3",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return 2 errors for each path parameter that isn't in the path in spec 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
info: {
|
||||
"title": "Unused path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(2)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "parameters", "0","name"])
|
||||
expect(firstError.message).toEqual(`Path parameter "param1" must have the corresponding {param1} segment in the "/foo" path`)
|
||||
const secondError = allErrors[1]
|
||||
expect(secondError.path).toEqual(["paths", "/foo", "get", "parameters", "0","name"])
|
||||
expect(secondError.message).toEqual(`Path parameter "param2" must have the corresponding {param2} segment in the "/foo" path`)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return 2 errors for each referenced path parameter that isn't in the path for 2 spec", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
info: {
|
||||
"title": "Unused path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo/{param1}/{param2}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/parameters/param3"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/parameters/param4"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
param3: {
|
||||
name: "param3",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
param4: {
|
||||
name: "param4",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(2)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/foo/{param1}/{param2}", "parameters", "1","name"])
|
||||
expect(firstError.message).toEqual(`Path parameter "param3" must have the corresponding {param3} segment in the "/foo/{param1}/{param2}" path`)
|
||||
const secondError = allErrors[1]
|
||||
expect(secondError.path).toEqual(["paths", "/foo/{param1}/{param2}","get", "parameters", "1","name"])
|
||||
expect(secondError.message).toEqual(`Path parameter "param4" must have the corresponding {param4} segment in the "/foo/{param1}/{param2}" path`)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no errors for a valid Swagger 3 definition", () => {
|
||||
const spec = {
|
||||
swagger: "3.0",
|
||||
info: {
|
||||
"title": "Correct path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo/{param1}/{param2}/{param3}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/parameters/param3"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
parameters: {
|
||||
param3: {
|
||||
name: "param3",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return 2 errors for each path parameter that isn't in the path in spec 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.2",
|
||||
info: {
|
||||
"title": "Unused path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(2)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "parameters", "0","name"])
|
||||
expect(firstError.message).toEqual(`Path parameter "param1" must have the corresponding {param1} segment in the "/foo" path`)
|
||||
const secondError = allErrors[1]
|
||||
expect(secondError.path).toEqual(["paths", "/foo", "get", "parameters", "0","name"])
|
||||
expect(secondError.message).toEqual(`Path parameter "param2" must have the corresponding {param2} segment in the "/foo" path`)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return 2 errors for each referenced path parameter that isn't in the path for 3 spec", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.2",
|
||||
info: {
|
||||
"title": "Unused path parameters in path",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/foo/{param1}/{param2}": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param1",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/components/parameters/param3"
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
name: "param2",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
{
|
||||
$ref: "#/components/parameters/param4"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
"description": "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
parameters: {
|
||||
param3: {
|
||||
name: "param3",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
},
|
||||
param4: {
|
||||
name: "param4",
|
||||
in: "path",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(2)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/foo/{param1}/{param2}", "parameters", "1","name"])
|
||||
expect(firstError.message).toEqual(`Path parameter "param3" must have the corresponding {param3} segment in the "/foo/{param1}/{param2}" path`)
|
||||
const secondError = allErrors[1]
|
||||
expect(secondError.path).toEqual(["paths", "/foo/{param1}/{param2}","get", "parameters", "1","name"])
|
||||
expect(secondError.message).toEqual(`Path parameter "param4" must have the corresponding {param4} segment in the "/foo/{param1}/{param2}" path`)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,484 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - 2and3 paths", () => {
|
||||
describe("Paths cannot have query strings in them", () => {
|
||||
it("should return one problem for an stray '?' in a Swagger 2 path string", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/report?asdf=123": {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Query strings in paths are not allowed.")
|
||||
expect(firstError.path).toEqual(["paths", "/report?asdf=123"])
|
||||
})
|
||||
})
|
||||
it("should return one problem for an stray '?' in an OpenAPI 3 path string", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/report?asdf=123": {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Query strings in paths are not allowed.")
|
||||
expect(firstError.path).toEqual(["paths", "/report?asdf=123"])
|
||||
})
|
||||
})
|
||||
it("should return no problems for a correct Swagger 2 path template", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
it("should return no problems for a correct OpenAPI 3 path template", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Path parameter definitions need matching paramater declarations", function(){
|
||||
describe("OpenAPI 3", () => {
|
||||
it("should not return problems for a valid path-level definiton/declaration pair", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return problems for a valid path-level definiton/declaration pair using a $ref", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [
|
||||
{ $ref: "#/parameters/id" }
|
||||
]
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
id: {
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return problems for a valid operation-level definiton/declaration pair", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should return one problem for a path parameter defined at the operation level that is not present within every operation on the path", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
},
|
||||
post: {
|
||||
description: "the path parameter definition is missing here"
|
||||
},
|
||||
delete: {
|
||||
description: "the path parameter definition is missing here"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( `Declared path parameter \"id\" needs to be defined within every operation in the path (missing in "post", "delete"), or moved to the path-level parameters object`)
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return one problem when the definition is completely absent", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should return one error when no parameters are defined", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should return one problem for a missed 'in' value", function(){
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
// in: "path",
|
||||
description: "An id"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("Swagger 2", () => {
|
||||
it("should not return problems for a valid path-level definiton/declaration pair", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return problems for a valid path-level definiton/declaration pair using a $ref", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [
|
||||
{ $ref: "#/parameters/id" }
|
||||
]
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
id: {
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return problems for a valid operation-level definiton/declaration pair", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should return one problem for a path parameter defined at the operation level that is not present within every operation on the path", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
description: "An id",
|
||||
required: true
|
||||
}]
|
||||
},
|
||||
post: {
|
||||
description: "the path parameter definition is missing here"
|
||||
},
|
||||
delete: {
|
||||
description: "the path parameter definition is missing here"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( `Declared path parameter \"id\" needs to be defined within every operation in the path (missing in "post", "delete"), or moved to the path-level parameters object`)
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return one problem when the definition is completely absent", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should return one error when no parameters are defined", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
info: {
|
||||
title: "test",
|
||||
version: "1.0.0"
|
||||
},
|
||||
paths: {
|
||||
"/{12345instanceABCDE_instance_12345}": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"12345instanceABCDE_instance_12345\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/{12345instanceABCDE_instance_12345}"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should return a well-formed error when ", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it.skip("should return one problem for a missed 'in' value", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
// in: "path",
|
||||
description: "An id"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a specific error for parameters with different casing characters OpenAPI 3 definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.1",
|
||||
"paths": {
|
||||
"/{myParam}": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "myparam",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual("Parameter names are case-sensitive. The parameter named \"myparam\" does not match the case used in the path \"/{myParam}\".")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no errors for parameters with same characters in path and parameters definition", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.1",
|
||||
"paths": {
|
||||
"/{myParam}": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "myParam",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,698 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - 2and3 refs", function() {
|
||||
this.timeout(10 * 1000)
|
||||
describe("Ref siblings", () => {
|
||||
|
||||
it("should return a warning when another property is a sibling of a $ref in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/components/schemas/abc",
|
||||
description: "My very cool get"
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("Sibling values alongside $refs are ignored.\nTo add properties to a $ref, wrap the $ref into allOf, or move the extra properties into the referenced definition (if applicable).")
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "get", "description"])
|
||||
})
|
||||
})
|
||||
it("should return a warning when another property is a sibling of a $ref in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/definitions/abc",
|
||||
description: "My very cool get"
|
||||
}
|
||||
}
|
||||
},
|
||||
definitions: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("Sibling values alongside $refs are ignored.\nTo add properties to a $ref, wrap the $ref into allOf, or move the extra properties into the referenced definition (if applicable).")
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "get", "description"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no warnings when a $ref has no siblings in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/components/schemas/abc"
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no warnings when a $ref has no siblings in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/definitions/abc"
|
||||
}
|
||||
}
|
||||
},
|
||||
definitions: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no warnings when a path item $ref has siblings in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
$ref: "#/components/schemas/abc",
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/components/schemas/abc"
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no warnings when a path item $ref has siblings in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
$ref: "#/definitions/abc",
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
$ref: "#/definitions/abc"
|
||||
}
|
||||
}
|
||||
},
|
||||
definitions: {
|
||||
abc: {}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
})
|
||||
describe("Unused definitions", () => {
|
||||
it("should return a warning when a definition is declared but not used in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
"x-Foo": {
|
||||
type: "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("Definition was declared but never used in document")
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.path).toEqual(["components", "schemas", "x-Foo"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return a warning when a definition with special characters is properly referenced in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/x~1Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/x~0Bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
"x/Foo": {
|
||||
type: "object"
|
||||
},
|
||||
"x~Bar": {
|
||||
type: "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
// We want warnings only, without errors about invalid component names
|
||||
const allWarnings = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.level === "warning")
|
||||
expect(allWarnings.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a warning when a definition is declared but not used in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {}
|
||||
},
|
||||
definitions: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("Definition was declared but never used in document")
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.path).toEqual(["definitions", "abc"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return a warning when a definition with special character is declared and used in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
responses: {
|
||||
200: {
|
||||
schema: {
|
||||
$ref: "#/definitions/x~1Foo"
|
||||
}
|
||||
},
|
||||
400: {
|
||||
schema: {
|
||||
$ref: "#/definitions/x~0Bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
definitions: {
|
||||
"x/Foo": {
|
||||
type: "object"
|
||||
},
|
||||
"x~Bar": {
|
||||
type: "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
describe("Malformed $ref values", () => {
|
||||
it("should return an error when a JSON pointer lacks a leading `#/` in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#myObj/abc"
|
||||
}
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("$ref paths must begin with `#/`")
|
||||
expect(firstError.level).toEqual("error")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should return an error when a JSON pointer lacks a leading `#/` in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#myObj/abc"
|
||||
}
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("$ref paths must begin with `#/`")
|
||||
expect(firstError.level).toEqual("error")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer is a well-formed remote reference in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "http://google.com#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string",
|
||||
properties: {
|
||||
$ref: "http://google.com/MyRegularURLReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer is a well-formed remote reference in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "http://google.com#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string",
|
||||
properties: {
|
||||
$ref: "http://google.com/MyRegularURLReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return an error when a JSON pointer uses incorrect percent-encoding in Swagger 2", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/foo bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"foo bar": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors[0]).toInclude({
|
||||
level: "warning",
|
||||
message: "Definition was declared but never used in document",
|
||||
path: ["definitions", "foo bar"]
|
||||
})
|
||||
expect(allSemanticErrors.length).toEqual(2)
|
||||
expect(allSemanticErrors[1]).toInclude({
|
||||
level: "error",
|
||||
message: "$ref values must be RFC3986-compliant percent-encoded URIs",
|
||||
path: ["paths", "/foo", "get", "responses", "200", "schema", "$ref"]
|
||||
})
|
||||
})
|
||||
})
|
||||
it("should return an error when a JSON pointer uses incorrect percent-encoding in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
"openapi": "3.0.0",
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/foo bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
"foo bar": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors.length).toEqual(3)
|
||||
expect(allSemanticErrors[0]).toInclude({
|
||||
level: "warning",
|
||||
message: "Definition was declared but never used in document",
|
||||
path: ["components", "schemas", "foo bar"]
|
||||
})
|
||||
expect(allSemanticErrors[1]).toInclude({
|
||||
level: "error",
|
||||
message: "$ref values must be RFC3986-compliant percent-encoded URIs",
|
||||
path: ["paths", "/foo", "get", "responses", "200", "content", "application/json", "schema", "$ref"]
|
||||
}),
|
||||
expect(allSemanticErrors[2]).toInclude({
|
||||
level: "error",
|
||||
message: "Component names can only contain the characters A-Z a-z 0-9 - . _",
|
||||
path: ["components", "schemas", "foo bar"]
|
||||
})
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer uses correct percent-encoding in Swagger 2", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/foo%20bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"foo bar": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer uses correct percent-encoding in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
"openapi": "3.0.0",
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/foo%20bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
"foo bar": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors.length).toEqual(1)
|
||||
expect(allSemanticErrors[0]).toInclude({
|
||||
level: "error",
|
||||
message: "Component names can only contain the characters A-Z a-z 0-9 - . _",
|
||||
path: ["components", "schemas", "foo bar"]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
describe("Nonexistent $ref pointers", () => {
|
||||
it("should return an error when a local JSON pointer does not exist in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#/myObj/DoesNotExist"
|
||||
}
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string",
|
||||
properties: {
|
||||
$ref: "http://google.com/MyRegularURLReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("$refs must reference a valid location in the document")
|
||||
expect(firstError.level).toEqual("error")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should return an error when a local JSON pointer does not exist in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#/myObj/DoesNotExist"
|
||||
}
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string",
|
||||
properties: {
|
||||
$ref: "http://google.com/MyRegularURLReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
.filter((el, i, arr) => arr.indexOf(el) === i)
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toMatch("$refs must reference a valid location in the document")
|
||||
expect(firstError.level).toEqual("error")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer exists in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer exists in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string",
|
||||
properties: {
|
||||
$ref: "http://google.com/MyRegularURLReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer is a remote reference in Swagger 2", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "http://google.com#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a JSON pointer is a remote reference in OpenAPI 3", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
$ref: "http://google.com#/myObj/abc"
|
||||
},
|
||||
},
|
||||
myObj: {
|
||||
abc: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allSemanticErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.source !== "resolver")
|
||||
expect(allSemanticErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,217 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - 2and3 security", () => {
|
||||
it("should return an error when top-level security references a non-existing security scheme", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
security: [
|
||||
{
|
||||
fictional_security_definition: [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["security", "0"])
|
||||
expect(firstError.message).toMatch("Security requirements must match a security definition")
|
||||
})
|
||||
})
|
||||
|
||||
it("should return an error when an operation references a non-existing security scheme", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
security: [
|
||||
{
|
||||
fictional_security_definition: [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "security", "0"])
|
||||
expect(firstError.message).toMatch("Security requirements must match a security definition")
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a warning when a security scheme is defined but not used in OpenAPI 2.0", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
securityDefinitions: {
|
||||
auth: {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.message).toMatch("Security scheme was defined but never used.")
|
||||
expect(firstError.path).toEqual(["securityDefinitions", "auth"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a warning when a security scheme is defined but not used in OpenAPI 3.0", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
securitySchemes: {
|
||||
auth: {
|
||||
type: "http"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.level).toEqual("warning")
|
||||
expect(firstError.message).toMatch("Security scheme was defined but never used.")
|
||||
expect(firstError.path).toEqual(["components", "securitySchemes", "auth"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no errors when a security scheme is defined and referenced globally in OpenAPI 2.0", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
security: [
|
||||
{ auth: [] }
|
||||
],
|
||||
securityDefinitions: {
|
||||
auth: {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors when a security scheme is defined and used in an operation in OpenAPI 2.0", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
security: [
|
||||
{ auth: [] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
securityDefinitions: {
|
||||
auth: {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors when a security scheme is defined and referenced globally in OpenAPI 3.0", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
security: [
|
||||
{ auth: [] }
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
auth: {
|
||||
type: "http"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors when a security scheme is defined and used in an operation in OpenAPI 3.0", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
security: [
|
||||
{ auth: [] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
securitySchemes: {
|
||||
auth: {
|
||||
type: "http"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errrors when `security` contains multiple requirements combined using logical OR", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
security: [
|
||||
{},
|
||||
{ auth: [] }
|
||||
],
|
||||
securityDefinitions: {
|
||||
auth: {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors when security schemes are combined using logical AND", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
security: [
|
||||
{
|
||||
auth1: [],
|
||||
auth2: []
|
||||
}
|
||||
],
|
||||
securityDefinitions: {
|
||||
auth1: {
|
||||
type: "apiKey"
|
||||
},
|
||||
auth2: {
|
||||
type: "apiKey"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - 2and3 tags", () => {
|
||||
describe("global `tags` array", () => {
|
||||
describe("OpenAPI 3.0", () => {
|
||||
it("should return an error when two tag objects are equivalent", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return validateHelper(spec).then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual("Tag Objects must have unique `name` field values.")
|
||||
expect(firstError.path).toEqual(["tags", "1"])
|
||||
})
|
||||
})
|
||||
it("should return an error when two tag objects have the same name", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "pet",
|
||||
description: "Everything about your pets",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return validateHelper(spec).then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual("Tag Objects must have unique `name` field values.")
|
||||
expect(firstError.path).toEqual(["tags", "1"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when two tags have unique names", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "store",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
describe("Swagger 2.0", () => {
|
||||
it("should return an error when two tag objects are equivalent", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return validateHelper(spec).then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual("Tag Objects must have unique `name` field values.")
|
||||
expect(firstError.path).toEqual(["tags", "1"])
|
||||
})
|
||||
})
|
||||
it("should return an error when two tag objects have the same name", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "pet",
|
||||
description: "Everything about your pets",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return validateHelper(spec).then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual("Tag Objects must have unique `name` field values.")
|
||||
expect(firstError.path).toEqual(["tags", "1"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when two tags have unique names", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
tags: [
|
||||
{
|
||||
name: "pet",
|
||||
},
|
||||
{
|
||||
name: "store",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,50 @@
|
||||
import { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("editor bug #1817 - path parameter semantic error with TRACE", function() {
|
||||
it("should return no problems for a path parameter defined in a Swagger 2 TRACE operation", function () {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
trace: {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
it("should return no problems for a path parameter defined in an OpenAPI 3 TRACE operation", function () {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
trace: {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,532 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrors } from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - form data", function(){
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
describe("/parameters/...", function(){
|
||||
describe("typo in formdata", function(){
|
||||
it("should warn about formdata ( typo )", function(){
|
||||
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
parameters: {
|
||||
CoolParam: [
|
||||
{ in: "formdata" },
|
||||
]
|
||||
},
|
||||
paths: {
|
||||
"/some": {
|
||||
post: {
|
||||
parameters: [
|
||||
{ in: "formdata" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Parameter "in: formdata" is invalid, did you mean "in: formData"?`)
|
||||
expect(firstError.path).toEqual(["paths", "/some", "post", "parameters", "0"])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("missing consumes", function(){
|
||||
it("should complain if 'type:file` and no 'in: formData", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/some": {
|
||||
post: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{
|
||||
type: "file",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Parameters with "type: file" must have "in: formData"`)
|
||||
expect(firstError.path).toEqual(["paths", "/some", "post", "parameters", "0"])
|
||||
})
|
||||
})
|
||||
it("should complain if 'type:file` and no consumes - 'multipart/form-data'", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/some": {
|
||||
post: {
|
||||
parameters: [
|
||||
{
|
||||
in: "formData",
|
||||
type: "file",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "type: file" must include "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/some", "post"])
|
||||
})
|
||||
})
|
||||
it("should complain if 'in:formData` and no consumes - 'multipart/form-data' or 'application/x-www-form-urlencoded'", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/some": {
|
||||
post: {
|
||||
parameters: [
|
||||
{
|
||||
in: "formData",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/some", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not complain if 'in:formData` and consumes is set globally", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
consumes: [
|
||||
"multipart/form-data"
|
||||
],
|
||||
paths: {
|
||||
"/some": {
|
||||
post: {
|
||||
parameters: [
|
||||
{
|
||||
in: "formData",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("/pathitems/...", function(){
|
||||
it("should complain about having both in the same parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "formData" },
|
||||
{ in: "body" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Parameters cannot have both a "in: body" and "in: formData", as "formData" _will_ be the body`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "parameters"])
|
||||
})
|
||||
})
|
||||
it("should complain if 'type:file` and no 'in: formData", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ type: "file" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Parameters with "type: file" must have "in: formData"`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "parameters", "0"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("Path-level form parameters and operation-level consumes", function(){
|
||||
describe("`in: formData` + `type: file`", function() {
|
||||
it("should report an error for missing consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "file"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "type: file" must include "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should report an error for incorrect global consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
consumes: ["application/json"],
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "file"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "type: file" must include "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should report an error for incorrect operation-level consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "file"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
consumes: ["application/json"],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "type: file" must include "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not report an error for correct global consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
consumes: ["multipart/form-data"],
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "file"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not report an error for correct operation consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "file"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
consumes: ["multipart/form-data"],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
describe("`in: formData`", function() {
|
||||
it("should report an error for missing consumes with a path-level formData parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should report an error for incorrect global consumes with a path-level formData parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
consumes: ["application/json"],
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should report an error for incorrect operation-level consumes with a path-level formData parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
consumes: ["application/json"],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Operations with parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not report an error for correct global consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
consumes: ["multipart/form-data"],
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not report an error for correct operation consumes with a path-level parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
name: "param",
|
||||
in: "formData",
|
||||
required: true,
|
||||
type: "string"
|
||||
}
|
||||
],
|
||||
post: {
|
||||
consumes: ["multipart/form-data"],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,228 @@
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - oas3 components", () => {
|
||||
describe("OAS3 component names must consist of allowed characters", () => {
|
||||
it("should return an error when OAS3 component names contain forbidden characters", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
"Foo«Bar»": {}
|
||||
},
|
||||
parameters: {
|
||||
"Foo«Bar»": {
|
||||
in: "query",
|
||||
name: "foo",
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
"Foo«Bar»": {
|
||||
description: "ok"
|
||||
}
|
||||
},
|
||||
examples: {
|
||||
"Foo«Bar»": {
|
||||
value: 1
|
||||
}
|
||||
},
|
||||
requestBodies: {
|
||||
"Foo«Bar»": {
|
||||
content: {
|
||||
"text/plain": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
"Foo«Bar»": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
securitySchemes: {
|
||||
"Foo«Bar»": {
|
||||
type: "http",
|
||||
scheme: "basic"
|
||||
}
|
||||
},
|
||||
callbacks: {
|
||||
"Foo«Bar»": {
|
||||
"{$request.body#/callbackUrl}": {}
|
||||
}
|
||||
},
|
||||
links: {
|
||||
"Foo«Bar»": {
|
||||
operationId: "getUser"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
// We want errors only, without "unused definition" warnings
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
.filter(err => err.level === "error")
|
||||
|
||||
const errorMessage = "Component names can only contain the characters A-Z a-z 0-9 - . _"
|
||||
|
||||
expect(allErrors.length).toEqual(9)
|
||||
expect(allErrors[0]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "schemas", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[1]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "parameters", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[2]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "responses", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[3]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "examples", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[4]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "requestBodies", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[5]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "headers", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[6]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "securitySchemes", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[7]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "callbacks", "Foo«Bar»"]
|
||||
})
|
||||
expect(allErrors[8]).toInclude({
|
||||
level: "error",
|
||||
message: errorMessage,
|
||||
path: ["components", "links", "Foo«Bar»"]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return errors when OAS3 component names consist of allowed characters", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
"A-a.0_": {}
|
||||
},
|
||||
parameters: {
|
||||
"B-b.1_": {
|
||||
in: "query",
|
||||
name: "foo",
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
"C-c.3_": {
|
||||
description: "ok"
|
||||
}
|
||||
},
|
||||
examples: {
|
||||
"D-d.4_": {
|
||||
value: 1
|
||||
}
|
||||
},
|
||||
requestBodies: {
|
||||
"E-e.5_": {
|
||||
content: {
|
||||
"test/plain": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
"F-f.6_": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
securitySchemes: {
|
||||
"G-g.7_": {
|
||||
type: "http",
|
||||
scheme: "basic"
|
||||
}
|
||||
},
|
||||
callbacks: {
|
||||
"H-h.8_": {
|
||||
"{$request.body#/callbackUrl}": {}
|
||||
}
|
||||
},
|
||||
links: {
|
||||
"I-i.9_": {
|
||||
operationId: "getUser"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return errors when an x- extension key contains special characters", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
"x-foo«bar»": {
|
||||
"key with spaces": 42
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should not return errors when OAS2 component names contain special characters", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
definitions: {
|
||||
"Foo«Bar»": {
|
||||
type: "object"
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
"Foo«Bar»": {
|
||||
in: "query",
|
||||
name: "foo",
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
"Foo«Bar»": {
|
||||
description: "ok"
|
||||
}
|
||||
},
|
||||
securityDefinitions: {
|
||||
"Foo«Bar»": {
|
||||
type: "basic"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,91 @@
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrors } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - oas3 operations", () => {
|
||||
describe("GET and DELETE operations may not have a requestBody", () => {
|
||||
it("should return an error when a requestBody exists in a GET operation", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`GET operations cannot have a requestBody.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "requestBody"])
|
||||
})
|
||||
})
|
||||
it("should return an error when a requestBody exists in a DELETE operation", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
delete: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`DELETE operations cannot have a requestBody.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "delete", "requestBody"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when other methods contain a requestBody", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,165 @@
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - oas3 parameters", () => {
|
||||
describe("Header parameters should not be named Authorization, Content-Type, or Accept", () => {
|
||||
it("should return a warning when a header parameter named Authorization is defined in an operation", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "header",
|
||||
name: "authorization"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toMatch(`Header parameters named "Authorization" are ignored.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "parameters", "0", "name"])
|
||||
})
|
||||
})
|
||||
it("should return a warning when a header parameter named Content-Type is defined in an operation", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "header",
|
||||
name: "content-type"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toMatch(`Header parameters named "Content-Type" are ignored.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "parameters", "0", "name"])
|
||||
})
|
||||
})
|
||||
it("should return a warning when a header parameter named Accept is defined in an operation", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "header",
|
||||
name: "accept"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toMatch(`Header parameters named "Accept" are ignored.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "parameters", "0", "name"])
|
||||
})
|
||||
})
|
||||
it("should return a warning when a header parameter named Authorization is defined in components", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
parameters: {
|
||||
auth: {
|
||||
in: "header",
|
||||
name: "authorization"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toMatch(`Header parameters named "Authorization" are ignored.`)
|
||||
expect(firstError.path).toEqual(["components", "parameters", "auth", "name"])
|
||||
})
|
||||
})
|
||||
it("should return no warnings when a non-header parameter is named Authorization", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "query",
|
||||
name: "authorization"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no warnings when a header parameter name contains 'Authorization' as a substring", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "header",
|
||||
name: "X-Authorization"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no warnings when a header parameter is named 'Authorization' in OpenAPI 2.0", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
in: "header",
|
||||
name: "authorization"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,706 @@
|
||||
import expect from "expect"
|
||||
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "../validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - oas3 refs", () => {
|
||||
describe("$refs for request bodies must reference a request body by position", () => {
|
||||
it("should return an error when a requestBody incorrectly references a local component schema", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "#/components/schemas/MySchema"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
MySchema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`requestBody $refs cannot point to '#/components/schemas/…', they must point to '#/components/requestBodies/…'`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "post", "requestBody", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when a requestBody references a remote component schema", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "http://google.com/#/components/schemas/MySchema"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return an error when a requestBody in a callback incorrectly references a local component schema", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
info: null,
|
||||
version: "1.0.0-oas3",
|
||||
title: "example",
|
||||
paths: {
|
||||
"/api/callbacks": {
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
},
|
||||
callbacks: {
|
||||
callback: {
|
||||
"/callback": {
|
||||
post: {
|
||||
requestBody: {
|
||||
$ref: "#/components/schemas/callbackRequest"
|
||||
},
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
callbackRequest: {
|
||||
type: "object",
|
||||
properties: {
|
||||
property1: {
|
||||
type: "integer",
|
||||
example: 10000
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`requestBody $refs cannot point to '#/components/schemas/…', they must point to '#/components/requestBodies/…'`)
|
||||
expect(firstError.path).toEqual(["paths", "/api/callbacks", "post", "callbacks",
|
||||
"callback", "/callback", "post", "requestBody", "$ref"])
|
||||
})
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references a local component request body", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "#/components/requestBodies/MyBody"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
MyBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references a local operation request body", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "#/paths/~1/put/requestBody"
|
||||
}
|
||||
},
|
||||
put: {
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references a remote component request body", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "http://google.com/#/components/requestBodies/MyBody"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
MyBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references a remote https component request body", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "https://example.com/file.yaml#/components/requestBodies/group1/addPetBody"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
MyBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references an external yaml file", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "addPetBody.yaml"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
MyBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
it("should return no errors when a requestBody correctly references an external yaml pointing some node", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/": {
|
||||
post: {
|
||||
operationId: "myId",
|
||||
requestBody: {
|
||||
$ref: "./components.yaml#/path/to/some/node"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
MyBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
|
||||
describe("$refs for requestbody schemas must reference a schema by position", () => {
|
||||
it("should return an error when a requestBody schema incorrectly references a local component requestBody", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
post: {
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/requestBodies/Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
requestBodies: {
|
||||
Foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const allSemanticErrors = allErrors.filter(err => err.source === "semantic")
|
||||
|
||||
expect(allSemanticErrors.length).toEqual(1)
|
||||
|
||||
const firstError = allSemanticErrors[0]
|
||||
|
||||
expect(firstError.message).toEqual(`requestBody schema $refs must point to a position where a Schema Object can be legally placed`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "post", "requestBody", "content", "application/json", "schema", "$ref"])
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a requestBody schema references a local component schema", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
post: {
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
Foo: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a requestBody schema references remote document paths", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
post: {
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "http://google.com#/Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a requestBody schema references entire remote documents", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
post: {
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "addPetBody.yaml"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a requestBody schema references local operation requestBody schemas", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
post: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
},
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
put: {
|
||||
requestBody: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
},
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/paths/~1foo/post/requestBody/content/application~1json/schema"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("response header $refs should not point to parameters", () => {
|
||||
it("should return an error when a response header incorrectly references a local parameter component", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK",
|
||||
headers: {
|
||||
"X-MyHeader": {
|
||||
$ref: "#/components/parameters/MyHeader"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
headers: {
|
||||
MyHeader: {
|
||||
$ref: "#/components/parameters/MyHeader"
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
MyHeader: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`OAS3 header $refs should point to #/components/headers/... and not #/components/parameters/...`)
|
||||
expect(firstError.path).toEqual(["paths", "/foo", "get","responses","200", "headers", "X-MyHeader", "$ref"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no errors when a response header correctly references a local header component", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK",
|
||||
headers: {
|
||||
"X-MyHeader": {
|
||||
$ref: "#/components/headers/MyHeader"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
headers: {
|
||||
MyHeader: {
|
||||
$ref: "#/components/headers/MyHeader"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors for external $refs in response headers", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK",
|
||||
headers: {
|
||||
"X-MyHeader": {
|
||||
$ref: "https://www.google.com/#/components/parameter/MyHeader"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
headers: {
|
||||
MyHeader: {
|
||||
$ref: "#/components/headers/MyHeader"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
|
||||
describe("parameter $refs should not point to header components", () => {
|
||||
it("should return an error when a parameter incorrectly references a response header component", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "#/components/headers/foo"
|
||||
}
|
||||
],
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "#/components/headers/foo"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
parameters: {
|
||||
myParam: {
|
||||
$ref: "#/components/headers/foo"
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
foo: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(3)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`OAS3 parameter $refs should point to #/components/parameters/... and not #/components/headers/...`)
|
||||
expect(firstError.path).toEqual(["paths","/foo","parameters", "0", "$ref"])
|
||||
const secondError = allErrors[1]
|
||||
expect(secondError.message).toEqual(`OAS3 parameter $refs should point to #/components/parameters/... and not #/components/headers/...`)
|
||||
expect(secondError.path).toEqual(["paths","/foo","get","parameters", "0", "$ref"])
|
||||
const thirdError = allErrors[2]
|
||||
expect(thirdError.message).toEqual(`OAS3 parameter $refs should point to #/components/parameters/... and not #/components/headers/...`)
|
||||
expect(thirdError.path).toEqual(["components","parameters", "myParam", "$ref"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no errors when a parameter correctly references a parameter component", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "#/components/parameters/foo"
|
||||
}
|
||||
],
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "#/components/parameters/foo"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
parameters: {
|
||||
foo: {
|
||||
$ref: "#/components/parameters/foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return no errors for external parameter $refs", () => {
|
||||
const spec = {
|
||||
openapi: "3.0.0",
|
||||
paths: {
|
||||
"/foo": {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "http://www.google.com/#/components/parameters/foo"
|
||||
}
|
||||
],
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
$ref: "http://www.google.com/#/components/parameters/foo"
|
||||
}
|
||||
],
|
||||
responses: {
|
||||
"200": {
|
||||
description: "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
parameters: {
|
||||
foo: {
|
||||
$ref: "http://www.google.com/#/components/parameters/foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,142 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrors } from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - parameters", function() {
|
||||
this.timeout(10 * 1000 ) // For swagger-ui startup
|
||||
|
||||
it("should return an error when an array type parameter omits an `items` property", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"parameters": [
|
||||
{
|
||||
"name": "tags",
|
||||
"in": "query",
|
||||
"description": "tags to filter by",
|
||||
"type": "array"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0"])
|
||||
expect(firstError.message).toMatch(/.*type.*array.*require.*items/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Operations cannot have both a 'body' parameter and a 'formData' parameter", () => {
|
||||
it("should complain about having both in the same operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "formData" },
|
||||
{ in: "body" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Parameters cannot have both a "in: body" and "in: formData", as "formData" _will_ be the body`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "parameters"])
|
||||
})
|
||||
})
|
||||
it("should not complain about having only a body parameter in the same operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "body" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
it("should not complain about having only a formData parameter in the same operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "formData" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Operations must have only one body parameter", () => {
|
||||
it("should complain about having two body parameters in the same operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "body" },
|
||||
{ in: "body" },
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual(`Multiple body parameters are not allowed.`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "parameters"])
|
||||
})
|
||||
})
|
||||
it("should not complain about having one body parameter in the same operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
consumes: ["multipart/form-data"],
|
||||
parameters: [
|
||||
{ in: "body" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,275 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrors } from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - paths", function(){
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
describe("Empty path templates are not allowed", () => {
|
||||
|
||||
it("should return one problem for an empty path template", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{}": {}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.message).toEqual( "Empty path parameter declarations are not valid")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{}"])
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("Path parameters declared in the path string need matching definitions", () => {
|
||||
|
||||
it("should return one problem for an undefined declared path parameter", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual( "Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return one problem for an path parameter defined in another path", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {},
|
||||
"/UncoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Declared path parameter \"id\" needs to be defined as a path parameter at either the path or operation level")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{id}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no problems for a path parameter defined in the path", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should return no problems for a path parameter defined in an operation", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("Path strings must be equivalently different", () => {
|
||||
|
||||
it("should return one problem for an equivalent templated path strings", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
},
|
||||
"/CoolPath/{count}": {
|
||||
parameters: [{
|
||||
name: "count",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Equivalent paths are not allowed.")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath/{count}"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should return no problems for a templated and untemplated pair of path strings", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/": {},
|
||||
"/CoolPath/{count}": {
|
||||
parameters: [{
|
||||
name: "count",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should return no problems for a templated and double-templated set of path strings", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{group_id1}/all": {
|
||||
parameters: [{
|
||||
name: "group_id1",
|
||||
in: "path",
|
||||
required: true
|
||||
}]
|
||||
},
|
||||
"/CoolPath/{group_id2}/{user_id2}": {
|
||||
parameters: [
|
||||
{
|
||||
name: "group_id2",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "user_id2",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("Paths must have unique name + in parameters", () => {
|
||||
it("should return no problems for a name collision only", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "id",
|
||||
in: "query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
it("should return no problems when 'in' is not defined", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath/{id}": {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: "id",
|
||||
// in: "path"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("Integrations", () => {
|
||||
it.skip("should return two problems for an equivalent path string missing a parameter definition", function(){
|
||||
// const spec = {
|
||||
// paths: {
|
||||
// "/CoolPath/{id}": {
|
||||
// parameters: [{
|
||||
// name: "id",
|
||||
// in: "path"
|
||||
// }]
|
||||
// },
|
||||
// "/CoolPath/{count}": {}
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// let res = validate({ resolvedSpec: spec })
|
||||
// expect(res.errors).toEqual([
|
||||
// {
|
||||
// message: "Equivalent paths are not allowed.",
|
||||
// path: "paths./CoolPath/{count}"
|
||||
// },
|
||||
// {
|
||||
// message: "Declared path parameter \"count\" needs to be defined as a path parameter at either the path or operation level",
|
||||
// path: "paths./CoolPath/{count}"
|
||||
// }
|
||||
// ])
|
||||
// expect(res.warnings).toEqual([])
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,227 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - refs", function() {
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
describe.skip("Refs are restricted in specific areas of a spec", () => {
|
||||
describe("Response $refs", () => {
|
||||
it("should return a problem for a parameters $ref in a response position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
responses: {
|
||||
"200": {
|
||||
$ref: "#/parameters/abc"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "responses", "200", "$ref"])
|
||||
// expect(firstError.message).toMatch("")
|
||||
})
|
||||
})
|
||||
|
||||
// FIXME: This poses a problem for our newer validation PR, as it only iterates over the resolved spec.
|
||||
// We can look for $$refs, but that may be too fragile.
|
||||
// PS: We have a flag in mapSpec, that adds $$refs known as metaPatches
|
||||
it("should return a problem for a definitions $ref in a response position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
schema: {
|
||||
$ref: "#/responses/abc"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
const firstError = allErrors[0]
|
||||
expect(allErrors.length).toEqual(1)
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "responses", "200", "$ref"])
|
||||
expect(firstError.message).toEqual(`Response references are not allowed within schemas`)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return a problem for a responses $ref in a response position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
responses: {
|
||||
"200": {
|
||||
$ref: "#/responses/abc"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
describe("Schema $refs", () => {
|
||||
// See note on resolved vs raw spec
|
||||
it("should return a problem for a parameters $ref in a schema position", function(){
|
||||
// const spec = {
|
||||
// paths: {
|
||||
// "/CoolPath": {
|
||||
// schema: {
|
||||
// $ref: "#/parameters/abc"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// let res = validate({ jsSpec: spec })
|
||||
// expect(res.errors.length).toEqual(1)
|
||||
// expect(res.errors[0].path).toEqual(["paths", "/CoolPath", "schema", "$ref"])
|
||||
// expect(res.warnings.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("should return a problem for a responses $ref in a schema position", function(){
|
||||
// const spec = {
|
||||
// paths: {
|
||||
// "/CoolPath": {
|
||||
// schema: {
|
||||
// $ref: "#/responses/abc"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// let res = validate({ jsSpec: spec })
|
||||
// expect(res.errors.length).toEqual(1)
|
||||
// expect(res.errors[0].path).toEqual(["paths", "/CoolPath", "schema", "$ref"])
|
||||
// expect(res.warnings.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("should not return a problem for a definition $ref in a schema position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
schema: {
|
||||
$ref: "#/definition/abc"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return a problem for a schema property named 'properties'", function(){
|
||||
// #492 regression
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"definitions": {
|
||||
"ServicePlan": {
|
||||
"description": "New Plan to be added to a service.",
|
||||
"properties": {
|
||||
"plan_id": {
|
||||
"type": "string",
|
||||
"description": "ID of the new plan from the catalog."
|
||||
},
|
||||
"parameters": {
|
||||
"$ref": "#/definitions/Parameter"
|
||||
},
|
||||
"previous_values": {
|
||||
"$ref": "#/definitions/PreviousValues"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
let allErrors = system.errSelectors.allErrors().toJS()
|
||||
allErrors = allErrors.filter(a => a.level != "warning")
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
describe("Parameter $refs", () => {
|
||||
|
||||
it("should return a problem for a definition $ref in a parameter position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
parameters: [{
|
||||
$ref: "#/definitions/abc"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "parameters", "0", "$ref"])
|
||||
expect(firstError.message).toMatch("")
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a problem for a responses $ref in a parameter position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
parameters: [{
|
||||
$ref: "#/responses/abc"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "parameters", "0", "$ref"])
|
||||
expect(firstError.message).toMatch("")
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return a problem for a parameter $ref in a parameter position", function(){
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
parameters: [{
|
||||
$ref: "#/parameters/abc"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,544 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, {
|
||||
expectNoErrors,
|
||||
expectNoErrorsOrWarnings
|
||||
} from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - schema", function() {
|
||||
this.timeout(10 * 1000)
|
||||
|
||||
it("should return an error when a definition's property is readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
blah: {
|
||||
$ref: "#/definitions/CoolModel"
|
||||
},
|
||||
definitions: {
|
||||
CoolModel: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
readOnly: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Read only properties cannot be marked as required by a schema.")
|
||||
expect(firstError.path).toEqual(["definitions", "CoolModel", "required", "0"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should not return an error when a definition's property is not readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
definitions: {
|
||||
CoolModel: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors
|
||||
.allErrors()
|
||||
.filter(a => a.get("level") === "error") // We have an incidental "warning"
|
||||
.toJS()
|
||||
expect(allErrors).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should return an error when a response schema's property is readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
responses: {
|
||||
200: {
|
||||
schema: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
readOnly: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors
|
||||
.allErrors()
|
||||
.toJS()
|
||||
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
|
||||
expect(firstError.message).toEqual("Read only properties cannot be marked as required by a schema.")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "get", "responses", "200", "schema", "required", "0"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a response schema's property is not readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
responses: {
|
||||
200: {
|
||||
schema: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should return an error when a parameter schema's property is readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "BadParameter",
|
||||
in: "body",
|
||||
schema: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
readOnly: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors
|
||||
.allErrors()
|
||||
.toJS()
|
||||
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual("Read only properties cannot be marked as required by a schema.")
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "get", "parameters", "0", "schema", "required", "0"])
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when a parameter schema's property is not readOnly and required by the schema", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
parameters: [{
|
||||
name: "BadParameter",
|
||||
in: "body",
|
||||
schema: {
|
||||
required: ["BadProperty"],
|
||||
properties: {
|
||||
BadProperty: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
describe("Type key", () => {
|
||||
it("should return an error when \"type\" is an array", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/CoolPath": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
schema: {
|
||||
type: ["number", "string"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors
|
||||
.allErrors()
|
||||
.toJS()
|
||||
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.message).toEqual(`Schema "type" key must be a string`)
|
||||
expect(firstError.path).toEqual(["paths", "/CoolPath", "get", "responses", "200", "schema", "type"])
|
||||
})
|
||||
})
|
||||
it("should not return an error when \"type\" is a property name", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"definitions": {
|
||||
"ApiResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
let allErrors = system.errSelectors.allErrors().toJS()
|
||||
allErrors = allErrors.filter(a => a.level != "warning") // ignore warnings
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should not return an error when \"type\" is a property name inside additionalProperties", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"definitions": {
|
||||
"ApiResponse": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
let allErrors = system.errSelectors.allErrors().toJS()
|
||||
allErrors = allErrors.filter(a => a.level != "warning") // ignore warnings
|
||||
expect(allErrors.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
it("should not return an error when \"type\" is a model name", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"definitions": {
|
||||
"type": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrors(spec)
|
||||
})
|
||||
})
|
||||
|
||||
describe(`"type: array" requires "items"`, () => {
|
||||
describe("header objects", function() {
|
||||
// It takes a while to start up swagger-ui, for some reason
|
||||
|
||||
it("should return an error when an array header object omits an `items` property", () => {
|
||||
|
||||
// Given
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"version": "1.0.0",
|
||||
"title": "Swagger Petstore"
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"description": "Returns all pets from the system that the user has access to",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "pet response",
|
||||
"headers": {
|
||||
"X-MyHeader": {
|
||||
"type": "array"
|
||||
}
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "unexpected error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
|
||||
// Then
|
||||
expect(system.errSelectors.allErrors().count()).toEqual(1)
|
||||
const firstError = system.errSelectors.allErrors().first().toJS()
|
||||
expect(firstError.message).toMatch(/.*type.*array.*require.*items/)
|
||||
expect(firstError.path).toEqual(["paths", "/pets", "get", "responses", "200", "headers", "X-MyHeader"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should not return an error when an array header object has an `items` property", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"version": "1.0.0",
|
||||
"title": "Swagger Petstore"
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"description": "Returns all pets from the system that the user has access to",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "pet response",
|
||||
"headers": {
|
||||
"X-MyHeader": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "unexpected error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
describe("definitions", function() {
|
||||
// It takes a while to start up swagger-ui, for some reason
|
||||
|
||||
it("should return an error when an array definition omits an `items` property", () => {
|
||||
|
||||
// Given
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
$ref: "#/definitions/asdf"
|
||||
},
|
||||
"definitions": {
|
||||
"asdf": {
|
||||
type: "array"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
|
||||
// Then
|
||||
expect(system.errSelectors.allErrors().count()).toEqual(1)
|
||||
const firstError = system.errSelectors.allErrors().first().toJS()
|
||||
expect(firstError.message).toMatch(/.*type.*array.*require.*items/)
|
||||
expect(firstError.path).toEqual(["definitions", "asdf"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should not return an error when an array definition has an `items` property", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"version": "1.0.0",
|
||||
"title": "Swagger Petstore"
|
||||
},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"description": "Returns all pets from the system that the user has access to",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "pet response",
|
||||
"headers": {
|
||||
"X-MyHeader": {
|
||||
$ref: "#/definitions/array"
|
||||
}
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "unexpected error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"array": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
describe("'pattern' Z anchors", function() {
|
||||
it("should return an error when a schema has a Z anchor in its pattern", () => {
|
||||
|
||||
// Given
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
$ref: "#/definitions/asdf"
|
||||
},
|
||||
"definitions": {
|
||||
"asdf": {
|
||||
type: "string",
|
||||
pattern: "^[-a-zA-Z0-9_]+\\Z"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
|
||||
// Then
|
||||
expect(system.errSelectors.allErrors().count()).toEqual(1)
|
||||
const firstError = system.errSelectors.allErrors().first().toJS()
|
||||
expect(firstError.message).toEqual(`"\\Z" anchors are not allowed in regular expression patterns`)
|
||||
expect(firstError.path).toEqual(["definitions", "asdf", "pattern"])
|
||||
})
|
||||
|
||||
})
|
||||
it("should return an error when a subschema has a Z anchor in its pattern", () => {
|
||||
|
||||
// Given
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
responses: {
|
||||
"200": {
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
slug: {
|
||||
type: "string",
|
||||
pattern: "^[-a-zA-Z0-9_]+\\Z"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// When
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
// Then
|
||||
expect(system.errSelectors.allErrors().count()).toEqual(1)
|
||||
const firstError = system.errSelectors.allErrors().first().toJS()
|
||||
expect(firstError.message).toEqual(`"\\Z" anchors are not allowed in regular expression patterns`)
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "responses", "200", "schema", "properties", "slug", "pattern"])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it("should not return an error when a regex pattern doesn't use a Z anchor", () => {
|
||||
const spec = {
|
||||
swagger: "2.0",
|
||||
paths: {
|
||||
$ref: "#/definitions/asdf"
|
||||
},
|
||||
"definitions": {
|
||||
"asdf": {
|
||||
type: "string",
|
||||
pattern: "^[-a-zA-Z0-9_]+"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,146 @@
|
||||
import expect from "expect"
|
||||
import validateHelper, { expectNoErrorsOrWarnings } from "./validate-helper.js"
|
||||
|
||||
describe("validation plugin - semantic - security scopes", function() {
|
||||
this.timeout(10 * 1000) // For the slow validateHelper startup ( via swagger-ui )
|
||||
|
||||
it("should return an error when an operation references a non-existing security scope", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"securityDefinitions": {
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "apikey",
|
||||
"in": "query",
|
||||
"scopes": {
|
||||
"asdf": "blah blah"
|
||||
}
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "asdf",
|
||||
"security": [
|
||||
{
|
||||
"api_key": [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "security", "0", "0"])
|
||||
expect(firstError.message).toEqual("Security scope definition write:pets could not be resolved")
|
||||
})
|
||||
})
|
||||
|
||||
it("should return an error when an operation references a security definition with no scopes", () => {
|
||||
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"securityDefinitions": {
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "apikey",
|
||||
"in": "query"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "asdf",
|
||||
"security": [
|
||||
{
|
||||
"api_key": [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors.length).toEqual(1)
|
||||
const firstError = allErrors[0]
|
||||
expect(firstError.path).toEqual(["paths", "/", "get", "security", "0", "0"])
|
||||
expect(firstError.message).toMatch("Security scope definition write:pets could not be resolved")
|
||||
})
|
||||
})
|
||||
|
||||
it("should not return an error when an operation references an existing security scope", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"securityDefinitions": {
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "apikey",
|
||||
"in": "query",
|
||||
"scopes": {
|
||||
"write:pets": "write to pets"
|
||||
}
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "asdf",
|
||||
"security": [
|
||||
{
|
||||
"api_key": [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
|
||||
it("should not return an error when an top-level security references an existing security scope", () => {
|
||||
const spec = {
|
||||
"swagger": "2.0",
|
||||
"securityDefinitions": {
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "apikey",
|
||||
"in": "query",
|
||||
"scopes": {
|
||||
"write:pets": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"api_key": [
|
||||
"write:pets"
|
||||
]
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "asdf"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expectNoErrorsOrWarnings(spec)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,72 @@
|
||||
import expect from "expect"
|
||||
import SwaggerUi from "swagger-ui"
|
||||
import debounce from "lodash/debounce"
|
||||
|
||||
import ValidateBasePlugin from "plugins/validate-base"
|
||||
import ValidateSemanticPlugin from "plugins/validate-semantic"
|
||||
import ASTPlugin from "plugins/ast"
|
||||
|
||||
const envDelay = process.env.ASYNC_TEST_DELAY
|
||||
|
||||
const DELAY_MS = (typeof envDelay === "string" ? parseInt(envDelay) : envDelay) || 50
|
||||
|
||||
export default function validateHelper(spec) {
|
||||
return new Promise((resolve) => {
|
||||
const system = SwaggerUi({
|
||||
spec,
|
||||
domNode: null,
|
||||
presets: [
|
||||
SwaggerUi.plugins.SpecIndex,
|
||||
SwaggerUi.plugins.ErrIndex,
|
||||
SwaggerUi.plugins.DownloadUrl,
|
||||
SwaggerUi.plugins.SwaggerJsIndex,
|
||||
SwaggerUi.plugins.Oas3Index
|
||||
],
|
||||
initialState: {
|
||||
layout: undefined
|
||||
},
|
||||
plugins: [
|
||||
ASTPlugin,
|
||||
ValidateBasePlugin,
|
||||
ValidateSemanticPlugin,
|
||||
() => ({
|
||||
statePlugins: {
|
||||
configs: {
|
||||
actions: {
|
||||
loaded: () => {
|
||||
return {
|
||||
type: "noop"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
system.validateActions.all()
|
||||
|
||||
const registerActivity = debounce(() => resolve(system), DELAY_MS)
|
||||
|
||||
system.getStore().subscribe(registerActivity)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export function expectNoErrorsOrWarnings(spec) {
|
||||
return validateHelper(spec)
|
||||
.then( system => {
|
||||
const allErrors = system.errSelectors.allErrors().toJS()
|
||||
expect(allErrors).toEqual([])
|
||||
})
|
||||
}
|
||||
|
||||
export function expectNoErrors(spec) {
|
||||
return validateHelper(spec)
|
||||
.then(system => {
|
||||
let allErrors = system.errSelectors.allErrors().toJS()
|
||||
allErrors = allErrors.filter(a => a.level === "error") // ignore warnings
|
||||
expect(allErrors).toEqual([])
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user