swaggard 4.0.3 → 4.0.4

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: 8cd440c54fb4a517bedb134570c0dd76ed67ef1ef1947d0bcd89dc72f70d89a8
4
- data.tar.gz: cdc795955a2d5d3d89e570657da280fa6fb4873a5bd562f086def5871c035354
3
+ metadata.gz: 74bd791cc89ad2a4bd1c6d2f04dd0d4d0554083ec34fdb77ade0e9d1303d8183
4
+ data.tar.gz: e535e2f373af7c06ac2b088af0359feb9a67fbd14e65b24204fa4944c28046c3
5
5
  SHA512:
6
- metadata.gz: e0789daceebcc72d62aca37584b4a2163bc1d5de00d20e22d990a8556797492d29fda8ac85acdbacbfde76b8c985047db02b656e0c91686ac386be7b4c9f088f
7
- data.tar.gz: d860cfcc26f4c3a6c241fb5409ed3077c8330023b171b6c8a729e300056166ee58afd94020f43598d271b488c12ec1192265cbcc8ac2d2ed8bbd62677b3be959
6
+ metadata.gz: 4a15e9bdd53d788aa02c63df29eac1432f623fdfc1d35ac250cf4c6a1810dc3065682e141735cafc0e33684671afe8f25465d95ee9bc451e30f3434a59eb50b6
7
+ data.tar.gz: 166fed2b90cbfcd83f1c7250b6244c0485e07d999657707a1d433d03316fd2eefc67fb710f488c73a2e762898213791cc5eb8a4bb4ae4ed5f5f3ac58806e5939
@@ -5,13 +5,14 @@ module Swaggard
5
5
  attr_reader :id
6
6
  attr_writer :description, :title, :ignore_inherited
7
7
 
8
- def initialize(id, ancestors: [])
8
+ def initialize(id, ancestors: [], collection: false)
9
9
  @id = id
10
10
  @title = ''
11
11
  @properties = []
12
12
  @description = ''
13
13
  @ancestors = ancestors
14
14
  @ignore_inherited = false
15
+ @collection = collection
15
16
  end
16
17
 
17
18
  def add_property(property)
@@ -41,19 +42,31 @@ module Swaggard
41
42
  end
42
43
 
43
44
  def to_doc(definitions)
44
- {}.tap do |doc|
45
- doc['title'] = @title if @title.present?
46
- doc['type'] = 'object'
45
+ all_properties = properties(definitions)
46
+ properties_hash = Hash[all_properties.map { |property| [property.id, property.to_doc] }]
47
+ required_properties = all_properties.select(&:required?).map(&:id)
47
48
 
48
- doc['description'] = @description if @description.present?
49
+ if @collection
50
+ items = { 'type' => 'object', 'properties' => properties_hash }
51
+ items['required'] = required_properties if required_properties.any?
49
52
 
50
- all_properties = properties(definitions)
53
+ {}.tap do |doc|
54
+ doc['title'] = @title if @title.present?
55
+ doc['type'] = 'array'
56
+ doc['description'] = @description if @description.present?
57
+ doc['items'] = items
58
+ end
59
+ else
60
+ {}.tap do |doc|
61
+ doc['title'] = @title if @title.present?
62
+ doc['type'] = 'object'
51
63
 
52
- doc['properties'] = Hash[all_properties.map { |property| [property.id, property.to_doc] }]
53
- required_properties = all_properties.select(&:required?).map(&:id)
54
- doc['required'] = required_properties if required_properties.any?
55
- end
64
+ doc['description'] = @description if @description.present?
56
65
 
66
+ doc['properties'] = properties_hash
67
+ doc['required'] = required_properties if required_properties.any?
68
+ end
69
+ end
57
70
  end
58
71
 
59
72
  end
@@ -1,3 +1,3 @@
1
1
  module Swaggard
2
- VERSION = '4.0.3'
2
+ VERSION = '4.0.4'
3
3
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Swaggard::Swagger::Definition do
4
+ let(:property) { Swaggard::Swagger::Property.new('name', Swaggard::Swagger::Type.new(:string)) }
5
+
6
+ describe '#to_doc' do
7
+ context 'when collection is false (default)' do
8
+ subject(:definition) { described_class.new('Foo') }
9
+
10
+ before { definition.add_property(property) }
11
+
12
+ it 'emits a type: object schema with inline properties' do
13
+ expect(definition.to_doc({})).to eq(
14
+ 'type' => 'object',
15
+ 'properties' => { 'name' => { 'type' => 'string' } }
16
+ )
17
+ end
18
+ end
19
+
20
+ context 'when collection is true' do
21
+ subject(:definition) { described_class.new('Foo_all', collection: true) }
22
+
23
+ before { definition.add_property(property) }
24
+
25
+ it 'emits a type: array schema with properties wrapped under items' do
26
+ expect(definition.to_doc({})).to eq(
27
+ 'type' => 'array',
28
+ 'items' => {
29
+ 'type' => 'object',
30
+ 'properties' => { 'name' => { 'type' => 'string' } }
31
+ }
32
+ )
33
+ end
34
+
35
+ context 'with required properties' do
36
+ let(:property) do
37
+ Swaggard::Swagger::Property.new('name', Swaggard::Swagger::Type.new(:string), '', true)
38
+ end
39
+
40
+ it 'places the required array inside items' do
41
+ doc = definition.to_doc({})
42
+
43
+ expect(doc['required']).to be_nil
44
+ expect(doc['items']['required']).to eq(['name'])
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swaggard
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.3
4
+ version: 4.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Gomez
@@ -162,6 +162,7 @@ files:
162
162
  - spec/integration/openapi_spec.rb
163
163
  - spec/integration/swaggard_spec.rb
164
164
  - spec/spec_helper.rb
165
+ - spec/swaggard/swagger/definition_spec.rb
165
166
  homepage: https://github.com/adrian-gomez/swaggard
166
167
  licenses:
167
168
  - MIT
@@ -199,3 +200,4 @@ test_files:
199
200
  - spec/integration/openapi_spec.rb
200
201
  - spec/integration/swaggard_spec.rb
201
202
  - spec/spec_helper.rb
203
+ - spec/swaggard/swagger/definition_spec.rb