grape-swagger-entity 0.3.1 → 0.5.0

This diff has not been reviewed by any users.
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
2
  SHA256:
3
- metadata.gz: 41afe8433047f9b998351967b5e2ecf4809413c1f622125c70b860c5bbf11a43
4
- data.tar.gz: 60e644e6a03258aa4284c348cd486b49027f21558c52805f49a8b08316d5d6bb
3
+ metadata.gz: 4c6aad956f868bcf6563b52eaaf6347b821fe06a63153324e299562857418dc3
4
+ data.tar.gz: 04ffc54b6c66cb328e7687aac44f9140f0891ab74c3fab05463bda4a43f02f41
5
5
  SHA512:
6
- metadata.gz: b16ce2ac2fe5e37c7d67cb5113a0870bcd43dc7addc92b54d983cd8e7f6bb599ea7ab3b13f251eac92d2039ecc120a641eab6b18a256294550f26812247b3e12
7
- data.tar.gz: f28eb0a4058f2e007e72acdc38746fa0c8cd72bd46927a1f9519a351c45c9a81124aede349a2780faed8619a0bc1cb95b9be720a2fe782b571fb827380c473dc
6
+ metadata.gz: e4e6cd881f3c194a10beb6489b295ecc0d3168479935c4402922d4e4f9358686c3aa2d3df1023e99114710c14efa9685d8f5b1792558538f680721d77d7b8177
7
+ data.tar.gz: 5090a6ae14257708c383c10b858b6c89ba21e54fd5a873dfe10dcd1873b1aac125b3d4be4ad0c055778461859b91c18c2505607ecbd4552384c1a0f01ce46033
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .ruby-gemset
11
+ .ruby-version
@@ -21,5 +21,5 @@ Metrics/MethodLength:
21
21
  Naming/FileName:
22
22
  Enabled: false
23
23
 
24
- Layout/IndentHash:
24
+ Layout/IndentFirstHashElement:
25
25
  EnforcedStyle: consistent
@@ -10,18 +10,19 @@ after_success:
10
10
  - bundle exec danger
11
11
 
12
12
  rvm:
13
- - 2.4.2
14
- - 2.3.5
13
+ - 2.4.5
14
+ - 2.5.3
15
+ - 2.6.0
15
16
 
16
17
  env:
17
- - GRAPE_ENTITY=0.5.2
18
18
  - GRAPE_ENTITY=0.6.1
19
+ - GRAPE_ENTITY=0.7.1
19
20
 
20
21
  matrix:
21
22
  fast_finish: true
22
23
 
23
24
  include:
24
- - rvm: 2.2.8
25
+ - rvm: 2.3.8
25
26
  env:
26
27
  - rvm: ruby-head
27
28
  env:
@@ -31,7 +32,7 @@ matrix:
31
32
  env:
32
33
 
33
34
  allow_failures:
34
- - rvm: 2.2.8
35
+ - rvm: 2.3.8
35
36
  - rvm: ruby-head
36
37
  - rvm: jruby-head
37
38
  - rvm: rbx-2
@@ -8,6 +8,41 @@
8
8
 
9
9
  * Your contribution here.
10
10
 
11
+ ### 0.5.0 (June 29, 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.4.0 (May 30, 2020)
18
+
19
+ #### Features
20
+
21
+ * [#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).
22
+ * [#48](https://github.com/ruby-grape/grape-swagger-entity/pull/48): Add support for extensions - [@MaximeRDY](https://github.com/MaximeRDY).
23
+
24
+ #### Fixes
25
+
26
+ * [#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).
27
+
28
+ ### 0.3.4 (January 9, 2020)
29
+
30
+ #### Features
31
+
32
+ * [#40](https://github.com/ruby-grape/grape-swagger-entity/pull/40): Add support for minLength and maxLength - [@fotos](https://github.com/fotos).
33
+
34
+ ### 0.3.3 (Fabruary 22, 2019)
35
+
36
+ #### Features
37
+
38
+ * [#39](https://github.com/ruby-grape/grape-swagger-entity/pull/39): Fix to avoid conflict with polymorphic model - [@kinoppyd](https://github.com/kinoppyd).
39
+
40
+ ### 0.3.2 (January 15, 2019)
41
+
42
+ #### Features
43
+
44
+ * [#38](https://github.com/ruby-grape/grape-swagger-entity/pull/38): Added support for hidden option for documentation - [@vitoravelino](https://github.com/vitoravelino).
45
+
11
46
  ### 0.3.1 (November 26, 2018)
12
47
 
13
48
  #### Features
data/Gemfile CHANGED
@@ -13,10 +13,10 @@ group :development, :test do
13
13
  gem 'rake'
14
14
  gem 'rdoc'
15
15
  gem 'rspec', '~> 3.0'
16
- gem 'rubocop', '~> 0.48'
16
+ gem 'rubocop', '~> 0.68.1'
17
17
  end
18
18
 
19
- gem 'grape-swagger', git: 'https://github.com/ruby-grape/grape-swagger.git'
19
+ gem 'grape-swagger', path: '../grape-swagger'
20
20
 
21
21
  group :test do
22
22
  gem 'grape-entity', ENV.fetch('GRAPE_ENTITY', '0.6.1')
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
 
@@ -19,5 +19,5 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.required_ruby_version = '>= 2.2.6'
21
21
  s.add_runtime_dependency 'grape-entity', '>= 0.5.0'
22
- s.add_runtime_dependency 'grape-swagger', '>= 0.31.0'
22
+ s.add_runtime_dependency 'grape-swagger', '>= 1.4.0'
23
23
  end
@@ -2,6 +2,7 @@ require 'grape-swagger'
2
2
  require 'grape-entity'
3
3
 
4
4
  require 'grape-swagger/entity/version'
5
+ require 'grape-swagger/entity/helper'
5
6
  require 'grape-swagger/entity/attribute_parser'
6
7
  require 'grape-swagger/entity/parser'
7
8
 
@@ -12,7 +12,7 @@ module GrapeSwagger
12
12
  entity_model = model_from(entity_options)
13
13
 
14
14
  if entity_model
15
- name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
15
+ name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
16
16
 
17
17
  entity_model_type = entity_model_type(name, entity_options)
18
18
  return entity_model_type unless documentation
@@ -24,8 +24,8 @@ module GrapeSwagger
24
24
  param = data_type_from(entity_options)
25
25
  return param unless documentation
26
26
 
27
- param[:default] = documentation[:default] if documentation[:default]
28
- add_attribute_example(param, documentation[:example])
27
+ add_attribute_sample(param, documentation, :default)
28
+ add_attribute_sample(param, documentation, :example)
29
29
 
30
30
  if (values = documentation[:values])
31
31
  param[:enum] = values if values.is_a?(Array)
@@ -36,6 +36,10 @@ module GrapeSwagger
36
36
  add_array_documentation(param, documentation)
37
37
  end
38
38
 
39
+ add_attribute_documentation(param, documentation)
40
+
41
+ add_extension_documentation(param, documentation)
42
+ add_discriminator_extension(param, documentation)
39
43
  param
40
44
  end
41
45
  end
@@ -103,10 +107,16 @@ module GrapeSwagger
103
107
  end
104
108
  end
105
109
 
106
- def add_attribute_example(attribute, example)
107
- return unless example
110
+ def add_attribute_sample(attribute, hash, key)
111
+ value = hash[key]
112
+ return if value.nil?
113
+
114
+ attribute[key] = value.is_a?(Proc) ? value.call : value
115
+ end
108
116
 
109
- attribute[:example] = example.is_a?(Proc) ? example.call : example
117
+ def add_attribute_documentation(param, documentation)
118
+ param[:minLength] = documentation[:min_length] if documentation.key?(:min_length)
119
+ param[:maxLength] = documentation[:max_length] if documentation.key?(:max_length)
110
120
  end
111
121
 
112
122
  def add_array_documentation(param, documentation)
@@ -114,6 +124,14 @@ module GrapeSwagger
114
124
  param[:maxItems] = documentation[:max_items] if documentation.key?(:max_items)
115
125
  param[:uniqueItems] = documentation[:unique_items] if documentation.key?(:unique_items)
116
126
  end
127
+
128
+ def add_extension_documentation(param, documentation)
129
+ GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(documentation, param)
130
+ end
131
+
132
+ def add_discriminator_extension(param, documentation)
133
+ param[:documentation] = { is_discriminator: true } if documentation.key?(:is_discriminator)
134
+ end
117
135
  end
118
136
  end
119
137
  end
@@ -0,0 +1,36 @@
1
+ module GrapeSwagger
2
+ module Entity
3
+ # Helper methods for DRY
4
+ class Helper
5
+ class << self
6
+ def model_name(entity_model, endpoint)
7
+ if endpoint.nil?
8
+ entity_model.to_s.demodulize
9
+ else
10
+ endpoint.send(:expose_params_from_model, entity_model)
11
+ end
12
+ end
13
+
14
+ def discriminator(entity_model)
15
+ entity_model.superclass.root_exposures.detect do |value|
16
+ value.documentation.dig(:is_discriminator)
17
+ end
18
+ end
19
+
20
+ def root_exposures_without_parent(entity_model)
21
+ entity_model.root_exposures.select do |value|
22
+ entity_model.superclass.root_exposures.find_by(value.attribute).nil?
23
+ end
24
+ end
25
+
26
+ def root_exposure_with_discriminator(entity_model)
27
+ if discriminator(entity_model)
28
+ root_exposures_without_parent(entity_model)
29
+ else
30
+ entity_model.root_exposures
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -17,15 +17,24 @@ module GrapeSwagger
17
17
 
18
18
  private
19
19
 
20
+ class Alias
21
+ attr_reader :original, :renamed
22
+ def initialize(original, renamed)
23
+ @original = original
24
+ @renamed = renamed
25
+ end
26
+ end
27
+
20
28
  def extract_params(exposure)
21
- exposure.root_exposures.each_with_object({}) do |value, memo|
29
+ GrapeSwagger::Entity::Helper.root_exposure_with_discriminator(exposure).each_with_object({}) do |value, memo|
22
30
  if value.for_merge && (value.respond_to?(:entity_class) || value.respond_to?(:using_class_name))
23
31
  entity_class = value.respond_to?(:entity_class) ? value.entity_class : value.using_class_name
24
32
 
25
33
  extracted_params = extract_params(entity_class)
26
34
  memo.merge!(extracted_params)
27
35
  else
28
- memo[value.attribute] = value.send(:options)
36
+ opts = value.send(:options)
37
+ opts[:as] ? memo[Alias.new(value.attribute, opts[:as])] = opts : memo[value.attribute] = opts
29
38
  end
30
39
  end
31
40
  end
@@ -34,8 +43,12 @@ module GrapeSwagger
34
43
  return unless params
35
44
 
36
45
  parsed = params.each_with_object({}) do |(entity_name, entity_options), memo|
37
- next if entity_options.fetch(:documentation, {}).fetch(:in, nil).to_s == 'header'
46
+ documentation_options = entity_options.fetch(:documentation, {})
47
+ in_option = documentation_options.fetch(:in, nil).to_s
48
+ hidden_option = documentation_options.fetch(:hidden, nil)
49
+ next if in_option == 'header' || hidden_option == true
38
50
 
51
+ entity_name = entity_name.original if entity_name.is_a?(Alias)
39
52
  final_entity_name = entity_options.fetch(:as, entity_name)
40
53
  documentation = entity_options[:documentation]
41
54
 
@@ -51,7 +64,39 @@ module GrapeSwagger
51
64
  memo[final_entity_name][:description] = documentation[:desc] if documentation[:desc]
52
65
  end
53
66
 
54
- [parsed, required_params(params)]
67
+ discriminator = GrapeSwagger::Entity::Helper.discriminator(model)
68
+ if discriminator
69
+ respond_with_all_of(parsed, params, discriminator)
70
+ else
71
+ [parsed, required_params(params)]
72
+ end
73
+ end
74
+
75
+ def respond_with_all_of(parsed, params, discriminator)
76
+ parent_name = GrapeSwagger::Entity::Helper.model_name(model.superclass, endpoint)
77
+
78
+ {
79
+ allOf: [
80
+ {
81
+ '$ref' => "#/definitions/#{parent_name}"
82
+ },
83
+ [
84
+ add_discriminator(parsed, discriminator),
85
+ required_params(params).push(discriminator.attribute)
86
+ ]
87
+ ]
88
+ }
89
+ end
90
+
91
+ def add_discriminator(parsed, discriminator)
92
+ model_name = GrapeSwagger::Entity::Helper.model_name(model, endpoint)
93
+
94
+ parsed.merge(
95
+ discriminator.attribute => {
96
+ type: 'string',
97
+ enum: [model_name]
98
+ }
99
+ )
55
100
  end
56
101
 
57
102
  def parse_nested(entity_name, entity_options, parent_model = nil)
@@ -1,5 +1,5 @@
1
1
  module GrapeSwagger
2
2
  module Entity
3
- VERSION = '0.3.1'.freeze
3
+ VERSION = '0.5.0'.freeze
4
4
  end
5
5
  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.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Zaitsev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-26 00:00:00.000000000 Z
11
+ date: 2020-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape-entity
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.31.0
33
+ version: 1.4.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.4.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
@@ -83,8 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  - !ruby/object:Gem::Version
84
85
  version: '0'
85
86
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.7.6
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