swaggard 1.1.1 → 1.2.0

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
  SHA256:
3
- metadata.gz: 57f3251f22ae437dfa4a4030dc6e1929def89936e209bd9fdef0b6d7ed92c554
4
- data.tar.gz: 859c0573aee509bf0f6c376e20d451c5f97729c3b6cf03f988f450d90041ae4d
3
+ metadata.gz: 7a49ea847a04d150e7b42923bf6a8290950f5fa18c53deba16907c36a59a664f
4
+ data.tar.gz: 3986acba75f1b962c13ca5b93ceb2f1481609f3516b5438689822d0916675952
5
5
  SHA512:
6
- metadata.gz: 4d7f32e8aef8e8f5edfd761982e8a9b845364de71a042f1e56ebae621a00ea7193c1d6377991a22c021d5a1316466031aeb25204fc23d388a4ba96cca9c30f2f
7
- data.tar.gz: d9b0b90737efd86097f61bbcc06ac9c3b51881a83bfff8ad1f726940c2140f640adff9e549d81dec907b3f221a2fa21737cbeab76fd0c80ca9f450e91315f756
6
+ metadata.gz: 3fd5f67a5c03861c8e78a77088f1aa4712a75d9fb251a2c1acb2e597f5609ae81b935d5ac090f5e49ae9dba7ab0a0ceb17be40a553ea183185d3fc88cf14c13f
7
+ data.tar.gz: db5042eb41674f3f85f94aa860743eed9861eb49d0bc44b9ed02bc36dcb578816552ff44dc2016a61bb01c5f90d71c0b3a31b23f55fd538d8b53403a9ac3cc85
@@ -7,7 +7,7 @@ module Swaggard
7
7
  def initialize
8
8
  @paths = {}
9
9
  @tags = {}
10
- @definitions = []
10
+ @definitions = {}
11
11
  end
12
12
 
13
13
  def add_tag(tag)
@@ -19,7 +19,7 @@ module Swaggard
19
19
  def add_operation(operation)
20
20
  @paths[operation.path] ||= Swagger::Path.new(operation.path)
21
21
  @paths[operation.path].add_operation(operation)
22
- @definitions.concat(operation.definitions)
22
+ @definitions.merge!(operation.definitions)
23
23
  end
24
24
 
25
25
  def ignore_put_if_patch!
@@ -51,7 +51,7 @@ module Swaggard
51
51
  'produces' => Swaggard.configuration.api_formats.map { |format| "application/#{format}" },
52
52
  'tags' => @tags.map { |_, tag| tag.to_doc },
53
53
  'paths' => Hash[@paths.values.map { |path| [format_path(path.path), path.to_doc] }],
54
- 'definitions' => Hash[@definitions.map { |definition| [definition.id, definition.to_doc] }]
54
+ 'definitions' => Hash[@definitions.map { |id, definition| [id, definition.to_doc(@definitions)] }]
55
55
  }
56
56
  end
57
57
 
@@ -6,24 +6,33 @@ module Swaggard
6
6
  class Models
7
7
 
8
8
  def run(yard_objects)
9
- definitions = []
9
+ definitions = {}
10
10
 
11
11
  yard_objects.each do |yard_object|
12
- next unless yard_object.type == :class
12
+ definition = parse_yard_object(yard_object)
13
13
 
14
- definition = Swagger::Definition.new(yard_object.path)
14
+ definitions[definition.id] = definition if definition
15
+ end
16
+
17
+ definitions
18
+ end
15
19
 
20
+ def parse_yard_object(yard_object)
21
+ return unless yard_object.type == :class
22
+
23
+ Swagger::Definition.new(yard_object.path, ancestors: yard_object.inheritance_tree.map(&:path)).tap do |definition|
16
24
  yard_object.tags.each do |tag|
17
- property = Swagger::Property.new(tag)
18
- definition.add_property(property)
25
+ case tag.tag_name
26
+ when 'attr'
27
+ property = Swagger::Property.new(tag)
28
+ definition.add_property(property)
29
+ when 'ignore_inherited'
30
+ definition.ignore_inherited = true
31
+ end
19
32
  end
20
-
21
- definitions << definition
22
33
  end
23
-
24
- definitions
25
34
  end
26
35
 
27
36
  end
28
37
  end
29
- end
38
+ end
@@ -3,13 +3,15 @@ module Swaggard
3
3
  class Definition
4
4
 
5
5
  attr_reader :id
6
- attr_writer :description, :title
6
+ attr_writer :description, :title, :ignore_inherited
7
7
 
8
- def initialize(id)
8
+ def initialize(id, ancestors: [])
9
9
  @id = id
10
10
  @title = ''
11
11
  @properties = []
12
12
  @description = ''
13
+ @ancestors = ancestors
14
+ @ignore_inherited = false
13
15
  end
14
16
 
15
17
  def add_property(property)
@@ -20,15 +22,35 @@ module Swaggard
20
22
  @properties.empty?
21
23
  end
22
24
 
23
- def to_doc
25
+ def properties(definitions)
26
+ inherited_properties(definitions)
27
+ .concat(@properties)
28
+ .uniq { |property| property.id }
29
+ end
30
+
31
+ def inherited_properties(definitions)
32
+ return [] if @ignore_inherited
33
+
34
+ @ancestors.flat_map do |ancestor|
35
+ definition = definitions[ancestor]
36
+
37
+ next unless definition && definition.id != id
38
+
39
+ definition.properties(definitions)
40
+ end.compact
41
+ end
42
+
43
+ def to_doc(definitions)
24
44
  {}.tap do |doc|
25
45
  doc['title'] = @title if @title.present?
26
46
  doc['type'] = 'object'
27
47
 
28
48
  doc['description'] = @description if @description.present?
29
49
 
30
- doc['properties'] = Hash[@properties.map { |property| [property.id, property.to_doc] }]
31
- required_properties = @properties.select(&:required?).map(&:id)
50
+ all_properties = properties(definitions)
51
+
52
+ doc['properties'] = Hash[all_properties.map { |property| [property.id, property.to_doc] }]
53
+ required_properties = all_properties.select(&:required?).map(&:id)
32
54
  doc['required'] = required_properties if required_properties.any?
33
55
  end
34
56
 
@@ -92,8 +92,12 @@ module Swaggard
92
92
  end
93
93
 
94
94
  def definitions
95
- @responses.map(&:definition).compact.tap do |definitions|
96
- definitions << @body_parameter.definition if @body_parameter
95
+ @responses.map(&:definition).compact.inject({}) do |definitions, definition|
96
+ definitions[definition.id] = definition
97
+ end.tap do |definitions|
98
+ next unless @body_parameter
99
+
100
+ definitions[@body_parameter.definition.id] = @body_parameter.definition
97
101
  end
98
102
  end
99
103
 
@@ -1,3 +1,3 @@
1
1
  module Swaggard
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/swaggard.rb CHANGED
@@ -38,6 +38,7 @@ module Swaggard
38
38
  ::YARD::Tags::Library.define_tag('Response description', :response_description)
39
39
  ::YARD::Tags::Library.define_tag('Response example', :response_example)
40
40
  ::YARD::Tags::Library.define_tag('Response header', :response_header)
41
+ ::YARD::Tags::Library.define_tag('Ignore inherited attributes', :ignore_inherited)
41
42
  end
42
43
 
43
44
  def get_doc(host = nil)
@@ -119,7 +120,6 @@ module Swaggard
119
120
 
120
121
  tags.each do |tag|
121
122
  @tags << [tag, operations]
122
- # [tag.controller_class.controller_path] ||= { tag: tag, operations: operations }
123
123
  end
124
124
  end
125
125
 
@@ -136,12 +136,12 @@ module Swaggard
136
136
  def parse_models
137
137
  parser = Parsers::Models.new
138
138
 
139
- definitions =[]
139
+ definitions = {}
140
140
  configuration.models_paths.each do |path|
141
141
  Dir[path].each do |file|
142
142
  yard_objects = get_yard_objects(file)
143
143
 
144
- definitions.concat(parser.run(yard_objects))
144
+ definitions.merge!(parser.run(yard_objects))
145
145
  end
146
146
 
147
147
  @api.definitions = definitions
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swaggard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Gomez
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-06 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -192,7 +192,6 @@ files:
192
192
  - spec/fixtures/dummy/config/application.rb
193
193
  - spec/fixtures/dummy/config/environments/development.rb
194
194
  - spec/fixtures/dummy/config/routes.rb
195
- - spec/fixtures/dummy/log/development.log
196
195
  - spec/fixtures/swagger_schema.json
197
196
  - spec/integration/swaggard_spec.rb
198
197
  - spec/spec_helper.rb
@@ -200,7 +199,7 @@ homepage: https://github.com/adrian-gomez/swaggard
200
199
  licenses:
201
200
  - MIT
202
201
  metadata: {}
203
- post_install_message:
202
+ post_install_message:
204
203
  rdoc_options: []
205
204
  require_paths:
206
205
  - lib
@@ -215,19 +214,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
214
  - !ruby/object:Gem::Version
216
215
  version: '0'
217
216
  requirements: []
218
- rubygems_version: 3.0.3
219
- signing_key:
217
+ rubygems_version: 3.2.15
218
+ signing_key:
220
219
  specification_version: 4
221
220
  summary: 'Swaggard: Swagger Rails REST API doc using yard YARD'
222
221
  test_files:
223
- - spec/spec_helper.rb
224
- - spec/integration/swaggard_spec.rb
225
- - spec/fixtures/swagger_schema.json
226
- - spec/fixtures/dummy/app/controllers/pets_controller.rb
227
- - spec/fixtures/dummy/app/controllers/application_controller.rb
222
+ - spec/fixtures/api.json
228
223
  - spec/fixtures/dummy/app/controllers/admin/pets_controller.rb
229
- - spec/fixtures/dummy/config/routes.rb
230
- - spec/fixtures/dummy/config/environments/development.rb
224
+ - spec/fixtures/dummy/app/controllers/application_controller.rb
225
+ - spec/fixtures/dummy/app/controllers/pets_controller.rb
231
226
  - spec/fixtures/dummy/config/application.rb
232
- - spec/fixtures/dummy/log/development.log
233
- - spec/fixtures/api.json
227
+ - spec/fixtures/dummy/config/environments/development.rb
228
+ - spec/fixtures/dummy/config/routes.rb
229
+ - spec/fixtures/swagger_schema.json
230
+ - spec/integration/swaggard_spec.rb
231
+ - spec/spec_helper.rb
File without changes