open_api_parser 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/open_api_parser/document.rb +64 -0
- data/lib/open_api_parser/file_cache.rb +15 -0
- data/lib/open_api_parser/pointer.rb +45 -0
- data/lib/open_api_parser/specification/endpoint.rb +161 -0
- data/lib/open_api_parser/specification/root.rb +40 -0
- data/lib/open_api_parser/specification.rb +16 -0
- data/lib/open_api_parser/version.rb +3 -0
- data/lib/open_api_parser.rb +13 -0
- data/open_api_parser.gemspec +28 -0
- data/resources/swagger_meta_schema.json +1607 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7ebb287c98a211acf8ad3a00f41e6d217f4f18e5
|
4
|
+
data.tar.gz: 8ab60127e9539f5a2d62294c4a48e577713c8657
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b3bebdfc00a16569e432169aa403bf01cba7ef86cac9a23a2670be109116612e7041225b9f891efb96b4c5495fc7a042138587a1896c1cc1297194b30fb7503
|
7
|
+
data.tar.gz: ea03826b7a5996f341e7958ed203390979c87ce2349056cd00f0479f9bff147353797226c85dbdae3449685cdc8f4dc65fe8040ef32c0dd533d86919b7e34be3
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Braintree, a division of PayPal, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# OpenApiParser
|
2
|
+
|
3
|
+
A gem for parsing Open API specifications.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
First, resolve a specification given an absolute file path.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
specification = OpenApiParser::Specification.resolve("/path/to/my/specification.yaml")
|
11
|
+
```
|
12
|
+
|
13
|
+
When you `resolve` your file, all references will be expanded and inlined. The fully resolved specification will be validated against the Open API V2 meta schema. If you'd like to prevent meta schema validation, you can provide an option to do so.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
specification = OpenApiParser::Specification.resolve("/path/to/my/specification.yaml", validate_meta_schema: false)
|
17
|
+
```
|
18
|
+
|
19
|
+
If you'd rather instantiate a specification with a _fully resolved_ ruby hash, you can do so by directly instantiating an `OpenApiParser::Specification::Root`.
|
20
|
+
|
21
|
+
```
|
22
|
+
OpenApiParser::Specification::Root.new({...})
|
23
|
+
```
|
24
|
+
|
25
|
+
### Reference resolution
|
26
|
+
|
27
|
+
`OpenApiParser::Specification.resolve` will fully resolve the provided Open API specification by inlining all [JSON References](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html). Two types of references are allowed:
|
28
|
+
|
29
|
+
* [JSON Pointers](https://tools.ietf.org/html/rfc6901)
|
30
|
+
* JSON References to files
|
31
|
+
|
32
|
+
Pointers are resolved from the root of the document in which they appear. File references are resolved relative to the file in which they appear. Here's an example of each:
|
33
|
+
|
34
|
+
```yaml
|
35
|
+
definitions:
|
36
|
+
person:
|
37
|
+
type: object
|
38
|
+
properties:
|
39
|
+
name:
|
40
|
+
type: string
|
41
|
+
age:
|
42
|
+
type: integer
|
43
|
+
|
44
|
+
info:
|
45
|
+
person:
|
46
|
+
$ref: "/definitions/person"
|
47
|
+
other:
|
48
|
+
$ref: "file:another/file.yaml"
|
49
|
+
```
|
50
|
+
|
51
|
+
For more information, see the specs.
|
52
|
+
|
53
|
+
### Endpoints
|
54
|
+
|
55
|
+
With a resolved schema, you can access the information for an endpoint given a path and an HTTP verb.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
endpoint = specification.endpoint("/animals", "post")
|
59
|
+
```
|
60
|
+
|
61
|
+
With an endpoint, you can get access to JSON schema representations of the body, headers, path, query params and response body and headers defined in your Open API specification. You can use these to validate input using any existing JSON Schema library. We recommend [JsonSchema](https://github.com/brandur/json_schema).
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
endpoint.body_schema
|
65
|
+
# => {...}
|
66
|
+
|
67
|
+
endpoint.header_schema
|
68
|
+
# => {...}
|
69
|
+
|
70
|
+
endpoint.path_schema
|
71
|
+
# => {...}
|
72
|
+
|
73
|
+
endpoint.query_schema
|
74
|
+
# => {...}
|
75
|
+
|
76
|
+
endpoint.response_body_schema(201)
|
77
|
+
# => {...}
|
78
|
+
|
79
|
+
endpoint.response_header_schema(201)
|
80
|
+
# => {...}
|
81
|
+
```
|
82
|
+
|
83
|
+
You can also use the endpoint to transform user input into json that can be validated against the schemas generated in the previous examples.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
endpoint.header_json(request_headers_as_hash)
|
87
|
+
# => {...}
|
88
|
+
|
89
|
+
endpoint.path_json("/animals/123?query=param")
|
90
|
+
# => {...}
|
91
|
+
|
92
|
+
endpoint.query_json(request_query_params_as_hash)
|
93
|
+
# => {...}
|
94
|
+
```
|
95
|
+
|
96
|
+
For more information, see the specs.
|
97
|
+
|
98
|
+
## Installation
|
99
|
+
|
100
|
+
Add this line to your application's Gemfile:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
gem 'open_api_parser'
|
104
|
+
```
|
105
|
+
|
106
|
+
And then execute:
|
107
|
+
|
108
|
+
```bash
|
109
|
+
$ bundle
|
110
|
+
```
|
111
|
+
|
112
|
+
Or install it yourself as:
|
113
|
+
|
114
|
+
```bash
|
115
|
+
$ gem install open_api_parser
|
116
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module OpenApiParser
|
2
|
+
class Document
|
3
|
+
def self.resolve(path, file_cache=OpenApiParser::FileCache.new)
|
4
|
+
file_cache.get(path) do
|
5
|
+
content = YAML.load_file(path)
|
6
|
+
Document.new(path, content, file_cache).resolve
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(path, content, file_cache)
|
11
|
+
@path = path
|
12
|
+
@content = content
|
13
|
+
@file_cache = file_cache
|
14
|
+
end
|
15
|
+
|
16
|
+
def resolve
|
17
|
+
deeply_expand_refs(@content)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def deeply_expand_refs(fragment)
|
23
|
+
fragment = expand_refs(fragment)
|
24
|
+
|
25
|
+
if fragment.is_a?(Hash)
|
26
|
+
fragment.reduce({}) do |hash, (k, v)|
|
27
|
+
hash.merge(k => deeply_expand_refs(v))
|
28
|
+
end
|
29
|
+
elsif fragment.is_a?(Array)
|
30
|
+
fragment.map { |e| deeply_expand_refs(e) }
|
31
|
+
else
|
32
|
+
fragment
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def expand_refs(fragment)
|
37
|
+
if fragment.is_a?(Hash) && fragment.has_key?("$ref")
|
38
|
+
ref = fragment["$ref"]
|
39
|
+
|
40
|
+
if ref =~ /\Afile:/
|
41
|
+
expand_file(ref)
|
42
|
+
else
|
43
|
+
expand_pointer(ref)
|
44
|
+
end
|
45
|
+
else
|
46
|
+
fragment
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def expand_file(ref)
|
51
|
+
relative_path = ref.split(":").last
|
52
|
+
absolute_path = File.expand_path(File.join("..", relative_path), @path)
|
53
|
+
|
54
|
+
Document.resolve(absolute_path, @file_cache)
|
55
|
+
end
|
56
|
+
|
57
|
+
def expand_pointer(ref)
|
58
|
+
pointer = OpenApiParser::Pointer.new(ref)
|
59
|
+
fragment = pointer.resolve(@content)
|
60
|
+
|
61
|
+
expand_refs(fragment)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module OpenApiParser
|
2
|
+
class Pointer
|
3
|
+
def initialize(raw_pointer)
|
4
|
+
@raw_pointer = raw_pointer
|
5
|
+
end
|
6
|
+
|
7
|
+
def resolve(document)
|
8
|
+
return document if escaped_pointer == ""
|
9
|
+
|
10
|
+
tokens.reduce(document) do |nested_doc, token|
|
11
|
+
nested_doc.fetch(token)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def escaped_pointer
|
18
|
+
if @raw_pointer.start_with?("#")
|
19
|
+
URI.decode(@raw_pointer[1..-1])
|
20
|
+
else
|
21
|
+
@raw_pointer
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def parse_token(token)
|
26
|
+
if token =~ /\A\d+\z/
|
27
|
+
token.to_i
|
28
|
+
else
|
29
|
+
token.gsub("~0", "~").gsub("~1", "/")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def tokens
|
34
|
+
tokens = escaped_pointer[1..-1].split("/")
|
35
|
+
|
36
|
+
if @raw_pointer.end_with?("/")
|
37
|
+
tokens << ""
|
38
|
+
end
|
39
|
+
|
40
|
+
tokens.map do |token|
|
41
|
+
parse_token(token)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
module OpenApiParser
|
2
|
+
module Specification
|
3
|
+
class Endpoint
|
4
|
+
RESERVED_PARAMETER_KEYS = [
|
5
|
+
"name",
|
6
|
+
"in",
|
7
|
+
"description",
|
8
|
+
"required"
|
9
|
+
]
|
10
|
+
|
11
|
+
attr_reader :raw
|
12
|
+
|
13
|
+
def initialize(path, method, raw)
|
14
|
+
@path = path
|
15
|
+
@method = method
|
16
|
+
@raw = raw
|
17
|
+
end
|
18
|
+
|
19
|
+
def body_schema
|
20
|
+
body_param = parameters.detect { |param| param["in"] == "body" }
|
21
|
+
return restrictive_schema if body_param.nil?
|
22
|
+
|
23
|
+
body_param["schema"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def header_schema
|
27
|
+
@header_schema ||= begin
|
28
|
+
schema = parameter_schema(permissive_schema, "header")
|
29
|
+
|
30
|
+
schema.tap do |schema|
|
31
|
+
schema["properties"] = schema["properties"].reduce({}) do |props, (k, v)|
|
32
|
+
props.merge(headerize(k) => v)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def path_schema
|
39
|
+
@path_schema ||= parameter_schema(restrictive_schema, "path")
|
40
|
+
end
|
41
|
+
|
42
|
+
def query_schema
|
43
|
+
@query_schema ||= parameter_schema(restrictive_schema, "query")
|
44
|
+
end
|
45
|
+
|
46
|
+
def response_body_schema(status)
|
47
|
+
response = response_from_status(status)
|
48
|
+
return restrictive_schema if response.nil?
|
49
|
+
|
50
|
+
response["schema"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def response_header_schema(status)
|
54
|
+
response = response_from_status(status)
|
55
|
+
return permissive_schema if response.nil?
|
56
|
+
|
57
|
+
header_properties = response.fetch("headers", {}).reduce({}) do |props, (k, v)|
|
58
|
+
props.merge(headerize(k) => v)
|
59
|
+
end
|
60
|
+
|
61
|
+
permissive_schema.tap do |schema|
|
62
|
+
schema["properties"] = header_properties
|
63
|
+
schema["required"] = header_properties.keys
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def header_json(headers)
|
68
|
+
schema = header_schema
|
69
|
+
|
70
|
+
headers.reduce({}) do |json, (k, v)|
|
71
|
+
json.merge(json_entry(schema, headerize(k), v))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def path_json(request_path)
|
76
|
+
pattern = Regexp.new(@path.gsub(/\{([^}]+)\}/, '(?<\1>[^/]+)'))
|
77
|
+
match = pattern.match(request_path.gsub(/\..+\z/, ""))
|
78
|
+
schema = path_schema
|
79
|
+
|
80
|
+
match.names.reduce({}) do |json, name|
|
81
|
+
json.merge(json_entry(schema, name, match[name]))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def query_json(query_params)
|
86
|
+
schema = query_schema
|
87
|
+
|
88
|
+
query_params.reduce({}) do |json, (k, v)|
|
89
|
+
json.merge(json_entry(schema, k, v))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def permissive_schema
|
96
|
+
{
|
97
|
+
"additionalProperties" => true,
|
98
|
+
"properties" => {}
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
def restrictive_schema
|
103
|
+
{
|
104
|
+
"additionalProperties" => false,
|
105
|
+
"properties" => {}
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
def headerize(name)
|
110
|
+
name.gsub("-", "_").upcase
|
111
|
+
end
|
112
|
+
|
113
|
+
def json_entry(schema, name, value)
|
114
|
+
properties = schema["properties"]
|
115
|
+
|
116
|
+
if properties.has_key?(name) && properties[name]["type"] == "integer" && value =~ /\A[0-9]+\z/
|
117
|
+
{name => value.to_i}
|
118
|
+
else
|
119
|
+
{name => value}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def parameters
|
124
|
+
@raw.fetch("parameters", [])
|
125
|
+
end
|
126
|
+
|
127
|
+
def parameter_schema(empty_schema, type)
|
128
|
+
type_params = parameters.select { |param| param["in"] == type }
|
129
|
+
return empty_schema if type_params.empty?
|
130
|
+
|
131
|
+
properties = type_params.reduce({}) do |schema, param|
|
132
|
+
schema_value = param.clone.delete_if do |k, v|
|
133
|
+
RESERVED_PARAMETER_KEYS.include?(k)
|
134
|
+
end
|
135
|
+
|
136
|
+
schema.merge(param["name"] => schema_value)
|
137
|
+
end
|
138
|
+
|
139
|
+
type_schema = empty_schema.merge("properties" => properties)
|
140
|
+
|
141
|
+
required_params = type_params.select { |param| param["required"] == true }.map { |param| param["name"] }
|
142
|
+
|
143
|
+
if required_params.any?
|
144
|
+
type_schema["required"] = required_params
|
145
|
+
end
|
146
|
+
|
147
|
+
type_schema
|
148
|
+
end
|
149
|
+
|
150
|
+
def response_from_status(status)
|
151
|
+
entry = @raw.fetch("responses", {}).detect do |k, v|
|
152
|
+
k.to_s == status.to_s
|
153
|
+
end
|
154
|
+
|
155
|
+
return @raw.fetch("responses", {})["default"] if entry.nil?
|
156
|
+
|
157
|
+
entry.last
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module OpenApiParser
|
2
|
+
module Specification
|
3
|
+
class Root
|
4
|
+
attr_reader :raw
|
5
|
+
|
6
|
+
def initialize(raw)
|
7
|
+
@raw = raw
|
8
|
+
end
|
9
|
+
|
10
|
+
def endpoint(path, request_method)
|
11
|
+
uri = URI.parse(path)
|
12
|
+
requested_path = uri.path.gsub(/\..+\z/, "")
|
13
|
+
|
14
|
+
path_details = @raw["paths"].detect do |path_name, path|
|
15
|
+
requested_path =~ to_pattern(path_name)
|
16
|
+
end
|
17
|
+
return nil if path_details.nil?
|
18
|
+
|
19
|
+
path = path_details.last
|
20
|
+
|
21
|
+
method_details = path.detect do |method, schema|
|
22
|
+
method.to_s == request_method.downcase
|
23
|
+
end
|
24
|
+
return nil if method_details.nil?
|
25
|
+
|
26
|
+
Endpoint.new(path_details.first, method_details.first, method_details.last)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_json
|
30
|
+
JSON.generate(@raw)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def to_pattern(path_name)
|
36
|
+
Regexp.new("\\A" + path_name.gsub(/\{[^}]+\}/, "[^/]+") + "\\z")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module OpenApiParser
|
2
|
+
module Specification
|
3
|
+
META_SCHEMA_PATH = File.expand_path("../../../resources/swagger_meta_schema.json", __FILE__)
|
4
|
+
|
5
|
+
def self.resolve(path, validate_meta_schema: true)
|
6
|
+
raw_specification = Document.resolve(path)
|
7
|
+
|
8
|
+
if validate_meta_schema
|
9
|
+
meta_schema = JSON.parse(File.read(META_SCHEMA_PATH))
|
10
|
+
JsonSchema.parse!(meta_schema).validate!(raw_specification)
|
11
|
+
end
|
12
|
+
|
13
|
+
OpenApiParser::Specification::Root.new(raw_specification)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "json"
|
2
|
+
require "uri"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
require "json_schema"
|
6
|
+
|
7
|
+
require "open_api_parser/document"
|
8
|
+
require "open_api_parser/file_cache"
|
9
|
+
require "open_api_parser/pointer"
|
10
|
+
require "open_api_parser/specification"
|
11
|
+
require "open_api_parser/specification/endpoint"
|
12
|
+
require "open_api_parser/specification/root"
|
13
|
+
require "open_api_parser/version"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'open_api_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "open_api_parser"
|
8
|
+
spec.version = OpenApiParser::VERSION
|
9
|
+
spec.authors = ["Braintree"]
|
10
|
+
spec.email = ["code@getbraintree.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A parser for Open API specifications}
|
13
|
+
spec.description = %q{A parser for Open API specifications}
|
14
|
+
spec.homepage = "https://github.com/braintree/open_api_parser"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "json_schema", "~> 0.13.3"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|