rspec-openapi 0.3.18 → 0.4.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
2
  SHA256:
3
- metadata.gz: cd7d2eb0e69e2e60405a7d71d10acd3b2edf2d4db0358297a900200a0a69012e
4
- data.tar.gz: 577545384d404ba11382ab4425e0bd40abeeb3e58db922e82d923f0d6d6ae8e9
3
+ metadata.gz: 3032d3fed3b9bdc2b2a8512db3d1645679163539106650c1ad05069a15ab59cd
4
+ data.tar.gz: d1d3e85b467226a857b30bfe95b5643a1a37df2c0a925e509bdd893ae3017177
5
5
  SHA512:
6
- metadata.gz: 5f60ee2cf93946405e3e151786965b3a0f68e53ad147a6d7613f59f46f834efd9a641d0f878943af934879d2f5e27e8c8d4cd55dac5288d62a85dc2b77009ddb
7
- data.tar.gz: 7dc56150dfb4a8a5b526d7ec626b4ebc9c0680878cd1f976213490a489a598dfe2a285f01029e5a17cdbbe0d8b492fe79a2e51148ac24df94d532c404b0231fd
6
+ metadata.gz: 0c65c74db1ada24cf360545bda34830ca82d9cbd11b6e862a0563f9fc2bf244d65a96ec74e8223c182d1a2bff72b237e30c5884c2818576f9c142585d177351b
7
+ data.tar.gz: 397cfcddeaf3c1f683b9a1cd75960bf7d61e24adfcc9988bea2533c7a781acd42f43178b2ffb45deb5b3e821090f979d18af041a078f9ca3c2c7cd124cb4aff2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## v0.4.1
2
+
3
+ * Add `RSpec::OpenAPI.example_types` to hook types other than `type: :request`.
4
+ [#32](https://github.com/k0kubun/rspec-openapi/pull/32)
5
+
6
+ ## v0.4.0
7
+
8
+ * Drop `RSpec::OpenAPI.output` introduced in v0.3.20
9
+ * Guess whether it's JSON or not by the extension of `RSpec::OpenAPI.path`
10
+
11
+ ## v0.3.20
12
+
13
+ * Add `RSpec::OpenAPI.output` config to output JSON
14
+ [#31](https://github.com/k0kubun/rspec-openapi/pull/31)
15
+
16
+ ## v0.3.19
17
+
18
+ * Add `server_urls` and `request_headers` configs
19
+ [#30](https://github.com/k0kubun/rspec-openapi/pull/30)
20
+
1
21
  ## v0.3.18
2
22
 
3
23
  * Support nested query parameters
data/README.md CHANGED
@@ -108,12 +108,23 @@ The following configurations are optional.
108
108
  # Change the path to generate schema from `doc/openapi.yaml`
109
109
  RSpec::OpenAPI.path = 'doc/schema.yaml'
110
110
 
111
+ # Change the output type to JSON
112
+ RSpec::OpenAPI.path = 'doc/schema.json'
113
+
111
114
  # Disable generating `example`
112
115
  RSpec::OpenAPI.enable_example = false
113
116
 
114
117
  # Change `info.version`
115
118
  RSpec::OpenAPI.application_version = '1.0.0'
116
119
 
120
+ # Set `headers` - generate parameters with headers for a request
121
+ RSpec::OpenAPI.request_headers = %w[X-Authorization-Token]
122
+
123
+ # Set `server_urls` - generate servers of a schema file
124
+ RSpec::OpenAPI.server_urls = %w[
125
+ http://localhost:3000
126
+ ]
127
+
117
128
  # Generate a comment on top of a schema file
118
129
  RSpec::OpenAPI.comment = <<~EOS
119
130
  This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
@@ -124,6 +135,9 @@ EOS
124
135
 
125
136
  # Generate a custom description, given an RSpec example
126
137
  RSpec::OpenAPI.description_builder = -> (example) { example.description }
138
+
139
+ # Change the example type(s) that will generate schema
140
+ RSpec::OpenAPI.example_types = %i[request]
127
141
  ```
128
142
 
129
143
  ### How can I add information which can't be generated from RSpec?
@@ -155,10 +169,6 @@ Beta
155
169
 
156
170
  Basic features are there, and some people are already using this.
157
171
 
158
- ### Current limitation
159
-
160
- * Generating a JSON file is not supported yet
161
-
162
172
  ### Other missing features with notes
163
173
 
164
174
  * Delete obsoleted endpoints
@@ -6,6 +6,7 @@ class << RSpec::OpenAPI::DefaultSchema = Object.new
6
6
  title: title,
7
7
  version: RSpec::OpenAPI.application_version,
8
8
  },
9
+ servers: RSpec::OpenAPI.server_urls.map { |url| { url: url } } || [],
9
10
  paths: {},
10
11
  }.freeze
11
12
  end
@@ -9,7 +9,7 @@ records = []
9
9
  records_errors = []
10
10
 
11
11
  RSpec.configuration.after(:each) do |example|
12
- if example.metadata[:type] == :request && example.metadata[:openapi] != false
12
+ if RSpec::OpenAPI.example_types.include?(example.metadata[:type]) && example.metadata[:openapi] != false
13
13
  record = RSpec::OpenAPI::RecordBuilder.build(self, example: example)
14
14
  records << record if record
15
15
  end
@@ -31,7 +31,7 @@ RSpec.configuration.after(:suite) do
31
31
  if records_errors.any?
32
32
  error_message = <<~EOS
33
33
  RSpec::OpenAPI got errors building #{records_errors.size} requests
34
-
34
+
35
35
  #{records_errors.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
36
36
  EOS
37
37
  colorizer = ::RSpec::Core::Formatters::ConsoleCodes
@@ -5,6 +5,7 @@ RSpec::OpenAPI::Record = Struct.new(
5
5
  :query_params, # @param [Hash] - {:query=>"string"}
6
6
  :request_params, # @param [Hash] - {:request=>"body"}
7
7
  :request_content_type, # @param [String] - "application/json"
8
+ :request_headers, # @param [Array] - [["header_key1", "header_value1"], ["header_key2", "header_value2"]]
8
9
  :summary, # @param [String] - "v1/statuses #show"
9
10
  :tags, # @param [Array] - ["Status"]
10
11
  :description, # @param [String] - "returns a status"
@@ -34,6 +34,12 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
34
34
  nil
35
35
  end
36
36
 
37
+ request_headers = RSpec::OpenAPI.request_headers.each_with_object([]) do |header, headers_arr|
38
+ header_key = header.gsub(/-/, '_').upcase
39
+ header_value = request.get_header(['HTTP', header_key].join('_')) || request.get_header(header_key)
40
+ headers_arr << [header, header_value] if header_value
41
+ end
42
+
37
43
  RSpec::OpenAPI::Record.new(
38
44
  method: request.request_method,
39
45
  path: path,
@@ -41,6 +47,7 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
41
47
  query_params: request.query_parameters,
42
48
  request_params: raw_request_params(request),
43
49
  request_content_type: request.media_type,
50
+ request_headers: request_headers,
44
51
  summary: summary,
45
52
  tags: tags,
46
53
  description: RSpec::OpenAPI.description_builder.call(example),
@@ -67,6 +67,16 @@ class << RSpec::OpenAPI::SchemaBuilder = Object.new
67
67
  }.compact
68
68
  end
69
69
 
70
+ record.request_headers.each do |key, value|
71
+ parameters << {
72
+ name: build_parameter_name(key, value),
73
+ in: 'header',
74
+ required: true,
75
+ schema: build_property(try_cast(value)),
76
+ example: (try_cast(value) if example_enabled?),
77
+ }.compact
78
+ end
79
+
70
80
  return nil if parameters.empty?
71
81
  parameters
72
82
  end
@@ -1,5 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'yaml'
3
+ require 'json'
3
4
 
4
5
  # TODO: Support JSON
5
6
  class RSpec::OpenAPI::SchemaFile
@@ -20,13 +21,19 @@ class RSpec::OpenAPI::SchemaFile
20
21
  # @return [Hash]
21
22
  def read
22
23
  return {} unless File.exist?(@path)
23
- YAML.load(File.read(@path))
24
+ YAML.load(File.read(@path)) # this can also parse JSON
24
25
  end
25
26
 
26
27
  # @param [Hash] spec
27
28
  def write(spec)
28
29
  FileUtils.mkdir_p(File.dirname(@path))
29
- File.write(@path, prepend_comment(YAML.dump(spec)))
30
+ output =
31
+ if json?
32
+ JSON.pretty_generate(spec)
33
+ else
34
+ prepend_comment(YAML.dump(spec))
35
+ end
36
+ File.write(@path, output)
30
37
  end
31
38
 
32
39
  def prepend_comment(content)
@@ -38,4 +45,8 @@ class RSpec::OpenAPI::SchemaFile
38
45
  end
39
46
  "#{comment.gsub(/^/, '# ').gsub(/^# \n/, "#\n")}#{content}"
40
47
  end
48
+
49
+ def json?
50
+ File.extname(@path) == '.json'
51
+ end
41
52
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module OpenAPI
3
- VERSION = '0.3.18'
3
+ VERSION = '0.4.1'
4
4
  end
5
5
  end
data/lib/rspec/openapi.rb CHANGED
@@ -7,8 +7,11 @@ module RSpec::OpenAPI
7
7
  @enable_example = true
8
8
  @description_builder = -> (example) { example.description }
9
9
  @application_version = '1.0.0'
10
+ @request_headers = []
11
+ @server_urls = []
12
+ @example_types = %i[request]
10
13
 
11
14
  class << self
12
- attr_accessor :path, :comment, :enable_example, :description_builder, :application_version
15
+ attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :request_headers, :server_urls, :example_types
13
16
  end
14
17
  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.18
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-17 00:00:00.000000000 Z
11
+ date: 2021-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack