rspec-openapi 0.3.0 → 0.3.5

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: 4bf20b571bfadeda67cae266f85c87530c32067d5b1d9934a16ea423652a17be
4
- data.tar.gz: ae4222d06d8d4aaee18ddcabc26808fe84cdfcf1ea6674527af7ffd7aa5e0d38
3
+ metadata.gz: d7134b7b05a91d447b6a52af56ee417e6f0c045d73526f171d9792d2301d40db
4
+ data.tar.gz: 8eeea5674f4e54c7ec53c1275f51b4dc47876438c64ee658db19cdbb6abc4489
5
5
  SHA512:
6
- metadata.gz: ef3047477727572e3c8bdab0680b5fe82264aa6653c33e5ea19c7680739db5b55bde2c51d11a3cce6c422d487f445705d549eca82068f474fcb750b6d21fdc59
7
- data.tar.gz: b3e03fedec66b64f24e83fe1023abaea2fb4accdb2e1da7dab78eb4870c0cc1338e9453e6c9cd345fe01e77258cbe39f01223b0ff4e708fd568c98dc5315b8d4
6
+ metadata.gz: 62b2b1e562954829ba607fa1f6713aa683fd508dde975198aa0c17c3dd7ad5db0e4d7e57aa80d6ca5a9d758a56d274128434eddc3f0dc9ac090117f80861bd7a
7
+ data.tar.gz: 354bb226fa980dac17d7a67d66404aad653c5fc4dd26b985fbd335152063b67716d2573e699835c5bce10d929fad08ea28a5f4545186b5b6ce17dbf13ffe01f0
@@ -1,6 +1,30 @@
1
+ ## v0.3.5
2
+
3
+ * Support finding routes in Rails engines
4
+
5
+ ## v0.3.4
6
+
7
+ * Generate tags by controller names
8
+ [#10](https://github.com/k0kubun/rspec-openapi/issues/10)
9
+
10
+ ## v0.3.3
11
+
12
+ * Avoid `JSON::ParserError` when a response body is no content
13
+ [#9](https://github.com/k0kubun/rspec-openapi/issues/9)
14
+
15
+ ## v0.3.2
16
+
17
+ * Stop generating format as path parameters in Rails
18
+ [#8](https://github.com/k0kubun/rspec-openapi/issues/8)
19
+
20
+ ## v0.3.1
21
+
22
+ * Add `RSpec::OpenAPI.description_builder` config to dynamically generate a description [experimental]
23
+ [#6](https://github.com/k0kubun/rspec-openapi/issues/6)
24
+
1
25
  ## v0.3.0
2
26
 
3
- * Initial support of rack-test and non-Rails apps
27
+ * Initial support of rack-test and non-Rails apps [#5](https://github.com/k0kubun/rspec-openapi/issues/5)
4
28
 
5
29
  ## v0.2.2
6
30
 
data/README.md CHANGED
@@ -65,15 +65,19 @@ paths:
65
65
  "/tables":
66
66
  get:
67
67
  summary: 'tables #index'
68
+ tags:
69
+ - tables
68
70
  parameters:
69
71
  - name: page
70
72
  in: query
71
73
  schema:
72
74
  type: integer
75
+ example: 1
73
76
  - name: per
74
77
  in: query
75
78
  schema:
76
79
  type: integer
80
+ example: 10
77
81
  responses:
78
82
  '200':
79
83
  description: returns a list of tables
@@ -114,6 +118,9 @@ RSpec::OpenAPI.comment = <<~EOS
114
118
  When you write a spec in spec/requests, running the spec with `OPENAPI=1 rspec` will
115
119
  update this file automatically. You can also manually edit this file.
116
120
  EOS
121
+
122
+ # Generate a custom description, given an RSpec example
123
+ RSpec::OpenAPI.description_builder = -> (example) { example.description }
117
124
  ```
118
125
 
119
126
  ### How can I add information which can't be generated from RSpec?
@@ -5,8 +5,9 @@ module RSpec::OpenAPI
5
5
  @path = 'doc/openapi.yaml'
6
6
  @comment = nil
7
7
  @enable_example = true
8
+ @description_builder = -> (example) { example.description }
8
9
 
9
10
  class << self
10
- attr_accessor :path, :comment, :enable_example
11
+ attr_accessor :path, :comment, :enable_example, :description_builder
11
12
  end
12
13
  end
@@ -6,6 +6,7 @@ RSpec::OpenAPI::Record = Struct.new(
6
6
  :request_params, # @param [Hash] - {:request=>"body"}
7
7
  :request_content_type, # @param [String] - "application/json"
8
8
  :summary, # @param [String] - "v1/statuses #show"
9
+ :tags, # @param [Array] - ["Status"]
9
10
  :description, # @param [String] - "returns a status"
10
11
  :status, # @param [Integer] - 200
11
12
  :response_body, # @param [Object] - {"status" => "ok"}
@@ -15,44 +15,74 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
15
15
  end
16
16
 
17
17
  # Generate `path` and `summary` in a framework-friendly manner when possible
18
- if defined?(Rails) && Rails.application
18
+ if rails?
19
19
  route = find_rails_route(request)
20
20
  path = route.path.spec.to_s.delete_suffix('(.:format)')
21
21
  summary = "#{route.requirements[:controller]} ##{route.requirements[:action]}"
22
+ tags = [route.requirements[:controller]]
22
23
  else
23
24
  path = request.path
24
25
  summary = "#{request.method} #{request.path}"
25
26
  end
26
27
 
28
+ response_body =
29
+ begin
30
+ response.parsed_body
31
+ rescue JSON::ParserError
32
+ nil
33
+ end
34
+
27
35
  RSpec::OpenAPI::Record.new(
28
36
  method: request.request_method,
29
37
  path: path,
30
- path_params: request.path_parameters,
38
+ path_params: raw_path_params(request),
31
39
  query_params: request.query_parameters,
32
40
  request_params: raw_request_params(request),
33
41
  request_content_type: request.content_type,
34
42
  summary: summary,
35
- description: example.description,
43
+ tags: tags,
44
+ description: RSpec::OpenAPI.description_builder.call(example),
36
45
  status: response.status,
37
- response_body: response.parsed_body,
46
+ response_body: response_body,
38
47
  response_content_type: response.content_type,
39
48
  ).freeze
40
49
  end
41
50
 
42
51
  private
43
52
 
53
+ def rails?
54
+ defined?(Rails) && Rails.application
55
+ end
56
+
44
57
  def rack_test?(context)
45
58
  defined?(Rack::Test::Methods) && context.class < Rack::Test::Methods
46
59
  end
47
60
 
48
61
  # @param [ActionDispatch::Request] request
49
62
  def find_rails_route(request)
63
+ # Reverse the destructive modification by Rails https://github.com/rails/rails/blob/v5.2.4.4/actionpack/lib/action_dispatch/journey/router.rb#L36-L44
64
+ unless request.script_name.empty?
65
+ request = request.dup
66
+ request.path_info = File.join(request.script_name, request.path_info)
67
+ end
68
+
50
69
  Rails.application.routes.router.recognize(request) do |route|
51
70
  return route
52
71
  end
53
72
  raise "No route matched for #{request.request_method} #{request.path_info}"
54
73
  end
55
74
 
75
+ # :controller and :action always exist. :format is added when routes is configured as such.
76
+ def raw_path_params(request)
77
+ if rails?
78
+ request.path_parameters.reject do |key, _value|
79
+ %i[controller action format].include?(key)
80
+ end
81
+ else
82
+ request.path_parameters
83
+ end
84
+ end
85
+
56
86
  # workaround to get real request parameters
57
87
  # because ActionController::ParamsWrapper overwrites request_parameters
58
88
  def raw_request_params(request)
@@ -2,23 +2,29 @@ class << RSpec::OpenAPI::SchemaBuilder = Object.new
2
2
  # @param [RSpec::OpenAPI::Record] record
3
3
  # @return [Hash]
4
4
  def build(record)
5
+ response = {
6
+ description: record.description,
7
+ }
8
+
9
+ if record.response_body
10
+ response[:content] = {
11
+ normalize_content_type(record.response_content_type) => {
12
+ schema: build_property(record.response_body),
13
+ example: (record.response_body if example_enabled?),
14
+ }.compact,
15
+ }
16
+ end
17
+
5
18
  {
6
19
  paths: {
7
20
  normalize_path(record.path) => {
8
21
  record.method.downcase => {
9
22
  summary: record.summary,
23
+ tags: record.tags,
10
24
  parameters: build_parameters(record),
11
25
  requestBody: build_request_body(record),
12
26
  responses: {
13
- record.status.to_s => {
14
- description: record.description,
15
- content: {
16
- normalize_content_type(record.response_content_type) => {
17
- schema: build_property(record.response_body),
18
- example: (record.response_body if example_enabled?),
19
- }.compact,
20
- },
21
- },
27
+ record.status.to_s => response,
22
28
  },
23
29
  }.compact,
24
30
  },
@@ -36,7 +42,6 @@ class << RSpec::OpenAPI::SchemaBuilder = Object.new
36
42
  parameters = []
37
43
 
38
44
  record.path_params.each do |key, value|
39
- next if %i[controller action].include?(key)
40
45
  parameters << {
41
46
  name: key.to_s,
42
47
  in: 'path',
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module OpenAPI
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-10 00:00:00.000000000 Z
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -73,7 +73,7 @@ metadata:
73
73
  homepage_uri: https://github.com/k0kubun/rspec-openapi
74
74
  source_code_uri: https://github.com/k0kubun/rspec-openapi
75
75
  changelog_uri: https://github.com/k0kubun/rspec-openapi/blob/master/CHANGELOG.md
76
- post_install_message:
76
+ post_install_message:
77
77
  rdoc_options: []
78
78
  require_paths:
79
79
  - lib
@@ -88,8 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.2.0.pre1
92
- signing_key:
91
+ rubygems_version: 3.1.2
92
+ signing_key:
93
93
  specification_version: 4
94
94
  summary: Generate OpenAPI schema from RSpec request specs
95
95
  test_files: []