Added Swagger

This commit is contained in:
2020-06-10 08:25:21 +02:00
parent 5c6f37eaf7
commit af76cbca87
257 changed files with 48861 additions and 12 deletions
@@ -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)
})
})
})