swagger_yard 0.3.5 → 0.3.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15d5836beac3af1865cfb811b531d4b3998cc5d4
4
- data.tar.gz: b2017de06dd82f00ffeacb4dbf16fe2399e22b09
3
+ metadata.gz: 9aeaddf2ad2b175e4bfdc57babde3aa86ee99062
4
+ data.tar.gz: ff4b60b5e565142fb1fbca9a965df9ac966c1de1
5
5
  SHA512:
6
- metadata.gz: 1ca5f85cf9b7543be72fee44ef44852d71947fd7ed20a34363666b540d3e441415ac2ce11b2aacee2a8e1460d298887152ff7972f87dd92065eebc302bbb3bdb
7
- data.tar.gz: 2d87279b97f90bd3b69b7af502601c2013b62add6487d7d0ba2d7d661bab3341b64fc0ccf40e51afda8aedd54e9464a0d006e8f36fda07e52b440bb5cbdf447c
6
+ metadata.gz: 803a430a06eb91d2b853a9e1eccd45920d7047db36181076e53aec1e64ebc43a2e288cbab11f4541bb43e8d37f11b7f9822d0557065cd0f1ac92389ada974643
7
+ data.tar.gz: 19ce8dba724574bc55588c3ee5b913b3416ee18e24b26818e8ed5862637df439d8b83ddb3e54fbd1477594cc29d13d43cbe9d6352b327becf9e4d52aa19b6b49
data/README.md CHANGED
@@ -96,7 +96,7 @@ class Accounts::OwnershipsController < ActionController::Base
96
96
 
97
97
  ##
98
98
  # Returns an ownership for an account by id
99
- #
99
+ #
100
100
  # @path [GET] /accounts/ownerships/{id}
101
101
  # @response_type [Ownership]
102
102
  # @error_message [EmptyOwnership] 404 Ownership not found
@@ -129,6 +129,32 @@ To then use your `Model` in your `Controller` documentation, add `@parameter`s:
129
129
  # @parameter pet(body) [Pet] The pet object
130
130
  ```
131
131
 
132
+ To support Swagger Polymorphism, use `@discriminator` and `@inherits`:
133
+
134
+ ```ruby
135
+ #
136
+ # @model Pet
137
+ #
138
+ # @property id(required) [integer] the identifier for the pet
139
+ # @property name [Array<string>] the names for the pet
140
+ # @property age [integer] the age of the pet
141
+ # @property relatives(required) [Array<Pet>] other Pets in its family
142
+ # @discriminator petType(required) [string] the type of pet
143
+ #
144
+ class Pet
145
+ end
146
+
147
+ #
148
+ # @model Dog
149
+ #
150
+ # @inherits Pet
151
+ #
152
+ # @property packSize(required) [integer] the size of the pack the dog is from
153
+ #
154
+ class Dog < Pet
155
+ end
156
+ ```
157
+
132
158
  ## Authorization ##
133
159
 
134
160
  Currently, SwaggerYard only supports API Key auth descriptions. Start by adding `@authorization` to your `ApplicationController`.
@@ -178,7 +204,7 @@ ResourceListing
178
204
  | -> Operation(s) (controller action with HTTP method)
179
205
  | |
180
206
  | -> Parameter(s) (action param)
181
- |
207
+ |
182
208
  -> Model (model)
183
209
  |
184
210
  -> Properties (model attributes)
@@ -65,7 +65,9 @@ module SwaggerYard
65
65
  ::YARD::Tags::Library.define_tag("Error response message", :error_message, :with_types_and_name)
66
66
  ::YARD::Tags::Library.define_tag("Api Summary", :summary)
67
67
  ::YARD::Tags::Library.define_tag("Model resource", :model)
68
+ ::YARD::Tags::Library.define_tag("Model superclass", :inherits)
68
69
  ::YARD::Tags::Library.define_tag("Model property", :property, :with_types_name_and_default)
70
+ ::YARD::Tags::Library.define_tag("Model discriminator", :discriminator, :with_types_name_and_default)
69
71
  ::YARD::Tags::Library.define_tag("Authorization", :authorization, :with_types_and_name)
70
72
  ::YARD::Tags::Library.define_tag("Authorization Use", :authorize_with)
71
73
  end
@@ -4,7 +4,7 @@ module SwaggerYard
4
4
  # complex model object as defined by swagger schema
5
5
  #
6
6
  class Model
7
- attr_reader :id
7
+ attr_reader :id, :discriminator, :inherits
8
8
 
9
9
  def self.from_yard_object(yard_object)
10
10
  new.tap do |model|
@@ -18,6 +18,7 @@ module SwaggerYard
18
18
 
19
19
  def initialize
20
20
  @properties = []
21
+ @inherits = []
21
22
  end
22
23
 
23
24
  def valid?
@@ -31,17 +32,42 @@ module SwaggerYard
31
32
  @id = Model.mangle(tag.text)
32
33
  when "property"
33
34
  @properties << Property.from_tag(tag)
35
+ when "discriminator"
36
+ prop = Property.from_tag(tag)
37
+ @properties << prop
38
+ @discriminator ||= prop.name
39
+ when "inherits"
40
+ @inherits << Model.mangle(tag.text)
34
41
  end
35
42
  end
36
43
 
37
44
  self
38
45
  end
39
46
 
40
- def to_h
41
- {}.tap do |h|
42
- h["properties"] = Hash[@properties.map {|p| [p.name, p.to_h]}]
43
- h["required"] = @properties.select(&:required?).map(&:name) if @properties.detect(&:required?)
47
+ def inherits_references
48
+ @inherits.map do |name|
49
+ {
50
+ "$ref" => "#/definitions/#{name}"
51
+ }
44
52
  end
45
53
  end
54
+
55
+ def to_h
56
+ h = {
57
+ "type" => "object",
58
+ "properties" => Hash[@properties.map {|p| [p.name, p.to_h]}]
59
+ }
60
+
61
+ h["required"] = @properties.select(&:required?).map(&:name) if @properties.detect(&:required?)
62
+ h["discriminator"] = @discriminator if @discriminator
63
+
64
+ # Polymorphism
65
+ h = { "allOf" => inherits_references + [h] } unless @inherits.empty?
66
+
67
+ # Description
68
+ h["description"] = @description if @description
69
+
70
+ h
71
+ end
46
72
  end
47
73
  end
@@ -7,8 +7,8 @@ module SwaggerYard
7
7
  end
8
8
 
9
9
  def initialize(controller_path, model_path)
10
- @model_path = model_path
11
- @controller_path = controller_path
10
+ @model_paths = [*model_path].compact
11
+ @controller_paths = [*controller_path].compact
12
12
 
13
13
  @resource_to_file_path = {}
14
14
  @authorizations = []
@@ -47,25 +47,26 @@ module SwaggerYard
47
47
  end
48
48
 
49
49
  private
50
- def parse_models
51
- return [] unless @model_path
52
50
 
53
- Dir[@model_path].map do |file_path|
54
- SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
55
- Model.from_yard_object(obj)
51
+ def parse_models
52
+ @model_paths.map do |model_path|
53
+ Dir[model_path.to_s].map do |file_path|
54
+ SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
55
+ Model.from_yard_object(obj)
56
+ end
56
57
  end
57
58
  end.flatten.compact.select(&:valid?)
58
59
  end
59
60
 
60
61
  def parse_controllers
61
- return [] unless @controller_path
62
-
63
- Dir[@controller_path].map do |file_path|
64
- SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
65
- obj.tags.select {|t| t.tag_name == "authorization"}.each do |t|
66
- @authorizations << Authorization.from_yard_object(t)
62
+ @controller_paths.map do |controller_path|
63
+ Dir[controller_path.to_s].map do |file_path|
64
+ SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
65
+ obj.tags.select {|t| t.tag_name == "authorization"}.each do |t|
66
+ @authorizations << Authorization.from_yard_object(t)
67
+ end
68
+ ApiDeclaration.from_yard_object(obj)
67
69
  end
68
- ApiDeclaration.from_yard_object(obj)
69
70
  end
70
71
  end.flatten.select(&:valid?)
71
72
  end
@@ -1,19 +1,33 @@
1
1
  module SwaggerYard
2
2
  class Info
3
3
  def to_h
4
- { "title" => SwaggerYard.config.title,
4
+ {
5
+ "title" => SwaggerYard.config.title,
5
6
  "description" => SwaggerYard.config.description,
6
- "version" => SwaggerYard.config.api_version }
7
+ "version" => SwaggerYard.config.api_version
8
+ }
7
9
  end
8
10
  end
9
11
 
10
12
  class Swagger
11
13
  def to_h
12
- { "swagger" => "2.0",
13
- "info" => Info.new.to_h,
14
- "host" => URI(SwaggerYard.config.api_base_path).host,
15
- "basePath" => URI(SwaggerYard.config.api_base_path).request_uri
16
- }.merge(ResourceListing.all.to_h)
14
+ {
15
+ "swagger" => "2.0",
16
+ "info" => Info.new.to_h
17
+ }.merge(uri_info).merge(ResourceListing.all.to_h)
18
+ end
19
+
20
+ private
21
+
22
+ def uri_info
23
+ uri = URI(SwaggerYard.config.api_base_path)
24
+ host = uri.host
25
+ host = "#{uri.host}:#{uri.port}" unless uri.port == uri.default_port
26
+
27
+ {
28
+ 'host' => host,
29
+ 'basePath' => uri.request_uri
30
+ }
17
31
  end
18
32
  end
19
33
  end
@@ -14,6 +14,8 @@ module SwaggerYard
14
14
  when /^regexp?$/i
15
15
  name = 'string'
16
16
  options[:pattern] = parts.last
17
+ when /^object$/i
18
+ options[:object] = parts[1..-1]
17
19
  else
18
20
  name = parts.first
19
21
  options[:format] = parts.last
@@ -22,7 +24,7 @@ module SwaggerYard
22
24
  new(name, options)
23
25
  end
24
26
 
25
- attr_reader :name, :array, :enum
27
+ attr_reader :name, :array, :enum, :object
26
28
 
27
29
  def initialize(name, options = {})
28
30
  @name = Model.mangle(name) if name
@@ -30,6 +32,7 @@ module SwaggerYard
30
32
  @enum = options[:enum]
31
33
  @format = options[:format]
32
34
  @pattern = options[:pattern]
35
+ @object = options[:object]
33
36
  end
34
37
 
35
38
  # TODO: have this look at resource listing?
@@ -43,6 +46,7 @@ module SwaggerYard
43
46
 
44
47
  alias :array? :array
45
48
  alias :enum? :enum
49
+ alias :object? :object
46
50
 
47
51
  def json_type
48
52
  type, format = name, @format
@@ -54,11 +58,11 @@ module SwaggerYard
54
58
  type = "string"
55
59
  format = name
56
60
  end
57
- {}.tap do |h|
58
- h["type"] = type
59
- h["format"] = format if format
60
- h["pattern"] = @pattern if @pattern
61
- end
61
+
62
+ hsh = { "type" => type }
63
+ hsh["format"] = format if format
64
+ hsh["pattern"] = @pattern if @pattern
65
+ hsh
62
66
  end
63
67
 
64
68
  def to_h
@@ -69,8 +73,14 @@ module SwaggerYard
69
73
  else
70
74
  json_type
71
75
  end
76
+
72
77
  if array?
73
78
  { "type" => "array", "items" => type }
79
+ elsif object?
80
+ {
81
+ "type" => "object",
82
+ "additionalProperties" => Type.from_type_list([object.join("<")]).to_h
83
+ }
74
84
  else
75
85
  type
76
86
  end
@@ -1,3 +1,3 @@
1
1
  module SwaggerYard
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_yard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - chtrinh (Chris Trinh)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard