Skip to content

Commit 878d993

Browse files
committed
F / Singluar example for MediaType
1 parent 53112f9 commit 878d993

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@
132132
### First of all, `include OpenApi::DSL` in your base class (which is for writing spec):
133133

134134
For example:
135-
```ruby
136-
# in app/controllers/api/api_controller.rb
137-
class ApiController < ActionController::API
138-
include OpenApi::DSL
139-
end
140-
```
135+
```ruby
136+
# in app/controllers/api/api_controller.rb
137+
class ApiController < ActionController::API
138+
include OpenApi::DSL
139+
end
140+
```
141141

142142
### DSL Usage Example
143143

@@ -452,10 +452,11 @@
452452

453453
```ruby
454454
# ** Method Signature
455-
response code, desc, media_type = nil, data: { }, **options
455+
response code, desc, media_type = nil, headers: { }, data: { }, **options
456456
# ** Usage
457457
resp 200, 'success', :json, data: { name: 'test' }
458458
response 200, 'query result', :pdf, data: File
459+
response :success, 'succ', :json, headers: { 'X-Request-Start': String }, data: { }
459460

460461
# ** Method Signature
461462
response_ref code_and_compkey_hash

lib/oas_objs/media_type_obj.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ module OpenApi
77
module DSL
88
# https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#media-type-object
99
class MediaTypeObj < Hash
10-
attr_accessor :media_type, :schema, :examples
10+
attr_accessor :media_type, :schema, :examples, :example
1111

12-
def initialize(media_type, hash)
13-
examples_hash = hash.delete(:examples)
14-
exp_params = schema_type.keys if (exp_params = hash.delete(:exp_params)) == :all
15-
self.examples = ExampleObj.new(examples_hash, exp_params, multiple: true) if examples_hash.present?
12+
def initialize(media_type, example: nil, examples: nil, exp_params: nil, **media_hash)
13+
schema_type = media_hash.values_at(:type, :data).compact.first
14+
exp_params = schema_type.keys if exp_params == :all
15+
self.examples = ExampleObj.new(examples, exp_params, multiple: true) if examples.present?
16+
self.example = ExampleObj.new(example) if example.present?
1617
self.media_type = media_type_mapping(media_type)
17-
self.schema = SchemaObj.new(hash.values_at(:type, :data).compact.first,
18-
hash.except(:type, :data))
18+
self.schema = SchemaObj.new(schema_type, media_hash.except(:type, :data))
1919
end
2020

2121
def process
2222
return { } if media_type.nil?
2323
schema_processed = schema.process
2424
result = schema_processed.values.join.blank? ? { } : { schema: schema_processed }
25+
result[:example] = example.process unless example.nil?
2526
result[:examples] = examples.process unless examples.nil?
2627
{ media_type => result }
2728
end

lib/open_api/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module OpenApi
4-
VERSION = '2.1.0'
4+
VERSION = '2.1.1'
55
end

spec/api_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@
194194
expect_its :headers, has_key: :'X-Request-Start'
195195
end
196196

197+
context 'when passing a example' do
198+
api -> do
199+
response :success, 'succ', :json, data: { name: String }, example: { name: 'Tom' }
200+
end, has_key!: :success
201+
focus_on :success
202+
expect_its :content, has_keys: { 'application/json': [ example: [:name] ] }
203+
end
204+
197205
context 'when re-calling through the same code and media-type' do
198206
api -> do
199207
response :success, 'success desc1', :json, data: { name: String }

0 commit comments

Comments
 (0)