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,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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]
@@ -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