Skip to content

Commit 5d1c208

Browse files
committed
Merge pull request #35 from civisanalytics/validate
Print a warning if an OAI specification is invalid
2 parents 7c5665a + 28fcb15 commit 5d1c208

17 files changed

+1913
-25
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Metrics/AbcSize:
22
Max: 30
33

44
Metrics/ClassLength:
5-
Max: 220
5+
Max: 240
66

77
Metrics/LineLength:
88
Max: 120

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rvm:
66
- 2.2.5
77
- 2.3.1
88
before_install:
9+
- gem update --system
910
- rvm @global do gem install bundler
1011
branches:
1112
only:

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
* Added a [Code of Conduct](CODE_OF_CONDUCT.md)
1111
* Added a matrix build of 2.0, 2.1, 2.2, and 2.3 to Travis
1212
* [#33](https://github.com/civisanalytics/swagger-diff/pull/33)
13-
Added a changelog feature
13+
added a changelog feature
14+
* [#35](https://github.com/civisanalytics/swagger-diff/pull/35)
15+
print a warning if an OAI specification is invalid
1416

1517
### Changed
1618

LICENSE.txt renamed to LICENSE.md

File renamed without changes.

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,18 @@ See [CONTRIBUTING](CONTRIBUTING.md).
159159

160160
## License
161161

162-
Swagger::Diff is released under the [BSD 3-Clause License](LICENSE.txt).
162+
Swagger::Diff is released under the [BSD 3-Clause License](LICENSE.md).
163+
164+
## OpenAPI (fka Swagger) Specification License
165+
166+
Swagger::Diff includes an
167+
[unmodified copy of the OpenAPI Specification](schema/oai/schema.json).
168+
The OpenAPI Specification is licensed under the
169+
[Apache License, Version 2.0](schema/oai/LICENSE.md).
170+
171+
## JSON Schema Specification License
172+
173+
Swagger::Diff includes an
174+
[unmodified copy of the JSON Schema Specification, draft v4](schema/json/schema.json).
175+
The JSON Schema Specification is licensed under the
176+
[Simplified BSD License](schema/json/LICENSE.md).

lib/swagger/diff.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'open-uri'
33
require 'set'
44
require 'yaml'
5+
require 'json-schema'
56
require 'rspec/expectations'
67
require 'swagger/diff/diff'
78
require 'swagger/diff/rspec_matchers'

lib/swagger/diff/specification.rb

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Specification
44
def initialize(spec)
55
@spec = spec
66
@parsed = parse_swagger(spec)
7+
validate_swagger
78
@endpoint_hash = parsed_to_hash(@parsed)
89
end
910

@@ -257,6 +258,32 @@ def response_attributes_inner(endpoint)
257258
end
258259
ret
259260
end
261+
262+
def schema_for(type)
263+
File.join(
264+
File.expand_path(File.join('..', '..', '..', '..'), __FILE__),
265+
'schema', type, 'schema.json'
266+
)
267+
end
268+
269+
def validate_swagger
270+
json_schema = File.open(schema_for('json')) do |json_schema_file|
271+
JSON::Schema.new(
272+
JSON.parse(json_schema_file.read),
273+
Addressable::URI.parse('http://json-schema.org/draft-04/schema#')
274+
)
275+
end
276+
JSON::Validator.add_schema(json_schema)
277+
errors = JSON::Validator.fully_validate(schema_for('oai'), JSON.dump(@parsed))
278+
unless errors.empty?
279+
spec = if @spec.to_s.length > 80
280+
"#{@spec.to_s[0..74]} ..."
281+
else
282+
@spec
283+
end
284+
warn "#{spec} is not a valid Swagger specification:\n\n#{errors.join("\n")}"
285+
end
286+
end
260287
end
261288
end
262289
end

schema/json/LICENSE.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2016 IETF Trust and the persons identified as authors of the code.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
* Neither the name of Internet Society, IETF or IETF Trust, nor the names of
13+
specific contributors, may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

schema/json/schema.json

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"id": "http://json-schema.org/draft-04/schema#",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"description": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"positiveInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"positiveIntegerDefault0": {
16+
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
17+
},
18+
"simpleTypes": {
19+
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
20+
},
21+
"stringArray": {
22+
"type": "array",
23+
"items": { "type": "string" },
24+
"minItems": 1,
25+
"uniqueItems": true
26+
}
27+
},
28+
"type": "object",
29+
"properties": {
30+
"id": {
31+
"type": "string",
32+
"format": "uri"
33+
},
34+
"$schema": {
35+
"type": "string",
36+
"format": "uri"
37+
},
38+
"title": {
39+
"type": "string"
40+
},
41+
"description": {
42+
"type": "string"
43+
},
44+
"default": {},
45+
"multipleOf": {
46+
"type": "number",
47+
"minimum": 0,
48+
"exclusiveMinimum": true
49+
},
50+
"maximum": {
51+
"type": "number"
52+
},
53+
"exclusiveMaximum": {
54+
"type": "boolean",
55+
"default": false
56+
},
57+
"minimum": {
58+
"type": "number"
59+
},
60+
"exclusiveMinimum": {
61+
"type": "boolean",
62+
"default": false
63+
},
64+
"maxLength": { "$ref": "#/definitions/positiveInteger" },
65+
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
66+
"pattern": {
67+
"type": "string",
68+
"format": "regex"
69+
},
70+
"additionalItems": {
71+
"anyOf": [
72+
{ "type": "boolean" },
73+
{ "$ref": "#" }
74+
],
75+
"default": {}
76+
},
77+
"items": {
78+
"anyOf": [
79+
{ "$ref": "#" },
80+
{ "$ref": "#/definitions/schemaArray" }
81+
],
82+
"default": {}
83+
},
84+
"maxItems": { "$ref": "#/definitions/positiveInteger" },
85+
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
86+
"uniqueItems": {
87+
"type": "boolean",
88+
"default": false
89+
},
90+
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
91+
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
92+
"required": { "$ref": "#/definitions/stringArray" },
93+
"additionalProperties": {
94+
"anyOf": [
95+
{ "type": "boolean" },
96+
{ "$ref": "#" }
97+
],
98+
"default": {}
99+
},
100+
"definitions": {
101+
"type": "object",
102+
"additionalProperties": { "$ref": "#" },
103+
"default": {}
104+
},
105+
"properties": {
106+
"type": "object",
107+
"additionalProperties": { "$ref": "#" },
108+
"default": {}
109+
},
110+
"patternProperties": {
111+
"type": "object",
112+
"additionalProperties": { "$ref": "#" },
113+
"default": {}
114+
},
115+
"dependencies": {
116+
"type": "object",
117+
"additionalProperties": {
118+
"anyOf": [
119+
{ "$ref": "#" },
120+
{ "$ref": "#/definitions/stringArray" }
121+
]
122+
}
123+
},
124+
"enum": {
125+
"type": "array",
126+
"minItems": 1,
127+
"uniqueItems": true
128+
},
129+
"type": {
130+
"anyOf": [
131+
{ "$ref": "#/definitions/simpleTypes" },
132+
{
133+
"type": "array",
134+
"items": { "$ref": "#/definitions/simpleTypes" },
135+
"minItems": 1,
136+
"uniqueItems": true
137+
}
138+
]
139+
},
140+
"allOf": { "$ref": "#/definitions/schemaArray" },
141+
"anyOf": { "$ref": "#/definitions/schemaArray" },
142+
"oneOf": { "$ref": "#/definitions/schemaArray" },
143+
"not": { "$ref": "#" }
144+
},
145+
"dependencies": {
146+
"exclusiveMaximum": [ "maximum" ],
147+
"exclusiveMinimum": [ "minimum" ]
148+
},
149+
"default": {}
150+
}

schema/oai/LICENSE.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2016 The Linux Foundation
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
6+
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.

0 commit comments

Comments
 (0)