rspec-openapi 0.3.1 → 0.3.6

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: 98be98f98f98819f1d1b018ad1a155184ebbe1c226b96414895acc0f60106b18
4
- data.tar.gz: 6985138afe53b9223647f67d8802acdab580d737c18caec6b312d625d649d7c7
3
+ metadata.gz: 3c2228341b1c207993fd9f98f74069e06a14fde9107d2fce34d4d6ae6d2c2184
4
+ data.tar.gz: 23b931010297c4618edac4ce3b51e0d78a9f052891c1c9384844742846988424
5
5
  SHA512:
6
- metadata.gz: 13906efcf4d94054aea6c6132c09990bc99f264fc3fc37842f6d4d98515a5974f91a39cc3c7d749f0b2f24107e7fc9fb6bb94cfa6f3655fa0f13429101d17a20
7
- data.tar.gz: 10ca07bb2370cf61dee4bdbc0d19d1194d64daf558813d8fff113868226a5d1ff6036f71071f58106492592e012ab999dc430976e1e5b7a4361dc36ec1bc5fb8
6
+ metadata.gz: fc620c878f8bc514b6f802c324984d16029632f819d2b92815f3a5c9122e93b982658c28ba1a6bf20b4c0021c0570d634a807f9d1e90cd6782f1dc0805aa3762
7
+ data.tar.gz: 2cbc49093a983dcd0c1efdf2a41785ec202b6cc582ba72a7fe2f2a4339dbfbcffcab56bf13d2899fa7e7ef067fd794362ae0ffecd1aa52980e8ecc56fa667142
@@ -1,4 +1,27 @@
1
- ## Unreleased
1
+ ## v0.3.6
2
+
3
+ * Fix documents generated by Rails engines
4
+
5
+ ## v0.3.5
6
+
7
+ * Support finding routes in Rails engines
8
+
9
+ ## v0.3.4
10
+
11
+ * Generate tags by controller names
12
+ [#10](https://github.com/k0kubun/rspec-openapi/issues/10)
13
+
14
+ ## v0.3.3
15
+
16
+ * Avoid `JSON::ParserError` when a response body is no content
17
+ [#9](https://github.com/k0kubun/rspec-openapi/issues/9)
18
+
19
+ ## v0.3.2
20
+
21
+ * Stop generating format as path parameters in Rails
22
+ [#8](https://github.com/k0kubun/rspec-openapi/issues/8)
23
+
24
+ ## v0.3.1
2
25
 
3
26
  * Add `RSpec::OpenAPI.description_builder` config to dynamically generate a description [experimental]
4
27
  [#6](https://github.com/k0kubun/rspec-openapi/issues/6)
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
@@ -115,7 +119,7 @@ RSpec::OpenAPI.comment = <<~EOS
115
119
  update this file automatically. You can also manually edit this file.
116
120
  EOS
117
121
 
118
- # Generate a custom description, given a RSpec example
122
+ # Generate a custom description, given an RSpec example
119
123
  RSpec::OpenAPI.description_builder = -> (example) { example.description }
120
124
  ```
121
125
 
@@ -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,77 @@ 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,
43
+ tags: tags,
35
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
- def find_rails_route(request)
50
- Rails.application.routes.router.recognize(request) do |route|
62
+ def find_rails_route(request, app: Rails.application, fix_path: true)
63
+ # Reverse the destructive modification by Rails https://github.com/rails/rails/blob/v6.0.3.4/actionpack/lib/action_dispatch/journey/router.rb#L33-L41
64
+ if fix_path && !request.script_name.empty?
65
+ request = request.dup
66
+ request.path_info = File.join(request.script_name, request.path_info)
67
+ end
68
+
69
+ app.routes.router.recognize(request) do |route|
70
+ unless route.path.anchored
71
+ route = find_rails_route(request, app: route.app.app, fix_path: false)
72
+ end
51
73
  return route
52
74
  end
53
75
  raise "No route matched for #{request.request_method} #{request.path_info}"
54
76
  end
55
77
 
78
+ # :controller and :action always exist. :format is added when routes is configured as such.
79
+ def raw_path_params(request)
80
+ if rails?
81
+ request.path_parameters.reject do |key, _value|
82
+ %i[controller action format].include?(key)
83
+ end
84
+ else
85
+ request.path_parameters
86
+ end
87
+ end
88
+
56
89
  # workaround to get real request parameters
57
90
  # because ActionController::ParamsWrapper overwrites request_parameters
58
91
  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.1'
3
+ VERSION = '0.3.6'
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.1
4
+ version: 0.3.6
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-13 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: []