Skip to content

Commit 2d35743

Browse files
authored
Merge pull request #3368 from wing328/ruby_fix_rspec
[Ruby] fix default rspec test cases for Ruby API client
2 parents d3a1326 + 069a6b2 commit 2d35743

File tree

18 files changed

+167
-295
lines changed

18 files changed

+167
-295
lines changed

modules/swagger-codegen/src/main/resources/ruby/api_client_spec.mustache

Lines changed: 8 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,6 @@ describe {{moduleName}}::ApiClient do
4343
end
4444
end
4545

46-
describe "#update_params_for_auth!" do
47-
it "sets header api-key parameter with prefix" do
48-
{{moduleName}}.configure do |c|
49-
c.api_key_prefix['api_key'] = 'PREFIX'
50-
c.api_key['api_key'] = 'special-key'
51-
end
52-
53-
api_client = {{moduleName}}::ApiClient.new
54-
55-
config2 = {{moduleName}}::Configuration.new do |c|
56-
c.api_key_prefix['api_key'] = 'PREFIX2'
57-
c.api_key['api_key'] = 'special-key2'
58-
end
59-
api_client2 = {{moduleName}}::ApiClient.new(config2)
60-
61-
auth_names = ['api_key', 'unknown']
62-
63-
header_params = {}
64-
query_params = {}
65-
api_client.update_params_for_auth! header_params, query_params, auth_names
66-
expect(header_params).to eq({'api_key' => 'PREFIX special-key'})
67-
expect(query_params).to eq({})
68-
69-
header_params = {}
70-
query_params = {}
71-
api_client2.update_params_for_auth! header_params, query_params, auth_names
72-
expect(header_params).to eq({'api_key' => 'PREFIX2 special-key2'})
73-
expect(query_params).to eq({})
74-
end
75-
76-
it "sets header api-key parameter without prefix" do
77-
{{moduleName}}.configure do |c|
78-
c.api_key_prefix['api_key'] = nil
79-
c.api_key['api_key'] = 'special-key'
80-
end
81-
82-
api_client = {{moduleName}}::ApiClient.new
83-
84-
header_params = {}
85-
query_params = {}
86-
auth_names = ['api_key', 'unknown']
87-
api_client.update_params_for_auth! header_params, query_params, auth_names
88-
expect(header_params).to eq({'api_key' => 'special-key'})
89-
expect(query_params).to eq({})
90-
end
91-
end
92-
9346
describe "params_encoding in #build_request" do
9447
let(:config) { {{moduleName}}::Configuration.new }
9548
let(:api_client) { {{moduleName}}::ApiClient.new(config) }
@@ -155,49 +108,18 @@ describe {{moduleName}}::ApiClient do
155108
expect(data).to be_instance_of(Hash)
156109
expect(data).to eq({:message => 'Hello'})
157110
end
158-
159-
it "handles Hash<String, Pet>" do
160-
api_client = {{moduleName}}::ApiClient.new
161-
headers = {'Content-Type' => 'application/json'}
162-
response = double('response', headers: headers, body: '{"pet": {"id": 1}}')
163-
data = api_client.deserialize(response, 'Hash<String, Pet>')
164-
expect(data).to be_instance_of(Hash)
165-
expect(data.keys).to eq([:pet])
166-
167-
pet = data[:pet]
168-
expect(pet).to be_instance_of({{moduleName}}::Pet)
169-
expect(pet.id).to eq(1)
170-
end
171-
172-
it "handles Hash<String, Hash<String, Pet>>" do
173-
api_client = {{moduleName}}::ApiClient.new
174-
headers = {'Content-Type' => 'application/json'}
175-
response = double('response', headers: headers, body: '{"data": {"pet": {"id": 1}}}')
176-
result = api_client.deserialize(response, 'Hash<String, Hash<String, Pet>>')
177-
expect(result).to be_instance_of(Hash)
178-
expect(result.keys).to match_array([:data])
179-
180-
data = result[:data]
181-
expect(data).to be_instance_of(Hash)
182-
expect(data.keys).to match_array([:pet])
183-
184-
pet = data[:pet]
185-
expect(pet).to be_instance_of({{moduleName}}::Pet)
186-
expect(pet.id).to eq(1)
187-
end
188111
end
189112

190113
describe "#object_to_hash" do
191114
it "ignores nils and includes empty arrays" do
192-
api_client = {{moduleName}}::ApiClient.new
193-
pet = {{moduleName}}::Pet.new
194-
pet.id = 1
195-
pet.name = ''
196-
pet.status = nil
197-
pet.photo_urls = nil
198-
pet.tags = []
199-
expected = {id: 1, name: '', tags: []}
200-
expect(api_client.object_to_hash(pet)).to eq(expected)
115+
# uncomment below to test object_to_hash for model
116+
#api_client = {{moduleName}}::ApiClient.new
117+
#_model = {{moduleName}}::ModelName.new
118+
# update the model attribute below
119+
#_model.id = 1
120+
# update the expected value (hash) below
121+
#expected = {id: 1, name: '', tags: []}
122+
#expect(api_client.object_to_hash(_model)).to eq(expected)
201123
end
202124
end
203125

modules/swagger-codegen/src/main/resources/ruby/configuration_spec.mustache

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ describe {{moduleName}}::Configuration do
88
let(:config) { {{moduleName}}::Configuration.default }
99

1010
before(:each) do
11-
{{moduleName}}.configure do |c|
12-
c.host = 'petstore.swagger.io'
13-
c.base_path = 'v2'
14-
end
11+
# uncomment below to setup host and base_path
12+
#require 'URI'
13+
#uri = URI.parse("{{{basePath}}}")
14+
#{{moduleName}}.configure do |c|
15+
# c.host = uri.host
16+
# c.base_path = uri.path
17+
#end
1518
end
1619

1720
describe '#base_url' do
1821
it 'should have the default value' do
19-
expect(config.base_url).to eq('http://petstore.swagger.io/v2')
22+
# uncomment below to test default value of the base path
23+
#expect(config.base_url).to eq("{{{basePath}}}")
2024
end
2125

2226
it 'should remove trailing slashes' do
2327
[nil, '', '/', '//'].each do |base_path|
2428
config.base_path = base_path
25-
expect(config.base_url).to eq('http://petstore.swagger.io')
29+
# uncomment below to test trailing slashes
30+
#expect(config.base_url).to eq("{{{basePath}}}")
2631
end
2732
end
2833
end

samples/client/petstore-security-test/ruby/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# petstore
22

3-
Petstore - the Ruby gem for the Swagger Petstore */ &#39; \&quot;
3+
Petstore - the Ruby gem for the Swagger Petstore */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
44

5-
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \"
5+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \" =_end --
66

77
This SDK is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
88

9-
- API version: 1.0.0 */ &#39; \&quot;
9+
- API version: 1.0.0 */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
1010
- Package version: 1.0.0
11-
- Build date: 2016-06-28T17:34:19.923+08:00
11+
- Build date: 2016-07-14T18:21:54.437+08:00
1212
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
1313

1414
## Installation
@@ -58,25 +58,25 @@ require 'petstore'
5858
api_instance = Petstore::FakeApi.new
5959

6060
opts = {
61-
test_code_inject____end: "test_code_inject____end_example" # String | To test code injection */ ' \"
61+
test_code_inject____end____rn_n_r: "test_code_inject____end____rn_n_r_example" # String | To test code injection */ ' \" =_end -- \\r\\n \\n \\r
6262
}
6363

6464
begin
65-
#To test code injection */ ' \"
66-
api_instance.test_code_inject____end(opts)
65+
#To test code injection */ ' \" =_end -- \\r\\n \\n \\r
66+
api_instance.test_code_inject____end__rn_n_r(opts)
6767
rescue Petstore::ApiError => e
68-
puts "Exception when calling FakeApi->test_code_inject____end: #{e}"
68+
puts "Exception when calling FakeApi->test_code_inject____end__rn_n_r: #{e}"
6969
end
7070

7171
```
7272

7373
## Documentation for API Endpoints
7474

75-
All URIs are relative to *https://petstore.swagger.io */ &#39; &quot; &#x3D;end/v2 */ &#39; &quot; &#x3D;end*
75+
All URIs are relative to *https://petstore.swagger.io */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r/v2 */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r*
7676

7777
Class | Method | HTTP request | Description
7878
------------ | ------------- | ------------- | -------------
79-
*Petstore::FakeApi* | [**test_code_inject____end**](docs/FakeApi.md#test_code_inject____end) | **PUT** /fake | To test code injection */ ' \"
79+
*Petstore::FakeApi* | [**test_code_inject____end__rn_n_r**](docs/FakeApi.md#test_code_inject____end__rn_n_r) | **PUT** /fake | To test code injection */ ' \" =_end -- \\r\\n \\n \\r
8080

8181

8282
## Documentation for Models
@@ -90,7 +90,7 @@ Class | Method | HTTP request | Description
9090
### api_key
9191

9292
- **Type**: API key
93-
- **API key parameter name**: api_key */ &#39; &quot; &#x3D;end
93+
- **API key parameter name**: api_key */ &#39; &quot; &#x3D;end -- \r\n \n \r
9494
- **Location**: HTTP header
9595

9696
### petstore_auth
@@ -99,6 +99,6 @@ Class | Method | HTTP request | Description
9999
- **Flow**: implicit
100100
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
101101
- **Scopes**:
102-
- write:pets: modify pets in your account */ &#39; &quot; &#x3D;end
103-
- read:pets: read your pets */ &#39; &quot; &#x3D;end
102+
- write:pets: modify pets in your account */ &#39; &quot; &#x3D;end -- \r\n \n \r
103+
- read:pets: read your pets */ &#39; &quot; &#x3D;end -- \r\n \n \r
104104

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Petstore::FakeApi
22

3-
All URIs are relative to *https://petstore.swagger.io */ &#39; &quot; &#x3D;end/v2 */ &#39; &quot; &#x3D;end*
3+
All URIs are relative to *https://petstore.swagger.io */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r/v2 */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
7-
[**test_code_inject____end**](FakeApi.md#test_code_inject____end) | **PUT** /fake | To test code injection */ &#39; \&quot;
7+
[**test_code_inject____end__rn_n_r**](FakeApi.md#test_code_inject____end__rn_n_r) | **PUT** /fake | To test code injection */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
88

99

10-
# **test_code_inject____end**
11-
> test_code_inject____end(opts)
10+
# **test_code_inject____end__rn_n_r**
11+
> test_code_inject____end__rn_n_r(opts)
1212
13-
To test code injection */ ' \"
13+
To test code injection */ ' \" =_end -- \\r\\n \\n \\r
1414

1515
### Example
1616
```ruby
@@ -20,22 +20,22 @@ require 'petstore'
2020
api_instance = Petstore::FakeApi.new
2121

2222
opts = {
23-
test_code_inject____end: "test_code_inject____end_example" # String | To test code injection */ ' \"
23+
test_code_inject____end____rn_n_r: "test_code_inject____end____rn_n_r_example" # String | To test code injection */ ' \" =_end -- \\r\\n \\n \\r
2424
}
2525

2626
begin
27-
#To test code injection */ ' \"
28-
api_instance.test_code_inject____end(opts)
27+
#To test code injection */ ' \" =_end -- \\r\\n \\n \\r
28+
api_instance.test_code_inject____end__rn_n_r(opts)
2929
rescue Petstore::ApiError => e
30-
puts "Exception when calling FakeApi->test_code_inject____end: #{e}"
30+
puts "Exception when calling FakeApi->test_code_inject____end__rn_n_r: #{e}"
3131
end
3232
```
3333

3434
### Parameters
3535

3636
Name | Type | Description | Notes
3737
------------- | ------------- | ------------- | -------------
38-
**test_code_inject____end** | **String**| To test code injection */ &#39; \&quot; | [optional]
38+
**test_code_inject____end____rn_n_r** | **String**| To test code injection */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r | [optional]
3939

4040
### Return type
4141

@@ -47,8 +47,8 @@ No authorization required
4747

4848
### HTTP request headers
4949

50-
- **Content-Type**: application/json, */ " =end
51-
- **Accept**: application/json, */ " =end
50+
- **Content-Type**: application/json, */ \" =_end --
51+
- **Accept**: application/json, */ \" =_end --
5252

5353

5454

samples/client/petstore-security-test/ruby/docs/ModelReturn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**_return** | **Integer** | property description */ &#39; \&quot; | [optional]
6+
**_return** | **Integer** | property description */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r | [optional]
77

88

samples/client/petstore-security-test/ruby/lib/petstore.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=begin
2-
#Swagger Petstore */ ' \"
2+
#Swagger Petstore */ ' \" =_end -- \\r\\n \\n \\r
33
4-
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \"
4+
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \" =_end --
55
6-
OpenAPI spec version: 1.0.0 */ &#39; \&quot;
7-
Contact: [email protected] */ ' \"
6+
OpenAPI spec version: 1.0.0 */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
7+
Contact: [email protected] */ ' \" =_end -- \\r\\n \\n \\r
88
Generated by: https://github.com/swagger-api/swagger-codegen.git
99
1010
Licensed under the Apache License, Version 2.0 (the "License");

samples/client/petstore-security-test/ruby/lib/petstore/api/fake_api.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=begin
2-
#Swagger Petstore */ ' \"
2+
#Swagger Petstore */ ' \" =_end -- \\r\\n \\n \\r
33
4-
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \"
4+
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ */ ' \" =_end --
55
6-
OpenAPI spec version: 1.0.0 */ &#39; \&quot;
7-
Contact: [email protected] */ ' \"
6+
OpenAPI spec version: 1.0.0 */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
7+
Contact: [email protected] */ ' \" =_end -- \\r\\n \\n \\r
88
Generated by: https://github.com/swagger-api/swagger-codegen.git
99
1010
Licensed under the Apache License, Version 2.0 (the "License");
@@ -31,24 +31,24 @@ def initialize(api_client = ApiClient.default)
3131
@api_client = api_client
3232
end
3333

34-
# To test code injection */ ' \"
34+
# To test code injection */ ' \" =_end -- \\r\\n \\n \\r
3535
#
3636
# @param [Hash] opts the optional parameters
37-
# @option opts [String] :test_code_inject____end To test code injection */ &#39; \&quot;
37+
# @option opts [String] :test_code_inject____end____rn_n_r To test code injection */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
3838
# @return [nil]
39-
def test_code_inject____end(opts = {})
40-
test_code_inject____end_with_http_info(opts)
39+
def test_code_inject____end__rn_n_r(opts = {})
40+
test_code_inject____end__rn_n_r_with_http_info(opts)
4141
return nil
4242
end
4343

44-
# To test code injection */ &#39; \&quot;
44+
# To test code injection */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
4545
#
4646
# @param [Hash] opts the optional parameters
47-
# @option opts [String] :test_code_inject____end To test code injection */ &#39; \&quot;
47+
# @option opts [String] :test_code_inject____end____rn_n_r To test code injection */ &#39; \&quot; &#x3D;_end -- \\r\\n \\n \\r
4848
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
49-
def test_code_inject____end_with_http_info(opts = {})
49+
def test_code_inject____end__rn_n_r_with_http_info(opts = {})
5050
if @api_client.config.debugging
51-
@api_client.config.logger.debug "Calling API: FakeApi.test_code_inject____end ..."
51+
@api_client.config.logger.debug "Calling API: FakeApi.test_code_inject____end__rn_n_r ..."
5252
end
5353
# resource path
5454
local_var_path = "/fake".sub('{format}','json')
@@ -60,16 +60,16 @@ def test_code_inject____end_with_http_info(opts = {})
6060
header_params = {}
6161

6262
# HTTP header 'Accept' (if needed)
63-
local_header_accept = ['application/json', '*/ " =end']
63+
local_header_accept = ['application/json', '*/ \" =_end -- ']
6464
local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
6565

6666
# HTTP header 'Content-Type'
67-
local_header_content_type = ['application/json', '*/ " =end']
67+
local_header_content_type = ['application/json', '*/ \" =_end -- ']
6868
header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
6969

7070
# form parameters
7171
form_params = {}
72-
form_params["test code inject */ &#39; &quot; &#x3D;end"] = opts[:'test_code_inject____end'] if !opts[:'test_code_inject____end'].nil?
72+
form_params["test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r"] = opts[:'test_code_inject____end____rn_n_r'] if !opts[:'test_code_inject____end____rn_n_r'].nil?
7373

7474
# http body (model)
7575
post_body = nil
@@ -81,7 +81,7 @@ def test_code_inject____end_with_http_info(opts = {})
8181
:body => post_body,
8282
:auth_names => auth_names)
8383
if @api_client.config.debugging
84-
@api_client.config.logger.debug "API called: FakeApi#test_code_inject____end\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
84+
@api_client.config.logger.debug "API called: FakeApi#test_code_inject____end__rn_n_r\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
8585
end
8686
return data, status_code, headers
8787
end

0 commit comments

Comments
 (0)