grape-swagger-entity 0.3.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 728921dd6134e0517a9d041b71ad9eb576cbf69d
4
- data.tar.gz: f5a194a8dfff4a6d26283fdc52dbb75e0530e19a
2
+ SHA256:
3
+ metadata.gz: 45d768ff8d366802a511df94e675aed675d4961c965ac39a130df0e5a5a47f73
4
+ data.tar.gz: 29289e700cb29cd5d7078b58e04bb284c5ccad4da1f12e24917d31351cb2210c
5
5
  SHA512:
6
- metadata.gz: 6cde2391099a8e9347d51330d750148aa6d197d6aab5e3d369b1a864ebfe397f26aa42ac4ca1e7d388415f6956b250f491dfa480629899b13ed66a8e9e41fa7a
7
- data.tar.gz: 3936253b710cae0bdde531efa60bdc90e3f6c3063c990748c1f4d026cc4f94e9f3c8a0e3e73ed479898a5fbdfaf8bf83c1c5852dfefb0cf1288c2aac7c429394
6
+ metadata.gz: f1f7cb9609b3412b6fb624aaae0346b34549349a40f7912d1c72131fa7c905f47f8307928edb18dbcbc3ba6cefed34ecd861c125b60b45d8cbe1f4633a036199
7
+ data.tar.gz: b64399e310447774459e470cb7e9aa1621e2c544641ef16c6ec2e8a3a8cb472fff9cd8e33167a2b61d4939932aaca065172b01526ad099f0011fe989b2e4cdb0
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .ruby-gemset
11
+ .ruby-version
@@ -2,7 +2,7 @@ AllCops:
2
2
  Exclude:
3
3
  - vendor/**/*
4
4
  TargetRubyVersion:
5
- 2.2
5
+ 2.7
6
6
 
7
7
  inherit_from: .rubocop_todo.yml
8
8
 
@@ -21,5 +21,34 @@ Metrics/MethodLength:
21
21
  Naming/FileName:
22
22
  Enabled: false
23
23
 
24
- Layout/IndentHash:
24
+ Layout/FirstHashElementIndentation:
25
25
  EnforcedStyle: consistent
26
+
27
+ Layout/EmptyLinesAroundAttributeAccessor:
28
+ Enabled: true
29
+ Layout/SpaceAroundMethodCallOperator:
30
+ Enabled: true
31
+ Lint/DeprecatedOpenSSLConstant:
32
+ Enabled: true
33
+ Lint/MixedRegexpCaptureTypes:
34
+ Enabled: true
35
+ Lint/RaiseException:
36
+ Enabled: true
37
+ Lint/StructNewOverride:
38
+ Enabled: true
39
+ Style/ExponentialNotation:
40
+ Enabled: true
41
+ Style/HashEachMethods:
42
+ Enabled: true
43
+ Style/HashTransformKeys:
44
+ Enabled: true
45
+ Style/HashTransformValues:
46
+ Enabled: true
47
+ Style/RedundantFetchBlock:
48
+ Enabled: true
49
+ Style/RedundantRegexpCharacterClass:
50
+ Enabled: true
51
+ Style/RedundantRegexpEscape:
52
+ Enabled: true
53
+ Style/SlicingWithRange:
54
+ Enabled: true
@@ -1,49 +1,48 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-07-03 22:59:40 +0300 using RuboCop version 0.49.1.
3
+ # on 2020-06-29 22:12:50 UTC using RuboCop version 0.86.0.
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: 2
10
- Metrics/AbcSize:
11
- Max: 40
12
-
13
9
  # Offense count: 1
14
- # Configuration parameters: CountComments, ExcludedMethods.
15
- Metrics/BlockLength:
16
- Max: 27
10
+ # Configuration parameters: Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'grape-swagger-entity.gemspec'
15
+
16
+ # Offense count: 4
17
+ # Configuration parameters: IgnoredMethods.
18
+ Metrics/AbcSize:
19
+ Max: 30
17
20
 
18
21
  # Offense count: 1
19
22
  # Configuration parameters: CountComments.
20
23
  Metrics/ClassLength:
21
- Max: 124
24
+ Max: 111
22
25
 
23
- # Offense count: 1
26
+ # Offense count: 2
27
+ # Configuration parameters: IgnoredMethods.
24
28
  Metrics/CyclomaticComplexity:
25
- Max: 15
26
-
27
- # Offense count: 14
28
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
29
- # URISchemes: http, https
30
- Metrics/LineLength:
31
- Max: 120
29
+ Max: 11
32
30
 
33
- # Offense count: 3
34
- # Configuration parameters: CountComments.
31
+ # Offense count: 5
32
+ # Configuration parameters: CountComments, ExcludedMethods.
35
33
  Metrics/MethodLength:
36
- Max: 30
34
+ Max: 25
37
35
 
38
36
  # Offense count: 2
37
+ # Configuration parameters: IgnoredMethods.
39
38
  Metrics/PerceivedComplexity:
40
- Max: 16
39
+ Max: 12
41
40
 
42
- # Offense count: 2
41
+ # Offense count: 4
43
42
  Style/Documentation:
44
43
  Exclude:
45
44
  - 'spec/**/*'
46
45
  - 'test/**/*'
47
46
  - 'lib/grape-swagger/entity.rb'
48
- - 'lib/grape-swagger/entity/parser.rb'
49
47
  - 'lib/grape-swagger/entity/attribute_parser.rb'
48
+ - 'lib/grape-swagger/entity/parser.rb'
@@ -10,28 +10,27 @@ after_success:
10
10
  - bundle exec danger
11
11
 
12
12
  rvm:
13
- - 2.4.2
14
- - 2.3.5
13
+ - 2.5.8
14
+ - 2.6.6
15
+ - 2.7.1
15
16
 
16
17
  env:
17
- - GRAPE_ENTITY=0.5.2
18
18
  - GRAPE_ENTITY=0.6.1
19
+ - GRAPE_ENTITY=0.7.1
20
+ - GRAPE_ENTITY=0.8.0
19
21
 
20
22
  matrix:
21
23
  fast_finish: true
22
24
 
23
25
  include:
24
- - rvm: 2.2.8
26
+ - rvm: 2.4.10
25
27
  env:
26
28
  - rvm: ruby-head
27
29
  env:
28
30
  - rvm: jruby-head
29
31
  env:
30
- - rvm: rbx-2
31
- env:
32
32
 
33
33
  allow_failures:
34
- - rvm: 2.2.8
34
+ - rvm: 2.4.10
35
35
  - rvm: ruby-head
36
36
  - rvm: jruby-head
37
- - rvm: rbx-2
@@ -8,6 +8,39 @@
8
8
 
9
9
  * Your contribution here.
10
10
 
11
+ ### 0.5.1 (June 30, 2020)
12
+
13
+ #### Features
14
+
15
+ * [#50](https://github.com/ruby-grape/grape-swagger-entity/pull/50): Features/inheritance and discriminator - [@MaximeRDY](https://github.com/MaximeRDY).
16
+
17
+ ### 0.5.0 (June 29, 2020)
18
+
19
+ Yanked due to invalid dependency
20
+
21
+ ### 0.4.0 (May 30, 2020)
22
+
23
+ #### Features
24
+
25
+ * [#49](https://github.com/ruby-grape/grape-swagger-entity/pull/49): Bumped minimal required version of grape-swagger to 1.0.0 - [@Bugagazavr](https://github.com/Bugagazavr).
26
+ * [#48](https://github.com/ruby-grape/grape-swagger-entity/pull/48): Add support for extensions - [@MaximeRDY](https://github.com/MaximeRDY).
27
+
28
+ #### Fixes
29
+
30
+ * [#46](https://github.com/ruby-grape/grape-swagger-entity/pull/46): Fixed issue where a boolean example value of false would not display in swagger file - [@drewnichols](https://github.com/drewnichols).
31
+
32
+ ### 0.3.4 (January 9, 2020)
33
+
34
+ #### Features
35
+
36
+ * [#40](https://github.com/ruby-grape/grape-swagger-entity/pull/40): Add support for minLength and maxLength - [@fotos](https://github.com/fotos).
37
+
38
+ ### 0.3.3 (Fabruary 22, 2019)
39
+
40
+ #### Features
41
+
42
+ * [#39](https://github.com/ruby-grape/grape-swagger-entity/pull/39): Fix to avoid conflict with polymorphic model - [@kinoppyd](https://github.com/kinoppyd).
43
+
11
44
  ### 0.3.2 (January 15, 2019)
12
45
 
13
46
  #### Features
data/Dangerfile CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  danger.import_dangerfile(gem: 'ruby-grape-danger')
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in grape-swagger-entity.gemspec
@@ -12,8 +14,8 @@ group :development, :test do
12
14
  gem 'rack-test'
13
15
  gem 'rake'
14
16
  gem 'rdoc'
15
- gem 'rspec', '~> 3.0'
16
- gem 'rubocop', '~> 0.48'
17
+ gem 'rspec', '~> 3.9'
18
+ gem 'rubocop', '~> 0.85'
17
19
  end
18
20
 
19
21
  gem 'grape-swagger', git: 'https://github.com/ruby-grape/grape-swagger.git'
data/README.md CHANGED
@@ -26,7 +26,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
26
 
27
27
  ## Contributing
28
28
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/grape-swagger-entity.
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-grape/grape-swagger-entity.
30
30
 
31
31
  ## License
32
32
 
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'bundler'
3
5
 
data/bin/pry CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'grape-swagger/entity'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require 'grape-swagger/entity/version'
@@ -17,7 +19,7 @@ Gem::Specification.new do |s|
17
19
  s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
20
  s.require_paths = ['lib']
19
21
 
20
- s.required_ruby_version = '>= 2.2.6'
21
- s.add_runtime_dependency 'grape-entity', '>= 0.5.0'
22
- s.add_runtime_dependency 'grape-swagger', '>= 0.31.0'
22
+ s.required_ruby_version = '>= 2.4'
23
+ s.add_runtime_dependency 'grape-entity', '>= 0.6.0'
24
+ s.add_runtime_dependency 'grape-swagger', '>= 1.2.0'
23
25
  end
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'grape-swagger/entity'
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'grape-swagger'
2
4
  require 'grape-entity'
3
5
 
4
6
  require 'grape-swagger/entity/version'
7
+ require 'grape-swagger/entity/helper'
5
8
  require 'grape-swagger/entity/attribute_parser'
6
9
  require 'grape-swagger/entity/parser'
7
10
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GrapeSwagger
2
4
  module Entity
3
5
  class AttributeParser
@@ -12,7 +14,7 @@ module GrapeSwagger
12
14
  entity_model = model_from(entity_options)
13
15
 
14
16
  if entity_model
15
- name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
17
+ name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
16
18
 
17
19
  entity_model_type = entity_model_type(name, entity_options)
18
20
  return entity_model_type unless documentation
@@ -24,8 +26,8 @@ module GrapeSwagger
24
26
  param = data_type_from(entity_options)
25
27
  return param unless documentation
26
28
 
27
- param[:default] = documentation[:default] if documentation[:default]
28
- add_attribute_example(param, documentation[:example])
29
+ add_attribute_sample(param, documentation, :default)
30
+ add_attribute_sample(param, documentation, :example)
29
31
 
30
32
  if (values = documentation[:values])
31
33
  param[:enum] = values if values.is_a?(Array)
@@ -36,6 +38,10 @@ module GrapeSwagger
36
38
  add_array_documentation(param, documentation)
37
39
  end
38
40
 
41
+ add_attribute_documentation(param, documentation)
42
+
43
+ add_extension_documentation(param, documentation)
44
+ add_discriminator_extension(param, documentation)
39
45
  param
40
46
  end
41
47
  end
@@ -61,8 +67,7 @@ module GrapeSwagger
61
67
  end
62
68
 
63
69
  def ambiguous_model_type?(type)
64
- type &&
65
- type.is_a?(Class) &&
70
+ type&.is_a?(Class) &&
66
71
  !GrapeSwagger::DocMethods::DataType.primitive?(type.name.downcase) &&
67
72
  !type == Array
68
73
  end
@@ -83,7 +88,7 @@ module GrapeSwagger
83
88
  else
84
89
  { type: data_type }
85
90
  end
86
- type[:format] = documentation[:format] if documentation && documentation.key?(:format)
91
+ type[:format] = documentation[:format] if documentation&.key?(:format)
87
92
 
88
93
  type
89
94
  end
@@ -103,10 +108,16 @@ module GrapeSwagger
103
108
  end
104
109
  end
105
110
 
106
- def add_attribute_example(attribute, example)
107
- return unless example
111
+ def add_attribute_sample(attribute, hash, key)
112
+ value = hash[key]
113
+ return if value.nil?
114
+
115
+ attribute[key] = value.is_a?(Proc) ? value.call : value
116
+ end
108
117
 
109
- attribute[:example] = example.is_a?(Proc) ? example.call : example
118
+ def add_attribute_documentation(param, documentation)
119
+ param[:minLength] = documentation[:min_length] if documentation.key?(:min_length)
120
+ param[:maxLength] = documentation[:max_length] if documentation.key?(:max_length)
110
121
  end
111
122
 
112
123
  def add_array_documentation(param, documentation)
@@ -114,6 +125,14 @@ module GrapeSwagger
114
125
  param[:maxItems] = documentation[:max_items] if documentation.key?(:max_items)
115
126
  param[:uniqueItems] = documentation[:unique_items] if documentation.key?(:unique_items)
116
127
  end
128
+
129
+ def add_extension_documentation(param, documentation)
130
+ GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(documentation, param)
131
+ end
132
+
133
+ def add_discriminator_extension(param, documentation)
134
+ param[:documentation] = { is_discriminator: true } if documentation.key?(:is_discriminator)
135
+ end
117
136
  end
118
137
  end
119
138
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GrapeSwagger
4
+ module Entity
5
+ # Helper methods for DRY
6
+ class Helper
7
+ class << self
8
+ def model_name(entity_model, endpoint)
9
+ if endpoint.nil?
10
+ entity_model.to_s.demodulize
11
+ else
12
+ endpoint.send(:expose_params_from_model, entity_model)
13
+ end
14
+ end
15
+
16
+ def discriminator(entity_model)
17
+ entity_model.superclass.root_exposures.detect do |value|
18
+ value.documentation&.dig(:is_discriminator)
19
+ end
20
+ end
21
+
22
+ def root_exposures_without_parent(entity_model)
23
+ entity_model.root_exposures.select do |value|
24
+ entity_model.superclass.root_exposures.find_by(value.attribute).nil?
25
+ end
26
+ end
27
+
28
+ def root_exposure_with_discriminator(entity_model)
29
+ if discriminator(entity_model)
30
+ root_exposures_without_parent(entity_model)
31
+ else
32
+ entity_model.root_exposures
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GrapeSwagger
2
4
  module Entity
3
5
  class Parser
@@ -17,15 +19,25 @@ module GrapeSwagger
17
19
 
18
20
  private
19
21
 
22
+ class Alias
23
+ attr_reader :original, :renamed
24
+
25
+ def initialize(original, renamed)
26
+ @original = original
27
+ @renamed = renamed
28
+ end
29
+ end
30
+
20
31
  def extract_params(exposure)
21
- exposure.root_exposures.each_with_object({}) do |value, memo|
32
+ GrapeSwagger::Entity::Helper.root_exposure_with_discriminator(exposure).each_with_object({}) do |value, memo|
22
33
  if value.for_merge && (value.respond_to?(:entity_class) || value.respond_to?(:using_class_name))
23
34
  entity_class = value.respond_to?(:entity_class) ? value.entity_class : value.using_class_name
24
35
 
25
36
  extracted_params = extract_params(entity_class)
26
37
  memo.merge!(extracted_params)
27
38
  else
28
- memo[value.attribute] = value.send(:options)
39
+ opts = value.send(:options)
40
+ opts[:as] ? memo[Alias.new(value.attribute, opts[:as])] = opts : memo[value.attribute] = opts
29
41
  end
30
42
  end
31
43
  end
@@ -39,6 +51,7 @@ module GrapeSwagger
39
51
  hidden_option = documentation_options.fetch(:hidden, nil)
40
52
  next if in_option == 'header' || hidden_option == true
41
53
 
54
+ entity_name = entity_name.original if entity_name.is_a?(Alias)
42
55
  final_entity_name = entity_options.fetch(:as, entity_name)
43
56
  documentation = entity_options[:documentation]
44
57
 
@@ -54,7 +67,39 @@ module GrapeSwagger
54
67
  memo[final_entity_name][:description] = documentation[:desc] if documentation[:desc]
55
68
  end
56
69
 
57
- [parsed, required_params(params)]
70
+ discriminator = GrapeSwagger::Entity::Helper.discriminator(model)
71
+ if discriminator
72
+ respond_with_all_of(parsed, params, discriminator)
73
+ else
74
+ [parsed, required_params(params)]
75
+ end
76
+ end
77
+
78
+ def respond_with_all_of(parsed, params, discriminator)
79
+ parent_name = GrapeSwagger::Entity::Helper.model_name(model.superclass, endpoint)
80
+
81
+ {
82
+ allOf: [
83
+ {
84
+ '$ref' => "#/definitions/#{parent_name}"
85
+ },
86
+ [
87
+ add_discriminator(parsed, discriminator),
88
+ required_params(params).push(discriminator.attribute)
89
+ ]
90
+ ]
91
+ }
92
+ end
93
+
94
+ def add_discriminator(parsed, discriminator)
95
+ model_name = GrapeSwagger::Entity::Helper.model_name(model, endpoint)
96
+
97
+ parsed.merge(
98
+ discriminator.attribute => {
99
+ type: 'string',
100
+ enum: [model_name]
101
+ }
102
+ )
58
103
  end
59
104
 
60
105
  def parse_nested(entity_name, entity_options, parent_model = nil)
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GrapeSwagger
2
4
  module Entity
3
- VERSION = '0.3.2'.freeze
5
+ VERSION = '0.5.1'
4
6
  end
5
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-swagger-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Zaitsev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-15 00:00:00.000000000 Z
11
+ date: 2020-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape-entity
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.5.0
19
+ version: 0.6.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.5.0
26
+ version: 0.6.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: grape-swagger
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.31.0
33
+ version: 1.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.31.0
40
+ version: 1.2.0
41
41
  description:
42
42
  email:
43
43
  - kirik910@gmail.com
@@ -62,6 +62,7 @@ files:
62
62
  - lib/grape-swagger-entity.rb
63
63
  - lib/grape-swagger/entity.rb
64
64
  - lib/grape-swagger/entity/attribute_parser.rb
65
+ - lib/grape-swagger/entity/helper.rb
65
66
  - lib/grape-swagger/entity/parser.rb
66
67
  - lib/grape-swagger/entity/version.rb
67
68
  homepage: https://github.com/ruby-grape/grape-swagger-entity
@@ -76,15 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
77
  requirements:
77
78
  - - ">="
78
79
  - !ruby/object:Gem::Version
79
- version: 2.2.6
80
+ version: '2.4'
80
81
  required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  requirements:
82
83
  - - ">="
83
84
  - !ruby/object:Gem::Version
84
85
  version: '0'
85
86
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.5.2
87
+ rubygems_version: 3.0.3
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Grape swagger adapter to support grape-entity object parsing