open_api_annotator 0.2.0 → 0.6.0

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: 1bb9e46a88ce7e7634106d19d12c7df63d2f5760456c0419bf90607e49e5f10b
4
- data.tar.gz: 0f6d152a177721e6b753a2867cc7670d93d63c78734b38da6119ba57f273f9bb
3
+ metadata.gz: 928d9daca39028c7d5016fa2e44ca116891194210ebaee873e325240bff2a428
4
+ data.tar.gz: f40479f6b544ccef964c611acf974763bc205b623682b6114d66707253638b75
5
5
  SHA512:
6
- metadata.gz: 5ffe01f6857c993dad939e5e0ae61091bc2536223ac6705c06d2f50d7646a53c8db746a7b76db88f24406e295dc2bf5f10b38cd9862175d3a8410afe65113da2
7
- data.tar.gz: 65d7dd45c64987f61bcaee80fa44c2a1b33215ccce83e5e7119a66cd61b96ead87682ab94858b952e3714806f3753beb648cfb6b9c59c22feac36775acea9410
6
+ metadata.gz: 7bfb5f922e2b92ef554b3b2d042a108471a4fe666fd69ed9c242eeae1e48b1105af7dc30caafd8d7be64babeb478990918136563b1deedf1d3f18099addfd098
7
+ data.tar.gz: 7198242aec1d0c81b3e47e0ba44bcbbbfea679673f9bdf31de31c753b45aff536e4c15f46cd26b854220fbe249c82b3aa46d1a78f8b2cdc86920f26a36cdbfe2
@@ -0,0 +1,29 @@
1
+ name: test
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ matrix:
7
+ ruby: ["2.5", "2.6", "2.7", "3.0"]
8
+ gemfile: [Gemfile]
9
+ include:
10
+ - ruby: "2.7"
11
+ gemfile: "gemfiles/rails-6.0.gemfile"
12
+ - ruby: "2.6"
13
+ gemfile: "gemfiles/rails-5.2.gemfile"
14
+
15
+ env:
16
+ BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
17
+
18
+ runs-on: ubuntu-latest
19
+ steps:
20
+ - uses: actions/checkout@v2
21
+ - uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby }}
24
+ bundler-cache: true
25
+ - run: bundle exec rake
26
+ - name: Send coverage
27
+ uses: paambaati/codeclimate-action@v2.7.5
28
+ env:
29
+ CC_TEST_REPORTER_ID: 921d046db793707b7d0c0d1305970a137fdf98fbc6ceeb6c355c44757040244f
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /tmp/
9
9
 
10
10
  Gemfile.lock
11
+ *.gemfile.lock
11
12
 
12
13
  # rspec failure tracking
13
14
  .rspec_status
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.1
1
+ 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## Not released yet
2
+
3
+ ## 0.6.0
4
+ - Support Ruby 3.0 and Rails 6.1
5
+ ## 0.5.0
6
+ - Allow using with rails 6
7
+
8
+ ## 0.4.0
9
+ - Enable to annotate primitive types to endpoint #11
10
+
11
+ ## 0.3.0
12
+ - Add path parameters to parameters #8
13
+ - Enable add always requried fields #9
14
+
1
15
  ## 0.1.0
2
16
  First release
3
17
 
data/README.md CHANGED
@@ -76,7 +76,7 @@ end
76
76
 
77
77
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
78
 
79
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number using `bundle exec bump patch`(or minor, major), and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
80
 
81
81
  ## Contributing
82
82
 
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in open_api_annotator.gemspec
6
+ gemspec path: '..'
7
+
8
+ gem 'railties', '~> 5.2.0'
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in open_api_annotator.gemspec
6
+ gemspec path: '..'
7
+
8
+ gem 'railties', '~> 6.0.0'
@@ -21,6 +21,10 @@ module OpenApiAnnotator
21
21
  schema.properties.merge!(build_attribute_properties(serializer))
22
22
  schema.properties.merge!(build_has_many_association_properties(serializer))
23
23
  schema.properties.merge!(build_has_one_and_belongs_to_association_properties(serializer))
24
+ required_fields = OpenApiAnnotator.config.always_required_fields
25
+ if required_fields.present?
26
+ schema.required = required_fields.select{|field| schema.properties[field] }
27
+ end
24
28
  schema
25
29
  end
26
30
 
@@ -28,11 +32,22 @@ module OpenApiAnnotator
28
32
  properties = {}
29
33
  serializer.open_api_attributes.each do |attribute|
30
34
  next unless attribute.valid?
31
- properties[attribute.name.to_sym] = OpenApi::Schema.new(
32
- type: attribute.type,
33
- format: attribute.format,
34
- nullable: attribute.nullable,
35
- )
35
+ properties[attribute.name.to_sym] = if attribute.type.is_a?(Array)
36
+ OpenApi::Schema.new(
37
+ type: "array",
38
+ items: OpenApi::Schema.new(
39
+ type: attribute.type.first,
40
+ format: attribute.format,
41
+ nullable: attribute.nullable,
42
+ )
43
+ )
44
+ else
45
+ OpenApi::Schema.new(
46
+ type: attribute.type,
47
+ format: attribute.format,
48
+ nullable: attribute.nullable,
49
+ )
50
+ end
36
51
  end
37
52
  properties
38
53
  end
@@ -5,6 +5,7 @@ module OpenApiAnnotator
5
5
  :path_regexp,
6
6
  :application_controller_class_name,
7
7
  :application_serializer_class_name,
8
+ :always_required_fields,
8
9
  )
9
10
  def application_serializer_class
10
11
  if application_serializer_class_name
@@ -50,6 +51,7 @@ module OpenApiAnnotator
50
51
  validate_path_regexp!
51
52
  validate_application_controller_class_name!
52
53
  validate_application_serializer_class_name!
54
+ validate_always_required_fields!
53
55
  end
54
56
 
55
57
  def validate_info!
@@ -95,6 +97,10 @@ module OpenApiAnnotator
95
97
  # Do nothing
96
98
  end
97
99
 
100
+ def validate_always_required_fields!
101
+ # Do nothing
102
+ end
103
+
98
104
  class InvalidError < StandardError; end
99
105
  end
100
106
  end
@@ -23,14 +23,26 @@ module OpenApiAnnotator
23
23
  media_type = resolve_media_type(route.controller_name, route.action_name)
24
24
  description = build_description(route.controller_name, route.action_name)
25
25
  next unless media_type
26
+ operation = OpenApi::Operation.new(responses: OpenApi::Responses.new)
26
27
  response = OpenApi::Response.new(
27
28
  description: description,
28
29
  content: {
29
30
  "application/json" => media_type,
30
31
  }
31
32
  )
32
- operation = OpenApi::Operation.new(responses: OpenApi::Responses.new)
33
33
  operation.responses["200"] = response
34
+ route.parameters.each do |parameter|
35
+ parameter = OpenApi::Parameter.new(
36
+ name: parameter[:name],
37
+ in: :path,
38
+ required: true,
39
+ schema: OpenApi::Schema.new(
40
+ type: :string
41
+ )
42
+ )
43
+ operation.parameters = [] unless operation.parameters
44
+ operation.parameters.push(parameter)
45
+ end
34
46
  path_item.operations[route.http_verb.underscore] = operation
35
47
  end
36
48
  path_item
@@ -42,9 +54,21 @@ module OpenApiAnnotator
42
54
 
43
55
  case type
44
56
  when Array
45
- "Returns an array of #{type.first.name}"
57
+ name = build_type_name(type.first)
58
+ "Returns an array of #{name}"
59
+ else
60
+ name = build_type_name(type)
61
+ "Returns a #{name}"
62
+ end
63
+ end
64
+
65
+ def build_type_name(type)
66
+ type = convert_primitive_class_to_data_type(type)
67
+ case type
68
+ when OpenApi::DataType
69
+ "#{type.name}"
46
70
  when Class
47
- "Returns a #{type.name}"
71
+ "#{type.name}"
48
72
  else
49
73
  raise "not supported class #{type.class}"
50
74
  end
@@ -56,18 +80,39 @@ module OpenApiAnnotator
56
80
 
57
81
  case type
58
82
  when Array
59
- content_class = type.first
60
- reference = OpenApi::Reference.new(ref: "#/components/schemas/#{content_class.name}")
61
- schema = OpenApi::Schema.new(type: "array", items: reference)
83
+ schema_of_array = resolve_media_type_schema(type.first)
84
+ schema = OpenApi::Schema.new(type: "array", items: schema_of_array)
85
+ OpenApi::MediaType.new(schema: schema)
86
+ else
87
+ schema = resolve_media_type_schema(type)
62
88
  OpenApi::MediaType.new(schema: schema)
89
+ end
90
+ end
91
+
92
+ def resolve_media_type_schema(type)
93
+ type = convert_primitive_class_to_data_type(type)
94
+ case type
95
+ when OpenApi::DataType
96
+ OpenApi::Schema.new(type: type.name, format: type.format)
63
97
  when Class
64
- reference = OpenApi::Reference.new(ref: "#/components/schemas/#{type.name}")
65
- OpenApi::MediaType.new(schema: reference)
98
+ OpenApi::Reference.new(ref: "#/components/schemas/#{type.name}")
66
99
  else
67
100
  raise "not supported class #{type.class}"
68
101
  end
69
102
  end
70
103
 
104
+ def convert_primitive_class_to_data_type(type)
105
+ if type == String
106
+ OpenApi::DataType.new(:string, :string, :string)
107
+ elsif type == Integer
108
+ OpenApi::DataType.new(:integer, :integer, :int32)
109
+ elsif type == Float
110
+ OpenApi::DataType.new(:float, :number, :float)
111
+ else
112
+ type
113
+ end
114
+ end
115
+
71
116
  def resolve_type(controller_name, action_name)
72
117
  controller_class_name = "#{controller_name}_controller".classify
73
118
  begin
@@ -81,38 +126,43 @@ module OpenApiAnnotator
81
126
  end
82
127
  end
83
128
 
84
- Route = Struct.new(:http_verb, :path, :controller_name, :action_name) do
85
- def initialize(http_verb:, path:, controller_name:, action_name:)
129
+ Route = Struct.new(:http_verb, :path, :controller_name, :action_name, :parameters) do
130
+ def initialize(http_verb:, path:, controller_name:, action_name:, parameters: [])
86
131
  self.http_verb = http_verb
87
132
  self.path = path
88
133
  self.controller_name = controller_name
89
134
  self.action_name = action_name
135
+ self.parameters = parameters
90
136
  end
91
137
  end
92
138
 
93
139
  class RoutesFinder
94
140
  def find_all
95
141
  @routes ||= Rails.application.routes.routes.routes.map do |route|
96
- path = PathResolver.new.resolve(route.path.ast)
142
+ parameters = []
143
+ path = PathResolver.new.resolve(route.path.ast, parameters)
97
144
  controller = route.requirements[:controller]
98
145
  action = route.requirements[:action]
99
- Route.new(http_verb: route.verb, path: path, controller_name: controller, action_name: action)
146
+ Route.new(http_verb: route.verb, path: path, controller_name: controller, action_name: action, parameters: parameters)
100
147
  end
101
148
  end
102
149
  end
103
150
 
104
151
  class PathResolver
105
- def resolve(ast)
152
+ def resolve(ast, parameters_context = [])
106
153
  res = ""
107
154
  if ast.type == :CAT
108
155
  left = ast.left
109
156
  res +=
110
157
  if left.type == :SYMBOL
158
+ parameters_context.push({
159
+ name: left.name,
160
+ })
111
161
  "{#{left.name}}"
112
162
  else
113
163
  left.to_s
114
164
  end
115
- res += resolve(ast.right)
165
+ res += resolve(ast.right, parameters_context)
116
166
  end
117
167
  res
118
168
  end
@@ -34,7 +34,7 @@ module OpenApiAnnotator
34
34
  validate_open_api_format!(options[:format])
35
35
  validate_open_api_nullable!(options[:nullable])
36
36
  rescue ValidationError => e
37
- Rails.logger.warn(e.message)
37
+ Rails.logger.warn("Within #{self.name}, #{field}: #{e.message}")
38
38
  end
39
39
 
40
40
  def validate_open_api_type!(type)
@@ -1,3 +1,3 @@
1
1
  module OpenApiAnnotator
2
- VERSION = "0.2.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -21,17 +21,18 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency "open_api", ">= 0.3.4"
24
+ spec.add_dependency "open_api", ">= 0.5.0"
25
25
  spec.add_dependency "active_model_serializers", "~> 0.10.0"
26
26
 
27
- rails_versions = ['>= 4.1', '< 6']
27
+ rails_versions = ['>= 5.0', '< 6.2']
28
28
  spec.add_dependency "actionpack", rails_versions
29
29
  spec.add_dependency "railties", rails_versions
30
30
 
31
- spec.add_development_dependency "bundler", "~> 1.16"
32
- spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 13.0"
33
33
  spec.add_development_dependency "rspec", "~> 3.0"
34
34
  spec.add_development_dependency "pry"
35
- spec.add_development_dependency "simplecov"
35
+ # Pin to 1.17 due to https://github.com/codeclimate/test-reporter/issues/413
36
+ spec.add_development_dependency "simplecov", "~> 0.17.0"
36
37
  spec.add_development_dependency "bump"
37
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_api_annotator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kent Nagata
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-01 00:00:00.000000000 Z
11
+ date: 2021-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: open_api
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.3.4
19
+ version: 0.5.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.3.4
26
+ version: 0.5.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: active_model_serializers
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,68 +44,68 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '4.1'
47
+ version: '5.0'
48
48
  - - "<"
49
49
  - !ruby/object:Gem::Version
50
- version: '6'
50
+ version: '6.2'
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ">="
56
56
  - !ruby/object:Gem::Version
57
- version: '4.1'
57
+ version: '5.0'
58
58
  - - "<"
59
59
  - !ruby/object:Gem::Version
60
- version: '6'
60
+ version: '6.2'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: railties
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - ">="
66
66
  - !ruby/object:Gem::Version
67
- version: '4.1'
67
+ version: '5.0'
68
68
  - - "<"
69
69
  - !ruby/object:Gem::Version
70
- version: '6'
70
+ version: '6.2'
71
71
  type: :runtime
72
72
  prerelease: false
73
73
  version_requirements: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: '4.1'
77
+ version: '5.0'
78
78
  - - "<"
79
79
  - !ruby/object:Gem::Version
80
- version: '6'
80
+ version: '6.2'
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: bundler
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
- version: '1.16'
87
+ version: '2.0'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
- version: '1.16'
94
+ version: '2.0'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: rake
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
99
  - - "~>"
100
100
  - !ruby/object:Gem::Version
101
- version: '10.0'
101
+ version: '13.0'
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - "~>"
107
107
  - !ruby/object:Gem::Version
108
- version: '10.0'
108
+ version: '13.0'
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: rspec
111
111
  requirement: !ruby/object:Gem::Requirement
@@ -138,16 +138,16 @@ dependencies:
138
138
  name: simplecov
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - ">="
141
+ - - "~>"
142
142
  - !ruby/object:Gem::Version
143
- version: '0'
143
+ version: 0.17.0
144
144
  type: :development
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - ">="
148
+ - - "~>"
149
149
  - !ruby/object:Gem::Version
150
- version: '0'
150
+ version: 0.17.0
151
151
  - !ruby/object:Gem::Dependency
152
152
  name: bump
153
153
  requirement: !ruby/object:Gem::Requirement
@@ -170,10 +170,10 @@ executables: []
170
170
  extensions: []
171
171
  extra_rdoc_files: []
172
172
  files:
173
+ - ".github/workflows/test.yml"
173
174
  - ".gitignore"
174
175
  - ".rspec"
175
176
  - ".ruby-version"
176
- - ".travis.yml"
177
177
  - CHANGELOG.md
178
178
  - CODE_OF_CONDUCT.md
179
179
  - Gemfile
@@ -182,6 +182,8 @@ files:
182
182
  - Rakefile
183
183
  - bin/console
184
184
  - bin/setup
185
+ - gemfiles/rails-5.2.gemfile
186
+ - gemfiles/rails-6.0.gemfile
185
187
  - lib/open_api_annotator.rb
186
188
  - lib/open_api_annotator/association.rb
187
189
  - lib/open_api_annotator/attribute.rb
@@ -205,7 +207,7 @@ homepage: https://github.com/ngtk/open_api_annotator
205
207
  licenses:
206
208
  - MIT
207
209
  metadata: {}
208
- post_install_message:
210
+ post_install_message:
209
211
  rdoc_options: []
210
212
  require_paths:
211
213
  - lib
@@ -220,8 +222,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
222
  - !ruby/object:Gem::Version
221
223
  version: '0'
222
224
  requirements: []
223
- rubygems_version: 3.0.1
224
- signing_key:
225
+ rubygems_version: 3.2.24
226
+ signing_key:
225
227
  specification_version: 4
226
228
  summary: OpenApi spec generation by bottom-up.
227
229
  test_files: []
data/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- env:
2
- global:
3
- - CC_TEST_REPORTER_ID=921d046db793707b7d0c0d1305970a137fdf98fbc6ceeb6c355c44757040244f
4
- sudo: false
5
- language: ruby
6
- rvm:
7
- - 2.4.4
8
- - 2.5.1
9
- before_install: gem install bundler -v 1.16.1
10
- before_script:
11
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
- - chmod +x ./cc-test-reporter
13
- - ./cc-test-reporter before-build
14
- after_script:
15
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT