rswag-specs 2.5.1 → 2.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: df4a6de7cd0c39993e11e3cee2e4f21a1bf7175d53d467530a2d5d2fb0de62ad
4
- data.tar.gz: 5845e6e3cc9368ff18a777155bb039aae5f8ad526444b9bf00ef5c3635ad732a
3
+ metadata.gz: 335cddedc6446301252f26bb28a0874072ebecfb5c8b35e5a2d94559a6606fcc
4
+ data.tar.gz: 3a2e60364a2a6e6ee463163eee2caa628ecf28c3ba9975ea239aa1106d4cd179
5
5
  SHA512:
6
- metadata.gz: '088948aa3b15b493d95d678e6d30d211bc3d942d27db9f6c340acdee76b43783352b830045d118b1d91f65be068248c4a829d5f0a06fe58cd4478b903b069a25'
7
- data.tar.gz: 60175ad736c79c12cfc14e1a9e810b57e0676a52ddc5b35fdb084fc6b0b8f0b1097080d6a367535287b982a71241b6675f6484252fce4a2b351972dead2664d5
6
+ metadata.gz: 436935923dcf3a446290c2bdf0160b399c5ba270096dc4019b1f47e2308f4e9ca66af8684fa09c6fc63f5b18069cf49b24ae124f19756480f5cff998873c26b5
7
+ data.tar.gz: 0c123c3e98481381a606a68c59b9cc5f0b8626a70f81c76c3d825bc3a6a46874e9524155da085baaf66abb2872c23f76a0f0931b390d669feeaccf1ba4da875d
@@ -69,13 +69,34 @@ module Rswag
69
69
  # NOTE: Similar to 'description', 'examples' need to handle the case when
70
70
  # being invoked with no params to avoid overriding 'examples' method of
71
71
  # rspec-core ExampleGroup
72
- def examples(example = nil)
73
- return super() if example.nil?
72
+ def examples(examples = nil)
73
+ return super() if examples.nil?
74
+ # should we add a deprecation warning?
75
+ examples.each_with_index do |(mime, example_object), index|
76
+ example(mime, "example_#{index}", example_object)
77
+ end
78
+ end
74
79
 
75
- metadata[:response][:content] =
76
- example.each_with_object({}) do |(mime, example_object), memo|
77
- memo[mime] = { example: example_object }
78
- end
80
+ def example(mime, name, value, summary=nil, description=nil)
81
+ # Todo - move initialization of metadata somewhere else.
82
+ if metadata[:response][:content].blank?
83
+ metadata[:response][:content] = {}
84
+ end
85
+
86
+ if metadata[:response][:content][mime].blank?
87
+ metadata[:response][:content][mime] = {}
88
+ metadata[:response][:content][mime][:examples] = {}
89
+ end
90
+
91
+ example_object = {
92
+ value: value,
93
+ summary: summary,
94
+ description: description
95
+ }.select { |_, v| v.present? }
96
+ # TODO, issue a warning if example is being overridden with the same key
97
+ metadata[:response][:content][mime][:examples].merge!(
98
+ { name.to_sym => example_object }
99
+ )
79
100
  end
80
101
 
81
102
  def run_test!(&block)
@@ -85,7 +106,7 @@ module Rswag
85
106
  submit_request(example.metadata)
86
107
  end
87
108
 
88
- it "returns a #{metadata[:response][:code]} response" do
109
+ it "returns a #{metadata[:response][:code]} response", rswag: true do
89
110
  assert_response_matches_metadata(metadata)
90
111
  block.call(response) if block_given?
91
112
  end
@@ -94,7 +115,7 @@ module Rswag
94
115
  submit_request(example.metadata)
95
116
  end
96
117
 
97
- it "returns a #{metadata[:response][:code]} response" do |example|
118
+ it "returns a #{metadata[:response][:code]} response", rswag: true do |example|
98
119
  assert_response_matches_metadata(example.metadata, &block)
99
120
  example.instance_exec(response, &block) if block_given?
100
121
  end
@@ -7,14 +7,11 @@ module Rswag
7
7
  class ExtendedSchema < JSON::Schema::Draft4
8
8
  def initialize
9
9
  super
10
- @attributes['type'] = ExtendedTypeAttribute
11
10
  @uri = URI.parse('http://tempuri.org/rswag/specs/extended_schema')
12
11
  @names = ['http://tempuri.org/rswag/specs/extended_schema']
13
12
  end
14
- end
15
13
 
16
- class ExtendedTypeAttribute < JSON::Schema::TypeV4Attribute
17
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
14
+ def validate(current_schema, data, *)
18
15
  return if data.nil? && (current_schema.schema['nullable'] == true || current_schema.schema['x-nullable'] == true)
19
16
 
20
17
  super
@@ -106,18 +106,57 @@ module Rswag
106
106
 
107
107
  request[:path] = template.tap do |path_template|
108
108
  parameters.select { |p| p[:in] == :path }.each do |p|
109
+ unless example.respond_to?(p[:name])
110
+ raise ArgumentError.new("`#{p[:name].to_s}` parameter key present, but not defined within example group"\
111
+ "(i. e `it` or `let` block)")
112
+ end
109
113
  path_template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s)
110
114
  end
111
115
 
112
116
  parameters.select { |p| p[:in] == :query }.each_with_index do |p, i|
113
117
  path_template.concat(i.zero? ? '?' : '&')
114
- path_template.concat(build_query_string_part(p, example.send(p[:name])))
118
+ path_template.concat(build_query_string_part(p, example.send(p[:name]), swagger_doc))
115
119
  end
116
120
  end
117
121
  end
118
122
 
119
- def build_query_string_part(param, value)
123
+ def build_query_string_part(param, value, swagger_doc)
120
124
  name = param[:name]
125
+
126
+ # OAS 3: https://swagger.io/docs/specification/serialization/
127
+ if swagger_doc && doc_version(swagger_doc).start_with?('3') && param[:schema]
128
+ style = param[:style]&.to_sym || :form
129
+ explode = param[:explode].nil? ? true : param[:explode]
130
+
131
+ case param[:schema][:type].to_sym
132
+ when :object
133
+ case style
134
+ when :deepObject
135
+ return { name => value }.to_query
136
+ when :form
137
+ if explode
138
+ return value.to_query
139
+ else
140
+ return "#{CGI.escape(name.to_s)}=" + value.to_a.flatten.map{|v| CGI.escape(v.to_s) }.join(',')
141
+ end
142
+ end
143
+ when :array
144
+ case explode
145
+ when true
146
+ return value.to_a.flatten.map{|v| "#{CGI.escape(name.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
147
+ else
148
+ separator = case style
149
+ when :form then ','
150
+ when :spaceDelimited then '%20'
151
+ when :pipeDelimited then '|'
152
+ end
153
+ return "#{CGI.escape(name.to_s)}=" + value.to_a.flatten.map{|v| CGI.escape(v.to_s) }.join(separator)
154
+ end
155
+ else
156
+ return "#{name}=#{value}"
157
+ end
158
+ end
159
+
121
160
  type = param[:type] || param.dig(:schema, :type)
122
161
  return "#{name}=#{value}" unless type&.to_sym == :array
123
162
 
@@ -154,8 +193,8 @@ module Rswag
154
193
  tuples << ['Content-Type', content_type]
155
194
  end
156
195
 
157
- # Rails test infrastructure requires rackified headers
158
- rackified_tuples = tuples.map do |pair|
196
+ # Rails test infrastructure requires rack-formatted headers
197
+ rack_formatted_tuples = tuples.map do |pair|
159
198
  [
160
199
  case pair[0]
161
200
  when 'Accept' then 'HTTP_ACCEPT'
@@ -167,7 +206,7 @@ module Rswag
167
206
  ]
168
207
  end
169
208
 
170
- request[:headers] = Hash[rackified_tuples]
209
+ request[:headers] = Hash[rack_formatted_tuples]
171
210
  end
172
211
 
173
212
  def add_payload(request, parameters, example)
@@ -53,7 +53,7 @@ module Rswag
53
53
  return unless errors.any?
54
54
 
55
55
  raise UnexpectedResponse,
56
- "Expected response body to match schema: #{errors[0]}\n" \
56
+ "Expected response body to match schema: #{errors.join("\n")}\n" \
57
57
  "Response body: #{JSON.pretty_generate(JSON.parse(body))}"
58
58
  end
59
59
 
@@ -60,6 +60,7 @@ module Rswag
60
60
  if value && schema_param && mime_list
61
61
  value[:requestBody] = { content: {} } unless value.dig(:requestBody, :content)
62
62
  value[:requestBody][:required] = true if schema_param[:required]
63
+ value[:requestBody][:description] = schema_param[:description] if schema_param[:description]
63
64
  mime_list.each do |mime|
64
65
  value[:requestBody][:content][mime] = { schema: schema_param[:schema] }
65
66
  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.5.1
4
+ version: 2.6.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-02-10 00:00:00.000000000 Z
13
+ date: 2022-09-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -67,8 +67,8 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.2'
69
69
  description: 'Simplify API integration testing with a succinct rspec DSL and generate
70
- OpenAPI specification files directly from your rspecs. More about the OpenAPI initiative
71
- here: http://spec.openapis.org/'
70
+ OpenAPI specification files directly from your rspec tests. More about the OpenAPI
71
+ initiative here: http://spec.openapis.org/'
72
72
  email:
73
73
  - domaindrivendev@gmail.com
74
74
  executables: []