Skip to content

Commit b9514a4

Browse files
committed
Merge pull request #585 from wing328/ruby_fix_path_variable
Fixed Ruby path variable url-encoded twice, removed duplicated path variable replacement
2 parents 2bb42a1 + ed43a38 commit b9514a4

File tree

9 files changed

+34
-46
lines changed

9 files changed

+34
-46
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class {{classname}}
55
basePath = "{{basePath}}"
66
# apiInvoker = APIInvoker
77

8-
def self.escapeString(string)
9-
URI.encode(string.to_s)
10-
end
11-
128
{{#operation}}
139
{{newline}}
1410
# {{summary}}
@@ -34,7 +30,7 @@ class {{classname}}
3430
}.merge(opts)
3531

3632
#resource path
37-
path = "{{path}}".sub('{format}','json'){{#pathParams}}.sub('{' + '{{baseName}}' + '}', escapeString({{paramName}}))
33+
path = "{{path}}".sub('{format}','json'){{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s)
3834
{{/pathParams}}{{newline}}
3935

4036
# pull querystring keys from options

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ module Swagger
7474
def interpreted_path
7575
p = self.path.dup
7676

77-
# Fill in the path params
78-
self.params.each_pair do |key, value|
79-
p = p.gsub("{#{key}}", value.to_s)
80-
end
81-
8277
# Stick a .{format} placeholder into the path if there isn't
8378
# one already or an actual format like json or xml
8479
# e.g. /words/blah => /words.{format}/blah

samples/client/petstore/ruby/lib/pet_api.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ class PetApi
44
basePath = "http://petstore.swagger.io/v2"
55
# apiInvoker = APIInvoker
66

7-
def self.escapeString(string)
8-
URI.encode(string.to_s)
9-
end
10-
117

128
# Update an existing pet
139
#
@@ -266,7 +262,7 @@ def self.getPetById (pet_id, opts={})
266262
}.merge(opts)
267263

268264
#resource path
269-
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', escapeString(pet_id))
265+
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
270266

271267

272268
# pull querystring keys from options
@@ -321,7 +317,7 @@ def self.updatePetWithForm (pet_id, name, status, opts={})
321317
}.merge(opts)
322318

323319
#resource path
324-
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', escapeString(pet_id))
320+
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
325321

326322

327323
# pull querystring keys from options
@@ -375,7 +371,7 @@ def self.deletePet (api_key, pet_id, opts={})
375371
}.merge(opts)
376372

377373
#resource path
378-
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', escapeString(pet_id))
374+
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
379375

380376

381377
# pull querystring keys from options
@@ -429,7 +425,7 @@ def self.uploadFile (pet_id, additional_metadata, file, opts={})
429425
}.merge(opts)
430426

431427
#resource path
432-
path = "/pet/{petId}/uploadImage".sub('{format}','json').sub('{' + 'petId' + '}', escapeString(pet_id))
428+
path = "/pet/{petId}/uploadImage".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
433429

434430

435431
# pull querystring keys from options

samples/client/petstore/ruby/lib/store_api.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ class StoreApi
44
basePath = "http://petstore.swagger.io/v2"
55
# apiInvoker = APIInvoker
66

7-
def self.escapeString(string)
8-
URI.encode(string.to_s)
9-
end
10-
117

128
# Returns pet inventories by status
139
# Returns a map of status codes to quantities
@@ -145,7 +141,7 @@ def self.getOrderById (order_id, opts={})
145141
}.merge(opts)
146142

147143
#resource path
148-
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', escapeString(order_id))
144+
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', order_id.to_s)
149145

150146

151147
# pull querystring keys from options
@@ -196,7 +192,7 @@ def self.deleteOrder (order_id, opts={})
196192
}.merge(opts)
197193

198194
#resource path
199-
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', escapeString(order_id))
195+
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', order_id.to_s)
200196

201197

202198
# pull querystring keys from options

samples/client/petstore/ruby/lib/swagger/request.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ def url(options = {})
7474
def interpreted_path
7575
p = self.path.dup
7676

77-
# Fill in the path params
78-
self.params.each_pair do |key, value|
79-
p = p.gsub("{#{key}}", value.to_s)
80-
end
81-
8277
# Stick a .{format} placeholder into the path if there isn't
8378
# one already or an actual format like json or xml
8479
# e.g. /words/blah => /words.{format}/blah

samples/client/petstore/ruby/lib/user_api.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ class UserApi
44
basePath = "http://petstore.swagger.io/v2"
55
# apiInvoker = APIInvoker
66

7-
def self.escapeString(string)
8-
URI.encode(string.to_s)
9-
end
10-
117

128
# Create user
139
# This can only be done by the logged in user.
@@ -332,7 +328,7 @@ def self.getUserByName (username, opts={})
332328
}.merge(opts)
333329

334330
#resource path
335-
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', escapeString(username))
331+
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)
336332

337333

338334
# pull querystring keys from options
@@ -385,7 +381,7 @@ def self.updateUser (username, body, opts={})
385381
}.merge(opts)
386382

387383
#resource path
388-
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', escapeString(username))
384+
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)
389385

390386

391387
# pull querystring keys from options
@@ -455,7 +451,7 @@ def self.deleteUser (username, opts={})
455451
}.merge(opts)
456452

457453
#resource path
458-
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', escapeString(username))
454+
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)
459455

460456

461457
# pull querystring keys from options

samples/client/petstore/ruby/spec/pet_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
end
6666

6767
it "should fetch an order" do
68-
item = StoreApi.getOrderById(2)
69-
item.id.should == 2
68+
item = StoreApi.getOrderById(5)
69+
item.id.should == 5
7070
end
71-
end
71+
end

samples/client/petstore/ruby/spec/request_spec.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,18 @@
7373
describe "path" do
7474

7575
it "accounts for a total absence of format in the path string" do
76-
@request = Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
76+
@request = Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
7777
:format => "xml",
7878
:params => {
79-
:word => "cat"
8079
}
8180
}))
8281
@request.url.should == "http://petstore.swagger.io/v2/word.xml/cat/entries"
8382
end
8483

85-
it "does string substitution on path params" do
86-
@request = Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
84+
it "does string substitution (format) on path params" do
85+
@request = Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
8786
:format => "xml",
8887
:params => {
89-
:word => "cat"
9088
}
9189
}))
9290
@request.url.should == "http://petstore.swagger.io/v2/word.xml/cat/entries"
@@ -123,7 +121,7 @@
123121
end
124122

125123
it "URI encodes the path" do
126-
@request = Swagger::Request.new(:get, "word.{format}/{word}/definitions", @default_params.merge({
124+
@request = Swagger::Request.new(:get, "word.{format}/bill gates/definitions", @default_params.merge({
127125
:params => {
128126
:word => "bill gates"
129127
}
@@ -200,4 +198,4 @@
200198

201199
end
202200

203-
end
201+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
3+
describe "Store" do
4+
before do
5+
Swagger.configure do |config|
6+
config.api_key = 'special-key'
7+
config.host = 'petstore.swagger.io'
8+
config.base_path = '/v2'
9+
end
10+
end
11+
12+
it "should fetch an order" do
13+
item = StoreApi.getOrderById(5)
14+
item.id.should == 5
15+
end
16+
end

0 commit comments

Comments
 (0)