grape-swagger 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/ci.yml +45 -0
  4. data/.gitignore +1 -0
  5. data/.rspec +2 -0
  6. data/.rubocop.yml +8 -1
  7. data/.rubocop_todo.yml +31 -11
  8. data/CHANGELOG.md +49 -1
  9. data/CONTRIBUTING.md +1 -1
  10. data/Gemfile +6 -3
  11. data/README.md +40 -8
  12. data/UPGRADING.md +15 -0
  13. data/example/config.ru +2 -2
  14. data/grape-swagger.gemspec +3 -2
  15. data/lib/grape-swagger/doc_methods/build_model_definition.rb +1 -20
  16. data/lib/grape-swagger/doc_methods/format_data.rb +2 -0
  17. data/lib/grape-swagger/doc_methods/move_params.rb +13 -15
  18. data/lib/grape-swagger/doc_methods/parse_params.rb +34 -6
  19. data/lib/grape-swagger/endpoint.rb +27 -1
  20. data/lib/grape-swagger/rake/oapi_tasks.rb +37 -17
  21. data/lib/grape-swagger/version.rb +1 -1
  22. data/lib/grape-swagger.rb +2 -2
  23. data/spec/issues/579_align_put_post_parameters_spec.rb +5 -5
  24. data/spec/issues/751_deeply_nested_objects_spec.rb +2 -2
  25. data/spec/issues/832_array_hash_float_decimal_spec.rb +111 -0
  26. data/spec/issues/847_route_param_options_spec.rb +37 -0
  27. data/spec/lib/move_params_spec.rb +55 -41
  28. data/spec/lib/oapi_tasks_spec.rb +16 -9
  29. data/spec/spec_helper.rb +0 -4
  30. data/spec/support/the_paths_definitions.rb +4 -4
  31. data/spec/swagger_v2/api_swagger_v2_additional_properties_spec.rb +83 -0
  32. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +79 -7
  33. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +7 -7
  34. data/spec/swagger_v2/boolean_params_spec.rb +3 -1
  35. data/spec/swagger_v2/param_values_spec.rb +1 -1
  36. metadata +11 -111
  37. data/.github/workflows/rubocop.yml +0 -26
  38. data/.github/workflows/ruby.yml +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fa0c33214a157ed16d147edc9813bbb6d745498947d211c2eff21ce86b023a6
4
- data.tar.gz: 27c475b099a28f9b8610ded30906edf1a974825153348d8d590abba13688f29d
3
+ metadata.gz: c2826417c60f2f12c57698a03f03a15b9c1603c9c19a5cfc5c6594aec5512f13
4
+ data.tar.gz: ed6a07977e30c0ec647f41e60acf2d0184ba8f8531c626da87213aa1136fec1b
5
5
  SHA512:
6
- metadata.gz: d01c3d660f905522d943bebf2e5a609231bba1938b0cc02c49908048a691a0f0f1baa184ec2fed63d8a72280a92f3aa208273ce64b2bada90b09725cff71b080
7
- data.tar.gz: 1316664cfc4ec624311e891cd5c4bc106739bc3be9cb363bac02acccaa5e282a9fc1f9012ca590313fef8b74a5d6930a0afda371c05657b271e5ddfaf9e502d4
6
+ metadata.gz: e032c09183d38e9ce114cb0c02f6c083a8d79339975678265ef2b6dc49945c8e977c4c2785a62f6aea0d0cc42ae72c8a6ece4cdd8d2fdb9db938eb27a819e25f
7
+ data.tar.gz: 42779db4d2991ddc177027f9ae4526dd6ff999faaf7abdd6a8f9cf44978878a19ce300b1440f32eaf562ca1575d1cc7f0c1a9215366eb66683d53a299767c5b0
@@ -12,3 +12,9 @@ updates:
12
12
  day: "friday"
13
13
  assignees:
14
14
  - "LeFnord"
15
+ - package-ecosystem: "github-actions"
16
+ directory: "/"
17
+ schedule:
18
+ interval: weekly
19
+ assignees:
20
+ - "LeFnord"
@@ -0,0 +1,45 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - '*'
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ rubocop:
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v3
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: '3.1'
22
+ bundler-cache: true
23
+ - name: Run rubocop
24
+ run: bundle exec rubocop --parallel --format progress
25
+
26
+ rspec:
27
+ runs-on: ubuntu-latest
28
+ needs: ['rubocop']
29
+ strategy:
30
+ matrix:
31
+ ruby-version: ['2.7', '3.0', '3.1', 'head']
32
+ grape-version: [1.6.2, 1.5.3]
33
+ model-parser: [grape-swagger-entity, grape-swagger-representable, '']
34
+ steps:
35
+ - name: Check out branch
36
+ uses: actions/checkout@v3
37
+ - name: Set up Ruby
38
+ uses: ruby/setup-ruby@v1
39
+ with:
40
+ ruby-version: ${{ matrix.ruby-version }}
41
+ GRAPE_VERSION: ${{ matrix.grape-version }}
42
+ MODEL_PARSER: ${{ matrix.model-parser }}
43
+ bundler-cache: true
44
+ - name: Run rspec rest of the suite
45
+ run: bundle exec rspec
data/.gitignore CHANGED
@@ -42,3 +42,4 @@ spec/params_entity_spec.rb
42
42
  vendor/bundle/
43
43
  spec/swagger_v2/x-dummy.rb
44
44
  coverage/
45
+ .byebug_history
data/.rspec CHANGED
@@ -1 +1,3 @@
1
1
  --color
2
+ --profile
3
+ --format documentation
data/.rubocop.yml CHANGED
@@ -5,7 +5,7 @@ AllCops:
5
5
  - vendor/**/*
6
6
  - example/**/*
7
7
  NewCops: enable
8
- TargetRubyVersion: 3.0
8
+ TargetRubyVersion: 3.1
9
9
  SuggestExtensions: false
10
10
 
11
11
  # Layout stuff
@@ -101,12 +101,19 @@ Style/HashEachMethods:
101
101
  Style/HashLikeCase:
102
102
  Enabled: true
103
103
 
104
+ Style/HashSyntax:
105
+ Enabled: false
106
+
104
107
  Style/HashTransformKeys:
105
108
  Enabled: true
106
109
 
107
110
  Style/HashTransformValues:
108
111
  Enabled: true
109
112
 
113
+ Style/OpenStructUse:
114
+ Exclude:
115
+ - spec/**/*
116
+
110
117
  Style/RegexpLiteral:
111
118
  Enabled: false
112
119
 
data/.rubocop_todo.yml CHANGED
@@ -1,11 +1,19 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-06-05 12:13:59 +0000 using RuboCop version 0.71.0.
3
+ # on 2022-01-14 10:22:29 UTC using RuboCop version 1.24.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 1
10
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: Include.
12
+ # Include: **/*.gemspec
13
+ Gemspec/RequireMFA:
14
+ Exclude:
15
+ - 'grape-swagger.gemspec'
16
+
9
17
  # Offense count: 1
10
18
  # Configuration parameters: Include.
11
19
  # Include: **/*.gemspec
@@ -13,20 +21,18 @@ Gemspec/RequiredRubyVersion:
13
21
  Exclude:
14
22
  - 'grape-swagger.gemspec'
15
23
 
16
- # Offense count: 30
24
+ # Offense count: 31
25
+ # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
17
26
  Metrics/AbcSize:
18
- Max: 59
19
-
20
- # Offense count: 10
21
- Metrics/CyclomaticComplexity:
22
- Max: 13
27
+ Max: 56
23
28
 
24
- # Offense count: 22
25
- # Configuration parameters: CountComments, ExcludedMethods.
29
+ # Offense count: 30
30
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
26
31
  Metrics/MethodLength:
27
- Max: 45
32
+ Max: 28
28
33
 
29
34
  # Offense count: 7
35
+ # Configuration parameters: IgnoredMethods.
30
36
  Metrics/PerceivedComplexity:
31
37
  Max: 16
32
38
 
@@ -35,6 +41,20 @@ Style/ClassVars:
35
41
  Exclude:
36
42
  - 'lib/grape-swagger/doc_methods.rb'
37
43
 
38
- # Offense count: 22
44
+ # Offense count: 23
45
+ # Configuration parameters: AllowedConstants.
39
46
  Style/Documentation:
40
47
  Enabled: false
48
+
49
+ # Offense count: 43
50
+ Style/OpenStructUse:
51
+ Exclude:
52
+ - 'spec/lib/endpoint_spec.rb'
53
+ - 'spec/lib/version_spec.rb'
54
+ - 'spec/support/mock_parser.rb'
55
+ - 'spec/support/model_parsers/mock_parser.rb'
56
+ - 'spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb'
57
+ - 'spec/swagger_v2/api_swagger_v2_mounted_spec.rb'
58
+ - 'spec/swagger_v2/api_swagger_v2_spec.rb'
59
+ - 'spec/swagger_v2/errors_spec.rb'
60
+ - 'spec/swagger_v2/reference_entity_spec.rb'
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### Next
1
+ ### next
2
2
 
3
3
  #### Features
4
4
 
@@ -9,6 +9,54 @@
9
9
  * Your contribution here.
10
10
 
11
11
 
12
+ ### 1.5.0 (July 28, 2022)
13
+
14
+ #### Features
15
+
16
+ * [#862](https://github.com/ruby-grape/grape-swagger/pull/862): Allow using nicknames for body definitions - [@magni-](https://github.com/magni-)
17
+
18
+ #### Fixes
19
+
20
+ * [#860](https://github.com/ruby-grape/grape-swagger/pull/860) chore: Included githubactions in the dependabot config [@naveensrinivasan](https://github.com/naveensrinivasan)
21
+ * [#843](https://github.com/ruby-grape/grape-swagger/pull/843) Syntax errors in README.md examples [@remvee](https://github.com/remvee)
22
+ * [#844](https://github.com/ruby-grape/grape-swagger/pull/844) Fixes the regexp used for parsing routes [@senhalil](https://github.com/senhalil)
23
+ * [#847](https://github.com/ruby-grape/grape-swagger/pull/847) Parse route_param type for nested endpoints [@dmoss18](https://github.com/dmoss18)
24
+ * [#856](https://github.com/ruby-grape/grape-swagger/pull/856) Remove unused methods in GrapeSwagger::DocMethods::BuildModelDefinition [@takahashim](https://github.com/takahashim)
25
+ * [#858](https://github.com/ruby-grape/grape-swagger/pull/858): Set permissions for GitHub actions [@naveensrinivasan](https://github.com/naveensrinivasan)
26
+ * [#853](https://github.com/ruby-grape/grape-swagger/pull/853): Add webrick to support Ruby 3.x [@takahashim](https://github.com/takahashim)
27
+ * [#852](https://github.com/ruby-grape/grape-swagger/pull/852): Fix example to work [@takahashim](https://github.com/takahashim)
28
+ * [#846](https://github.com/ruby-grape/grape-swagger/pull/846): Refactor oapi fetch task [@Vachman](https://github.com/Vachman)
29
+ * [#850](https://github.com/ruby-grape/grape-swagger/pull/850): Fix value of enum to be Array [@takahashim](https://github.com/takahashim)
30
+
31
+
32
+ ### 1.4.3 (January 5, 2022)
33
+
34
+ #### Fixes
35
+
36
+ * [#850](https://github.com/ruby-grape/grape-swagger/pull/850): Fix value of `enum` to be `Array` - [@takahashim](https://github.com/takahashim)
37
+ * [#846](https://github.com/ruby-grape/grape-swagger/pull/846): Fixes oapi rake tasks, allows generating sepcs for different API versions.
38
+ * [#852](https://github.com/ruby-grape/grape-swagger/pull/852): Fix example to work without error - [@takahashim](https://github.com/takahashim)
39
+ * [#853](https://github.com/ruby-grape/grape-swagger/pull/853): Add webrick gem so that example works in Ruby 3.x - [@takahashim](https://github.com/takahashim)
40
+ * [#844](https://github.com/ruby-grape/grape-swagger/pull/844): Fixes the regexp used for parsing routes - [@senhalil](https://github.com/senhalil)
41
+ * [#862](https://github.com/ruby-grape/grape-swagger/pull/862): Allow using nicknames for body definitions - [@magni-](https://github.com/magni-)
42
+ * Your contribution here.
43
+
44
+ ### 1.4.2 (October 22, 2021)
45
+
46
+ #### Fixes
47
+
48
+ * [#840](https://github.com/ruby-grape/grape-swagger/pull/840): Fixes documentation of `additionalProperties` field when used with array parameters, or when setting it to `false` - [@magni-](https://github.com/magni-)
49
+ * [#841](https://github.com/ruby-grape/grape-swagger/pull/839): Fixes `type` and `format` values for object fields nested in an array ([#832](https://github.com/ruby-grape/grape-swagger/issue/832)) - [@magni-](https://github.com/magni-)
50
+ * [#839](https://github.com/ruby-grape/grape-swagger/pull/839): Fixes documentation of `false` or `nil` default parameter values - [@magni-](https://github.com/magni-)
51
+
52
+
53
+ ### 1.4.1 (September 15, 2021)
54
+
55
+ #### Fixes
56
+
57
+ * [#833](https://github.com/ruby-grape/grape-swagger/pull/833): Fixes issue of examples not showing for `in: 'body'` parameters - [@stevenou](https://github.com/stevenou)
58
+
59
+
12
60
  ### 1.4.0 (March 20, 2021)
13
61
 
14
62
  #### Features
data/CONTRIBUTING.md CHANGED
@@ -115,7 +115,7 @@ git push origin my-feature-branch -f
115
115
 
116
116
  ## Check on Your Pull Request
117
117
 
118
- Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
118
+ Go back to your pull request after a few minutes and see whether it passed muster with GitHub Actions. Everything should look green, otherwise fix issues and amend your commit as described above.
119
119
 
120
120
  ## Be Patient
121
121
 
data/Gemfile CHANGED
@@ -6,14 +6,14 @@ ruby RUBY_VERSION
6
6
 
7
7
  gemspec
8
8
 
9
- gem 'grape', case version = ENV['GRAPE_VERSION'] || '>= 1.5.0'
9
+ gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '~> 1.6')
10
10
  when 'HEAD'
11
11
  { git: 'https://github.com/ruby-grape/grape' }
12
12
  else
13
13
  version
14
14
  end
15
15
 
16
- gem ENV['MODEL_PARSER'] if ENV.key?('MODEL_PARSER')
16
+ gem ENV.fetch('MODEL_PARSER', nil) if ENV.key?('MODEL_PARSER')
17
17
 
18
18
  group :development, :test do
19
19
  gem 'bundler'
@@ -28,14 +28,17 @@ group :development, :test do
28
28
  gem 'rdoc'
29
29
  gem 'rspec', '~> 3.9'
30
30
  gem 'rubocop', '~> 1.0', require: false
31
+ gem 'webrick'
31
32
  end
32
33
 
33
34
  group :test do
34
35
  gem 'coveralls_reborn', require: false
35
36
 
36
- gem 'ruby-grape-danger', '~> 0.1.1', require: false
37
+ gem 'ruby-grape-danger', '~> 0.2.0', require: false
37
38
  gem 'simplecov', require: false
39
+ end
38
40
 
41
+ group :test, :development do
39
42
  unless ENV['MODEL_PARSER'] == 'grape-swagger-entity'
40
43
  gem 'grape-swagger-entity', git: 'https://github.com/ruby-grape/grape-swagger-entity'
41
44
  end
data/README.md CHANGED
@@ -451,6 +451,7 @@ add_swagger_documentation \
451
451
  * [Collection Format](#collection-format)
452
452
  * [Hiding parameters](#hiding-parameters)
453
453
  * [Setting a Swagger default value](#default-value)
454
+ * [Setting `additionalProperties` for `object`-type parameters](#additional-properties)
454
455
  * [Example parameter value](#param-example)
455
456
  * [Response documentation](#response)
456
457
  * [Changing default status codes](#change-status)
@@ -779,6 +780,36 @@ params do
779
780
  end
780
781
  ```
781
782
 
783
+ ### Setting `additionalProperties` for `object`-type parameters <a name="additional-properties">
784
+
785
+ Use the `additional_properties` option in the `documentation` hash for `object`-type parameters to set [`additionalProperties`](https://swagger.io/specification/v2/#model-with-mapdictionary-properties).
786
+
787
+ #### Allow any additional properties
788
+ ```ruby
789
+ params do
790
+ optional :thing, type: Hash, documentation: { additional_properties: true }
791
+ end
792
+ ```
793
+
794
+ #### Allow any additional properties of a particular type
795
+ ```ruby
796
+ params do
797
+ optional :thing, type: Hash, documentation: { additional_properties: String }
798
+ end
799
+ ```
800
+
801
+ #### Allow any additional properties matching a defined schema
802
+ ```ruby
803
+ class Entity < Grape::Entity
804
+ expose :this
805
+ end
806
+
807
+ params do
808
+ optional :thing, type: Hash, documentation: { additional_properties: Entity }
809
+ end
810
+ ```
811
+
812
+
782
813
  #### Example parameter value <a name="param-example"></a>
783
814
 
784
815
  The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
@@ -786,7 +817,7 @@ The example parameter will populate the Swagger UI with the example value, and c
786
817
  ```ruby
787
818
  params do
788
819
  requires :id, type: Integer, documentation: { example: 123 }
789
- optional :name, type String, documentation: { example: 'Buddy Guy' }
820
+ optional :name, type: String, documentation: { example: 'Buddy Guy' }
790
821
  end
791
822
  ```
792
823
 
@@ -812,7 +843,7 @@ namespace 'store/order', desc: 'Order operations within a store', swagger: { nes
812
843
  get :order_id do
813
844
  ...
814
845
  end
815
- namespace 'actions', desc: 'Order actions' do, nested: false
846
+ namespace 'actions', desc: 'Order actions', nested: false do
816
847
  get 'evaluate' do
817
848
  ...
818
849
  end
@@ -1234,7 +1265,7 @@ end
1234
1265
 
1235
1266
  The result will look like following:
1236
1267
 
1237
- ```
1268
+ ```json
1238
1269
  "responses": {
1239
1270
  "200": {
1240
1271
  "description": "Get a kitten",
@@ -1261,7 +1292,7 @@ end
1261
1292
 
1262
1293
  The result will look like following:
1263
1294
 
1264
- ```
1295
+ ```json
1265
1296
  "responses": {
1266
1297
  "200": {
1267
1298
  "description": "Get kittens",
@@ -1288,7 +1319,7 @@ get '/things' do
1288
1319
  end
1289
1320
  ```
1290
1321
  The result will look like following:
1291
- ```
1322
+ ```json
1292
1323
  "responses": {
1293
1324
  "200": {
1294
1325
  "description": "Multiple response",
@@ -1320,7 +1351,7 @@ get '/things' do
1320
1351
  end
1321
1352
  ```
1322
1353
  The result will look like following:
1323
- ```
1354
+ ```json
1324
1355
  "responses": {
1325
1356
  "200": {
1326
1357
  "description": "Multiple response with array",
@@ -1494,7 +1525,7 @@ end
1494
1525
  ```
1495
1526
 
1496
1527
  Should generate this definitions:
1497
- ```JSON
1528
+ ```json
1498
1529
  {
1499
1530
  "definitions": {
1500
1531
  "Pet": {
@@ -1687,9 +1718,10 @@ GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
1687
1718
  ```
1688
1719
  rake oapi:fetch
1689
1720
  params:
1690
- - store={ true | file_name } – save as JSON (optional)
1721
+ - store={ true | file_name.json } – save as JSON (optional)
1691
1722
  - resource=resource_name – get only for this one (optional)
1692
1723
  ```
1724
+ For mutliversion API it creates several files with following naming: file_name_`API_VERSION`.json
1693
1725
 
1694
1726
  #### OpenApi/Swagger Validation
1695
1727
 
data/UPGRADING.md CHANGED
@@ -1,5 +1,20 @@
1
1
  ## Upgrading Grape-swagger
2
2
 
3
+ ### Upgrading to >= 1.5.0
4
+
5
+ - The names generated for body parameter definitions and their references has changed. It'll now include the HTTP action as well as any path parameters.
6
+ - E.g, given a `PUT /things/:id` endpoint, `paths.things/{id}.put.parameters` in the generated Swaggerfile will contain the following:
7
+ - With `grape-swagger < 1.5.0`: `{ "name": "Things", ..., "schema": { "$ref": "#/definitions/putThings" } }`
8
+ - With `grape-swagger >= 1.5.0`: `{ "name": "putThingsId", ..., "schema": { "$ref": "#/definitions/putThingsId" } }`
9
+ - If you use the `nickname` option for an endpoint, that nickname will be used for both the parameter name and its definition reference.
10
+ - E.g., if the endpoint above were nicknamed `put-thing`, the generated Swaggerfile will contain `{ "name": "put-thing", ..., "schema": { "$ref": "#/definitions/put-thing" } }`
11
+
12
+
13
+ ### Upgrading to >= 1.4.2
14
+
15
+ - `additionalProperties` has been deprecated and will be removed in a future version of `grape-swagger`. It has been replaced with `additional_properties`.
16
+ - The value of the `enum` attribute is now always an Array. If it has only one value, it will be a one-element Array.
17
+
3
18
  ### Upgrading to >= 1.4.0
4
19
 
5
20
  - Official support for ruby < 2.5 removed, ruby 2.5 only in testing mode, but no support.
data/example/config.ru CHANGED
@@ -1,4 +1,5 @@
1
- require 'rack/cors'
1
+ Bundler.require ENV['RACK_ENV']
2
+
2
3
  use Rack::Cors do
3
4
  allow do
4
5
  origins '*'
@@ -12,7 +13,6 @@ require './api/endpoints'
12
13
  require './api/entities'
13
14
 
14
15
  class Base < Grape::API
15
- require 'grape-entity'
16
16
  require '../lib/grape-swagger'
17
17
  format :json
18
18
 
@@ -13,10 +13,11 @@ Gem::Specification.new do |s|
13
13
  s.summary = 'Add auto generated documentation to your Grape API that can be displayed with Swagger.'
14
14
  s.license = 'MIT'
15
15
 
16
- s.required_ruby_version = '>= 2.5'
16
+ s.metadata['rubygems_mfa_required'] = 'true'
17
+
18
+ s.required_ruby_version = '>= 2.7'
17
19
  s.add_runtime_dependency 'grape', '~> 1.3'
18
20
 
19
21
  s.files = `git ls-files`.split("\n")
20
- s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
21
22
  s.require_paths = ['lib']
22
23
  end
@@ -4,14 +4,9 @@ module GrapeSwagger
4
4
  module DocMethods
5
5
  class BuildModelDefinition
6
6
  class << self
7
- def build(model, properties, required, other_def_properties = {})
7
+ def build(_model, properties, required, other_def_properties = {})
8
8
  definition = { type: 'object', properties: properties }.merge(other_def_properties)
9
9
 
10
- if required.nil?
11
- required_attrs = required_attributes(model)
12
- definition[:required] = required_attrs unless required_attrs.blank?
13
- end
14
-
15
10
  definition[:required] = required if required.is_a?(Array) && required.any?
16
11
 
17
12
  definition
@@ -67,20 +62,6 @@ module GrapeSwagger
67
62
  end
68
63
  end
69
64
  end
70
-
71
- private
72
-
73
- def required_attributes(model)
74
- parse_entity(model) || parse_representable(model)
75
- end
76
-
77
- def parse_entity(model)
78
- return unless model.respond_to?(:documentation)
79
- end
80
-
81
- def parse_representable(model)
82
- return unless model.respond_to?(:map)
83
- end
84
65
  end
85
66
  end
86
67
  end
@@ -35,6 +35,8 @@ module GrapeSwagger
35
35
 
36
36
  def add_array(parameter, related_parameters)
37
37
  related_parameters.each do |p|
38
+ next if p.key?(:items)
39
+
38
40
  p_type = p[:type] == 'array' ? 'string' : p[:type]
39
41
  p[:items] = { type: p_type, format: p[:format], enum: p[:enum], is_array: p[:is_array] }
40
42
  p[:items].delete_if { |_k, v| v.nil? }
@@ -38,15 +38,15 @@ module GrapeSwagger
38
38
  end
39
39
 
40
40
  def parent_definition_of_params(params, path, route)
41
- definition_name = OperationId.manipulate(parse_model(path))
42
- referenced_definition = build_definition(definition_name, params, route.request_method.downcase)
43
- definition = @definitions[referenced_definition]
41
+ definition_name = OperationId.build(route, path)
42
+ build_definition(definition_name, params)
43
+ definition = @definitions[definition_name]
44
44
 
45
45
  move_params_to_new(definition, params)
46
46
 
47
47
  definition[:description] = route.description if route.try(:description)
48
48
 
49
- build_body_parameter(referenced_definition, definition_name, route.options)
49
+ build_body_parameter(definition_name, route.options)
50
50
  end
51
51
 
52
52
  def move_params_to_new(definition, params)
@@ -103,9 +103,9 @@ module GrapeSwagger
103
103
 
104
104
  def document_as_property(param)
105
105
  property_keys.each_with_object({}) do |x, memo|
106
- value = param[x]
107
- next if value.blank?
106
+ next unless param.key?(x)
108
107
 
108
+ value = param[x]
109
109
  if x == :type && @definitions[value].present?
110
110
  memo['$ref'] = "#/definitions/#{value}"
111
111
  else
@@ -142,17 +142,16 @@ module GrapeSwagger
142
142
  definition[:required].push(*value)
143
143
  end
144
144
 
145
- def build_body_parameter(reference, name, options)
145
+ def build_body_parameter(name, options)
146
146
  {}.tap do |x|
147
147
  x[:name] = options[:body_name] || name
148
148
  x[:in] = 'body'
149
149
  x[:required] = true
150
- x[:schema] = { '$ref' => "#/definitions/#{reference}" }
150
+ x[:schema] = { '$ref' => "#/definitions/#{name}" }
151
151
  end
152
152
  end
153
153
 
154
- def build_definition(name, params, verb = nil)
155
- name = "#{verb}#{name}" if verb
154
+ def build_definition(name, params)
156
155
  @definitions[name] = should_expose_as_array?(params) ? array_type : object_type
157
156
 
158
157
  name
@@ -181,7 +180,8 @@ module GrapeSwagger
181
180
  end
182
181
 
183
182
  def property_keys
184
- %i[type format description minimum maximum items enum default additionalProperties]
183
+ %i[type format description minimum maximum items enum default additional_properties additionalProperties
184
+ example]
185
185
  end
186
186
 
187
187
  def deletable?(param)
@@ -193,8 +193,7 @@ module GrapeSwagger
193
193
  end
194
194
 
195
195
  def includes_body_param?(params)
196
- params.map { |x| return true if x[:in] == 'body' || x[:param_type] == 'body' }
197
- false
196
+ params.any? { |x| x[:in] == 'body' || x[:param_type] == 'body' }
198
197
  end
199
198
 
200
199
  def should_expose_as_array?(params)
@@ -202,8 +201,7 @@ module GrapeSwagger
202
201
  end
203
202
 
204
203
  def should_exposed_as(params)
205
- params.map { |x| return 'object' if x[:type] && x[:type] != 'array' }
206
- 'array'
204
+ params.any? { |x| x[:type] && x[:type] != 'array' } ? 'object' : 'array'
207
205
  end
208
206
  end
209
207
  end
@@ -25,7 +25,7 @@ module GrapeSwagger
25
25
  document_default_value(settings) unless value_type[:is_array]
26
26
  document_range_values(settings) unless value_type[:is_array]
27
27
  document_required(settings)
28
- document_additional_properties(settings)
28
+ document_additional_properties(definitions, settings) unless value_type[:is_array]
29
29
  document_add_extensions(settings)
30
30
  document_example(settings)
31
31
 
@@ -51,7 +51,7 @@ module GrapeSwagger
51
51
  end
52
52
 
53
53
  def document_default_value(settings)
54
- @parsed_param[:default] = settings[:default] if settings[:default].present?
54
+ @parsed_param[:default] = settings[:default] if settings.key?(:default)
55
55
  end
56
56
 
57
57
  def document_type_and_format(settings, data_type)
@@ -105,12 +105,38 @@ module GrapeSwagger
105
105
 
106
106
  array_items[:default] = value_type[:default] if value_type[:default].present?
107
107
 
108
+ set_additional_properties, additional_properties = parse_additional_properties(definitions, value_type)
109
+ array_items[:additionalProperties] = additional_properties if set_additional_properties
110
+
108
111
  array_items
109
112
  end
110
113
 
111
- def document_additional_properties(settings)
112
- additional_properties = settings[:additionalProperties]
113
- @parsed_param[:additionalProperties] = additional_properties if additional_properties
114
+ def document_additional_properties(definitions, settings)
115
+ set_additional_properties, additional_properties = parse_additional_properties(definitions, settings)
116
+ @parsed_param[:additionalProperties] = additional_properties if set_additional_properties
117
+ end
118
+
119
+ def parse_additional_properties(definitions, settings)
120
+ return false unless settings.key?(:additionalProperties) || settings.key?(:additional_properties)
121
+
122
+ value =
123
+ if settings.key?(:additionalProperties)
124
+ GrapeSwagger::Errors::SwaggerSpecDeprecated.tell!(:additionalProperties)
125
+ settings[:additionalProperties]
126
+ else
127
+ settings[:additional_properties]
128
+ end
129
+
130
+ parsed_value =
131
+ if definitions[value.to_s]
132
+ { '$ref': "#/definitions/#{value}" }
133
+ elsif value.is_a?(Class)
134
+ { type: DataType.call(value) }
135
+ else
136
+ value
137
+ end
138
+
139
+ [true, parsed_value]
114
140
  end
115
141
 
116
142
  def document_example(settings)
@@ -137,8 +163,10 @@ module GrapeSwagger
137
163
  parse_enum_or_range_values(values.call) if values.parameters.empty?
138
164
  when Range
139
165
  parse_range_values(values) if values.first.is_a?(Integer)
166
+ when Array
167
+ { enum: values }
140
168
  else
141
- { enum: values } if values
169
+ { enum: [values] } if values
142
170
  end
143
171
  end
144
172