rswag-specs 2.7.0 → 2.8.0

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: 989468cbcebd005e6bf95724400366cfa1b80e7d395bb6d1255bc12c283a98b2
4
- data.tar.gz: 949a822da86b5d6f3ead28cdd1262a093bcd8399875b1962f101c41634cc4c51
3
+ metadata.gz: cfedb2177ee21788aa710db72fbc573230a51c84877bd90edff99b8a1fc9967c
4
+ data.tar.gz: 842d0dba7c2dcd4aa59ec015928c825da6ef1cf892859e492589d540b003c181
5
5
  SHA512:
6
- metadata.gz: b4cb982b39359a8f69dd5ebf18287a2b4941349a202751d10f936b81014fce8ec99a24886e5de63c4fb1d4aba653893cf4ebff9f425f547be99b1ac3910e53ad
7
- data.tar.gz: 1064a439e5b8652c40a77eddd6d0837ea908780750187879bb8bdb11af67656ea6882a8a251bb8627001c7857ebad94c378d68432c1101e2e19647b5c7c29ae5
6
+ metadata.gz: 5640b7930547aa45a3b2662f8fad08508063d1c9726c2f72d6e2f15f6eab32408472e7645230b30a5eec74ae223cfae5e87e9d26311cc609aa543c90d8234aef
7
+ data.tar.gz: 98e797ce8b1babf942ba387cffbe6eec52994047543c669b9293631bc08bfc1695b9d3c1ca572f3ec9357e7649bf601f0df81bf28590ee605b501f7546aae8f1
@@ -55,6 +55,18 @@ module Rswag
55
55
  end
56
56
  end
57
57
 
58
+
59
+ def request_body_example(value:, summary: nil, name: nil)
60
+ if metadata.key?(:operation)
61
+ metadata[:operation][:request_examples] ||= []
62
+ example = { value: value }
63
+ example[:summary] = summary if summary
64
+ # We need the examples to have a unique name for a set of examples, so just make the name the length if one isn't provided.
65
+ example[:name] = name || metadata[:operation][:request_examples].length()
66
+ metadata[:operation][:request_examples] << example
67
+ end
68
+ end
69
+
58
70
  def response(code, description, metadata = {}, &block)
59
71
  metadata[:response] = { code: code, description: description }
60
72
  context(description, metadata, &block)
@@ -148,7 +148,7 @@ module Rswag
148
148
  style = param[:style]&.to_sym || :form
149
149
  explode = param[:explode].nil? ? true : param[:explode]
150
150
 
151
- case param[:schema][:type].to_sym
151
+ case param[:schema][:type]&.to_sym
152
152
  when :object
153
153
  case style
154
154
  when :deepObject
@@ -213,14 +213,22 @@ module Rswag
213
213
  tuples << ['Content-Type', content_type]
214
214
  end
215
215
 
216
+ # Host header
217
+ host = metadata[:operation][:host] || swagger_doc[:host]
218
+ if host.present?
219
+ host = example.respond_to?(:'Host') ? example.send(:'Host') : host
220
+ tuples << ['Host', host]
221
+ end
222
+
216
223
  # Rails test infrastructure requires rack-formatted headers
217
224
  rack_formatted_tuples = tuples.map do |pair|
218
225
  [
219
226
  case pair[0]
220
- when 'Accept' then 'HTTP_ACCEPT'
221
- when 'Content-Type' then 'CONTENT_TYPE'
222
- when 'Authorization' then 'HTTP_AUTHORIZATION'
223
- else pair[0]
227
+ when 'Accept' then 'HTTP_ACCEPT'
228
+ when 'Content-Type' then 'CONTENT_TYPE'
229
+ when 'Authorization' then 'HTTP_AUTHORIZATION'
230
+ when 'Host' then 'HTTP_HOST'
231
+ else pair[0]
224
232
  end,
225
233
  pair[1]
226
234
  ]
@@ -32,9 +32,22 @@ module Rswag
32
32
  end
33
33
 
34
34
  def validate_headers!(metadata, headers)
35
- expected = (metadata[:response][:headers] || {}).keys
35
+ header_schemas = (metadata[:response][:headers] || {})
36
+ expected = header_schemas.keys
36
37
  expected.each do |name|
37
- raise UnexpectedResponse, "Expected response header #{name} to be present" if headers[name.to_s].nil?
38
+ nullable_attribute = header_schemas.dig(name.to_s, :schema, :nullable)
39
+ required_attribute = header_schemas.dig(name.to_s, :required)
40
+
41
+ is_nullable = nullable_attribute.nil? ? false : nullable_attribute
42
+ is_required = required_attribute.nil? ? true : required_attribute
43
+
44
+ if headers.exclude?(name.to_s) && is_required
45
+ raise UnexpectedResponse, "Expected response header #{name} to be present"
46
+ end
47
+
48
+ if headers.include?(name.to_s) && headers[name.to_s].nil? && !is_nullable
49
+ raise UnexpectedResponse, "Expected response header #{name} to not be null"
50
+ end
38
51
  end
39
52
  end
40
53
 
@@ -62,8 +62,18 @@ module Rswag
62
62
  value[:requestBody] = { content: {} } unless value.dig(:requestBody, :content)
63
63
  value[:requestBody][:required] = true if schema_param[:required]
64
64
  value[:requestBody][:description] = schema_param[:description] if schema_param[:description]
65
+ examples = value.dig(:request_examples)
65
66
  mime_list.each do |mime|
66
67
  value[:requestBody][:content][mime] = { schema: schema_param[:schema] }
68
+ if examples
69
+ value[:requestBody][:content][mime][:examples] ||= {}
70
+ examples.map do |example|
71
+ value[:requestBody][:content][mime][:examples][example[:name]] = {
72
+ summary: example[:summary] || value[:summary],
73
+ value: example[:value]
74
+ }
75
+ end
76
+ end
67
77
  end
68
78
  end
69
79
 
@@ -200,6 +210,7 @@ module Rswag
200
210
  is_hash = value.is_a?(Hash)
201
211
  value.delete(:consumes) if is_hash && value[:consumes]
202
212
  value.delete(:produces) if is_hash && value[:produces]
213
+ value.delete(:request_examples) if is_hash && value[:request_examples]
203
214
  end
204
215
  end
205
216
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rswag-specs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richie Morris
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-10-19 00:00:00.000000000 Z
13
+ date: 2022-11-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport