swagger-parser 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.gitmodules +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +6 -0
- data/.yardopts +4 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +114 -0
- data/Rakefile +11 -0
- data/lib/swagger.rb +56 -0
- data/lib/swagger/api.rb +26 -0
- data/lib/swagger/attachable.rb +39 -0
- data/lib/swagger/boolean.rb +13 -0
- data/lib/swagger/builder.rb +109 -0
- data/lib/swagger/mime_type.rb +46 -0
- data/lib/swagger/notes.md +35 -0
- data/lib/swagger/parsers.rb +36 -0
- data/lib/swagger/schema.rb +63 -0
- data/lib/swagger/swagger_object.rb +49 -0
- data/lib/swagger/uri.rb +11 -0
- data/lib/swagger/uri_template.rb +11 -0
- data/lib/swagger/v2/api.rb +90 -0
- data/lib/swagger/v2/deterministic_json_schema.rb +130 -0
- data/lib/swagger/v2/example.rb +32 -0
- data/lib/swagger/v2/header.rb +22 -0
- data/lib/swagger/v2/info.rb +33 -0
- data/lib/swagger/v2/operation.rb +66 -0
- data/lib/swagger/v2/parameter.rb +35 -0
- data/lib/swagger/v2/path.rb +47 -0
- data/lib/swagger/v2/response.rb +26 -0
- data/lib/swagger/v2/security_requirement.rb +12 -0
- data/lib/swagger/v2/security_scheme.rb +25 -0
- data/lib/swagger/v2/tag.rb +11 -0
- data/lib/swagger/version.rb +3 -0
- data/resources/schemas/json_schema/draft-04.json +150 -0
- data/resources/schemas/swagger/v2.0/schema.json +1483 -0
- data/spec/fixtures/custom-properties.yaml +61 -0
- data/spec/fixtures/petstore-full.yaml +245 -0
- data/spec/fixtures/swagger.yaml +100 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/swagger/api_spec.rb +87 -0
- data/spec/swagger/builder_spec.rb +52 -0
- data/spec/swagger/custom_properties_spec.rb +22 -0
- data/spec/swagger/info_spec.rb +76 -0
- data/spec/swagger/operation_spec.rb +99 -0
- data/spec/swagger/swagger_spec.rb +74 -0
- data/swagger-parser.gemspec +32 -0
- metadata +250 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
module Swagger
|
2
|
+
module V2
|
3
|
+
# A class to represent example objects in the Swagger schema.
|
4
|
+
# Usually used to represent example request or responses.
|
5
|
+
# Provides access to both the raw example or a parsed representation.
|
6
|
+
class Example
|
7
|
+
extend Forwardable
|
8
|
+
def_delegator :@raw, :to_s, :inspect
|
9
|
+
|
10
|
+
# The example as it appears in the Swagger document.
|
11
|
+
# @return Object the example
|
12
|
+
attr_reader :raw
|
13
|
+
|
14
|
+
def initialize(sample)
|
15
|
+
@raw = sample
|
16
|
+
end
|
17
|
+
|
18
|
+
# The example after it has been parsed to match the +media_type+.
|
19
|
+
# @param media_type [String] the target media_type
|
20
|
+
# @return [Object] an object according to the +media_type+
|
21
|
+
def parse(media_type = 'application/json')
|
22
|
+
return @raw unless @raw.is_a? String
|
23
|
+
parser = Swagger::MimeType.parser_for(media_type)
|
24
|
+
parser.parse(@raw)
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
@raw.inspect
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
module V2
|
5
|
+
# Class representing a Swagger "Header Object".
|
6
|
+
# @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#headerObject Header Object
|
7
|
+
class Header < SwaggerObject
|
8
|
+
# @!group Fixed Fields
|
9
|
+
field :description, String
|
10
|
+
field :required, Swagger::Boolean
|
11
|
+
alias_method :required?, :required
|
12
|
+
field :type, String
|
13
|
+
field :format, String
|
14
|
+
field :items, Hash # TODO: Items Object
|
15
|
+
field :collectionFormat, String
|
16
|
+
field :default, Object
|
17
|
+
# @!endgroup
|
18
|
+
|
19
|
+
include DeterministicJSONSchema
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
module V2
|
5
|
+
# Class representing a Swagger "Info Object".
|
6
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#infoObject Info Object
|
7
|
+
class Info < SwaggerObject
|
8
|
+
# Class representing a Swagger "Contact Object".
|
9
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#contactObject Contact Object
|
10
|
+
class Contact < SwaggerObject
|
11
|
+
# @group Swagger Fields
|
12
|
+
field :name, String
|
13
|
+
field :url, Swagger::URI
|
14
|
+
field :email, String
|
15
|
+
# @endgroup
|
16
|
+
end
|
17
|
+
|
18
|
+
# Class representing a Swagger "License Object".
|
19
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#licenseObject License Object
|
20
|
+
class License < SwaggerObject
|
21
|
+
required_field :name, String
|
22
|
+
field :url, Swagger::URI
|
23
|
+
end
|
24
|
+
|
25
|
+
required_field :title, String
|
26
|
+
field :description, String
|
27
|
+
field :termsOfService, String
|
28
|
+
field :contact, Contact
|
29
|
+
field :license, License
|
30
|
+
required_field :version, String
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
require 'swagger/v2/parameter'
|
3
|
+
require 'swagger/v2/response'
|
4
|
+
require 'swagger/v2/security_requirement'
|
5
|
+
|
6
|
+
module Swagger
|
7
|
+
module V2
|
8
|
+
# Class representing a Swagger "Operation Object".
|
9
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#operationObject Operation Object
|
10
|
+
class Operation < SwaggerObject
|
11
|
+
extend Forwardable
|
12
|
+
def_delegators :parent, :uri_template, :path, :host
|
13
|
+
|
14
|
+
field :summary, String
|
15
|
+
field :description, String
|
16
|
+
field :operationId, String
|
17
|
+
alias_method :operation_id, :operationId
|
18
|
+
field :produces, Array[String] # TODO: Need a lookup that merges w/ API-level field
|
19
|
+
field :consumes, Array[String] # TODO: Need a lookup that merges w/ API-level field
|
20
|
+
field :tags, Array[String] # TODO: This is an array of tag names, need to handle resolution name -> Tag object
|
21
|
+
field :parameters, Array[Parameter] # TODO: Can't decide if default: [] is useful or troublesome
|
22
|
+
field :responses, Hash[String => Response]
|
23
|
+
field :schemes, Array[String] # TODO: Need a lookup that merges w/ API-level field
|
24
|
+
field :security, Array[SecurityRequirement] # TODO: Need a lookup that merges w/ API-level field
|
25
|
+
field :externalDocs, Object # TODO: ExternalDocumentation class
|
26
|
+
field :deprecated, Swagger::Boolean
|
27
|
+
|
28
|
+
def api_title
|
29
|
+
root.info.title
|
30
|
+
end
|
31
|
+
|
32
|
+
def full_name
|
33
|
+
"#{api_title} - #{summary}"
|
34
|
+
end
|
35
|
+
|
36
|
+
# The HTTP verb for the operation.
|
37
|
+
def verb
|
38
|
+
parent.operations.key self
|
39
|
+
end
|
40
|
+
|
41
|
+
def signature
|
42
|
+
"#{verb.to_s.upcase} #{parent.uri_template}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def default_response
|
46
|
+
return nil if responses.nil? || responses.values.nil?
|
47
|
+
|
48
|
+
# FIXME: Swagger isn't very clear on "normal response codes"
|
49
|
+
# In the examples, default is actually an error
|
50
|
+
responses['200'] || responses['201'] || responses['default'] || responses.values.first
|
51
|
+
end
|
52
|
+
|
53
|
+
# Iterates over each parameter defined directly on the operation, excluding parameters
|
54
|
+
# defined at the API level.
|
55
|
+
def each_parameter
|
56
|
+
return if parameters.nil?
|
57
|
+
parameters.each do |parameter|
|
58
|
+
yield parameter
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Iterates over all parameters defined on this operation or at the API level
|
63
|
+
# TODO: Implement all_parameters
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
module V2
|
5
|
+
# Class representing a Swagger "Parameter Object".
|
6
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#parameterObject Parameter Object
|
7
|
+
class Parameter < SwaggerObject
|
8
|
+
# @!group Fixed Fields
|
9
|
+
#required_field :name, String
|
10
|
+
# required_field :in, String
|
11
|
+
field :name, String
|
12
|
+
field :in, String
|
13
|
+
field :description, String
|
14
|
+
field :required, Swagger::Boolean
|
15
|
+
alias_method :required?, :required
|
16
|
+
# @!endgroup
|
17
|
+
|
18
|
+
# @!group Body Fields
|
19
|
+
field :schema, Schema
|
20
|
+
# @!endgroup
|
21
|
+
|
22
|
+
# @!group Non-Body Fields
|
23
|
+
field :type, String
|
24
|
+
field :format, String
|
25
|
+
field :items, Hash # TODO: Items Object
|
26
|
+
field :collectionFormat, String
|
27
|
+
field :default, Object
|
28
|
+
field :allowEmptyValue, Swagger::Boolean
|
29
|
+
alias_method :allowEmptyValue?, :allowEmptyValue
|
30
|
+
# @!endgroup
|
31
|
+
|
32
|
+
include DeterministicJSONSchema
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
require 'swagger/v2/operation'
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
module V2
|
6
|
+
# Class representing a Swagger "Path Item Object".
|
7
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#pathItemObject Path Item Object
|
8
|
+
class Path < SwaggerObject
|
9
|
+
extend Forwardable
|
10
|
+
def_delegator :parent, :host
|
11
|
+
|
12
|
+
VERBS = [:get, :put, :post, :delete, :options, :head, :patch]
|
13
|
+
VERBS.each do |verb|
|
14
|
+
field verb, Operation
|
15
|
+
end
|
16
|
+
field :parameters, Array[Parameter]
|
17
|
+
|
18
|
+
def initialize(hash)
|
19
|
+
hash[:parameters] ||= []
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def operations
|
24
|
+
VERBS.each_with_object({}) do |v, h|
|
25
|
+
operation = send v
|
26
|
+
h[v] = operation if operation
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def uri_template
|
31
|
+
"#{parent.host}#{parent.base_path}#{path}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def path
|
35
|
+
parent.paths.key self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Iterates over each Path level parameter.
|
39
|
+
def each_parameter
|
40
|
+
return if parameters.nil?
|
41
|
+
parameters.each do |parameter|
|
42
|
+
yield parameter
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
require 'swagger/v2/example'
|
3
|
+
require 'swagger/v2/header'
|
4
|
+
|
5
|
+
module Swagger
|
6
|
+
module V2
|
7
|
+
# Class representing a Swagger "Response Object".
|
8
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#responseObject Response Object
|
9
|
+
class Response < SwaggerObject
|
10
|
+
field :description, String
|
11
|
+
field :schema, Swagger::Schema
|
12
|
+
field :headers, Hash[String => Header] # TODO: Headers
|
13
|
+
field :examples, Hash[Swagger::MimeType => Example]
|
14
|
+
|
15
|
+
def status_code
|
16
|
+
# FIXME: swagger-spec needs a defined way to define codes
|
17
|
+
code = parent.responses.key self
|
18
|
+
code = '200' if code == 'default'
|
19
|
+
code.to_i
|
20
|
+
rescue
|
21
|
+
# TODO: Warning?
|
22
|
+
200
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
module V2
|
5
|
+
# Class representing a Swagger "Security Requirement Object".
|
6
|
+
# @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#securityRequirementObject
|
7
|
+
# Security Requirement Object
|
8
|
+
class SecurityRequirement < Hashie::Mash
|
9
|
+
# TODO: Need proper constraints, but coercion is tricky
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'swagger/swagger_object'
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
module V2
|
5
|
+
# Class representing a Swagger "Security Scheme Object".
|
6
|
+
# @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#securitySchemeObject
|
7
|
+
# Security Scheme Object
|
8
|
+
class SecurityScheme < SwaggerObject
|
9
|
+
# FIXME: Swagger documentation about what's required doesn't seem accurate - OSAuth2 centric?
|
10
|
+
|
11
|
+
# According to docs, all except description are required. Schema and samples don't match.
|
12
|
+
|
13
|
+
# @!group Fixed Fields
|
14
|
+
field :type, String
|
15
|
+
field :description, String
|
16
|
+
field :name, String
|
17
|
+
field :in, String
|
18
|
+
field :flow, String
|
19
|
+
field :authorizationUrl, String
|
20
|
+
field :tokenUrl, String
|
21
|
+
field :scopes, Hash
|
22
|
+
# @!endgroup
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Swagger
|
2
|
+
module V2
|
3
|
+
# Class representing a Swagger "Tag Object".
|
4
|
+
# @see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#tagObject Tag Object
|
5
|
+
class Tag < SwaggerObject
|
6
|
+
required_field :name, String
|
7
|
+
field :description, String
|
8
|
+
# TODO: Documentable
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -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
|
+
}
|