Skip to content

Commit c938a87

Browse files
YusukeHosonumaFornori
authored andcommitted
[crystal][client] Support authorization (OpenAPITools#9488)
* [crystal][client] support authorization header * [crystal][client] update samples * [crystal][client] update tests * [crystal][client] add unit test for api_client.cr
1 parent 9db6516 commit c938a87

File tree

10 files changed

+112
-43
lines changed

10 files changed

+112
-43
lines changed

modules/openapi-generator/src/main/resources/crystal/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ module {{moduleName}}
128128
local_var_path = "{{{path}}}"{{#pathParams}}.sub("{" + "{{baseName}}" + "}", URI.encode({{paramName}}.to_s){{^strictSpecBehavior}}.gsub("%2F", "/"){{/strictSpecBehavior}}){{/pathParams}}
129129

130130
# query parameters
131-
query_params = Hash(Symbol, String).new
131+
query_params = Hash(String, String).new
132132
{{#queryParams}}
133-
query_params[:"{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
133+
query_params["{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
134134
{{/queryParams}}
135135

136136
# header parameters

modules/openapi-generator/src/main/resources/crystal/api_client.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module {{moduleName}}
163163
# @param [Hash] query_params Query parameters
164164
# @param [String] auth_names Authentication scheme name
165165
def update_params_for_auth!(header_params, query_params, auth_names)
166-
Array{auth_names}.each do |auth_name|
166+
auth_names.each do |auth_name|
167167
auth_setting = @config.auth_settings[auth_name]
168168
next unless auth_setting
169169
case auth_setting[:in]
@@ -256,7 +256,7 @@ module {{moduleName}}
256256
#
257257
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
258258
# the data deserialized from response body (could be nil), response status code and response headers.
259-
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of Symbol => String, form_params = {} of Symbol => String)
259+
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of String => String, form_params = {} of Symbol => String)
260260
#ssl_options = {
261261
# :ca_file => @config.ssl_ca_file,
262262
# :verify => @config.ssl_verify,
@@ -274,6 +274,8 @@ module {{moduleName}}
274274
# conn.adapter(Faraday.default_adapter)
275275
#end
276276

277+
update_params_for_auth! header_params, query_params, auth_names
278+
277279
if !post_body.nil? && !post_body.empty?
278280
# use JSON string in the payload
279281
form_or_body = post_body

modules/openapi-generator/src/main/resources/crystal/configuration.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ module {{moduleName}}
3030
# @return [Hash] key: parameter name, value: parameter value (API key)
3131
#
3232
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
33-
# config.api_key[:"api_key"] = "xxx"
33+
# config.api_key[:api_key] = "xxx"
3434
property api_key : Hash(Symbol, String)
3535

3636
# Defines API key prefixes used with API Key authentications.
3737
#
3838
# @return [Hash] key: parameter name, value: API key prefix
3939
#
4040
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
41-
# config.api_key_prefix[:"api_key"] = "Token"
41+
# config.api_key_prefix[:api_key] = "Token"
4242
property api_key_prefix : Hash(Symbol, String)
4343

4444
# Defines the username used with HTTP basic authentication.
@@ -183,10 +183,10 @@ module {{moduleName}}
183183
# Gets API key (with prefix if set).
184184
# @param [String] param_name the parameter name of API key auth
185185
def api_key_with_prefix(param_name)
186-
if @api_key_prefix[param_name]
187-
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
186+
if prefix = @api_key_prefix[param_name]?
187+
"#{prefix} #{@api_key[param_name]}"
188188
else
189-
@api_key[param_name]
189+
@api_key[param_name]? || ""
190190
end
191191
end
192192

@@ -204,7 +204,7 @@ module {{moduleName}}
204204
type: "api_key",
205205
in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}},
206206
key: "{{keyParamName}}",
207-
value: api_key_with_prefix("{{keyParamName}}")
207+
value: api_key_with_prefix(:{{keyParamName}})
208208
},
209209
{{/isApiKey}}
210210
{{#isBasic}}

samples/client/petstore/crystal/spec/api/pet_api_spec.cr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ describe "PetApi" do
7979
describe "get_pet_by_id test" do
8080
it "should work" do
8181
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
82-
api_instance = Petstore::PetApi.new
82+
83+
config = Petstore::Configuration.new
84+
config.access_token = "yyy"
85+
config.api_key[:api_key] = "xxx"
86+
config.api_key_prefix[:api_key] = "Token"
87+
88+
api_client = Petstore::ApiClient.new(config)
89+
90+
api_instance = Petstore::PetApi.new(api_client)
91+
8392
# create a pet to start with
8493
pet_id = Int64.new(91829)
8594
pet = Petstore::Pet.new(id: pet_id, category: Petstore::Category.new(id: pet_id + 10, name: "crystal category"), name: "crystal", photo_urls: ["https://crystal-lang.org"], tags: [Petstore::Tag.new(id: pet_id + 100, name: "crystal tag")], status: "available")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require "./spec_helper"
2+
3+
describe Petstore::ApiClient do
4+
describe "#update_params_for_auth!" do
5+
describe "oauth2" do
6+
it "should add 'Authorization' to header" do
7+
config = Petstore::Configuration.new
8+
config.access_token = "xxx"
9+
10+
header_params = {} of String => String
11+
query_params = {} of String => String
12+
13+
api_client = Petstore::ApiClient.new(config)
14+
api_client.update_params_for_auth!(header_params, query_params, ["petstore_auth"])
15+
16+
header_params["Authorization"].should eq "Bearer xxx"
17+
query_params.size.should eq 0
18+
end
19+
end
20+
21+
describe "api_key" do
22+
context "without api_key_prefix" do
23+
it "should add 'api_key' to header" do
24+
config = Petstore::Configuration.new
25+
config.api_key[:api_key] = "xxx"
26+
27+
header_params = {} of String => String
28+
query_params = {} of String => String
29+
30+
api_client = Petstore::ApiClient.new(config)
31+
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
32+
33+
header_params["api_key"].should eq "xxx"
34+
query_params.empty?.should be_true
35+
end
36+
end
37+
38+
context "with api_key_prefix" do
39+
it "should add 'api_key' to header" do
40+
config = Petstore::Configuration.new
41+
config.api_key[:api_key] = "xxx"
42+
config.api_key_prefix[:api_key] = "Token"
43+
44+
header_params = {} of String => String
45+
query_params = {} of String => String
46+
47+
api_client = Petstore::ApiClient.new(config)
48+
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
49+
50+
header_params["api_key"].should eq "Token xxx"
51+
query_params.empty?.should be_true
52+
end
53+
end
54+
end
55+
end
56+
end

samples/client/petstore/crystal/src/petstore/api/pet_api.cr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Petstore
4040
local_var_path = "/pet"
4141

4242
# query parameters
43-
query_params = Hash(Symbol, String).new
43+
query_params = Hash(String, String).new
4444

4545
# header parameters
4646
header_params = Hash(String, String).new
@@ -99,7 +99,7 @@ module Petstore
9999
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
100100

101101
# query parameters
102-
query_params = Hash(Symbol, String).new
102+
query_params = Hash(String, String).new
103103

104104
# header parameters
105105
header_params = Hash(String, String).new
@@ -157,8 +157,8 @@ module Petstore
157157
local_var_path = "/pet/findByStatus"
158158

159159
# query parameters
160-
query_params = Hash(Symbol, String).new
161-
query_params[:"status"] = @api_client.build_collection_param(status, :csv)
160+
query_params = Hash(String, String).new
161+
query_params["status"] = @api_client.build_collection_param(status, :csv)
162162

163163
# header parameters
164164
header_params = Hash(String, String).new
@@ -217,8 +217,8 @@ module Petstore
217217
local_var_path = "/pet/findByTags"
218218

219219
# query parameters
220-
query_params = Hash(Symbol, String).new
221-
query_params[:"tags"] = @api_client.build_collection_param(tags, :csv)
220+
query_params = Hash(String, String).new
221+
query_params["tags"] = @api_client.build_collection_param(tags, :csv)
222222

223223
# header parameters
224224
header_params = Hash(String, String).new
@@ -277,7 +277,7 @@ module Petstore
277277
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
278278

279279
# query parameters
280-
query_params = Hash(Symbol, String).new
280+
query_params = Hash(String, String).new
281281

282282
# header parameters
283283
header_params = Hash(String, String).new
@@ -334,7 +334,7 @@ module Petstore
334334
local_var_path = "/pet"
335335

336336
# query parameters
337-
query_params = Hash(Symbol, String).new
337+
query_params = Hash(String, String).new
338338

339339
# header parameters
340340
header_params = Hash(String, String).new
@@ -393,7 +393,7 @@ module Petstore
393393
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
394394

395395
# query parameters
396-
query_params = Hash(Symbol, String).new
396+
query_params = Hash(String, String).new
397397

398398
# header parameters
399399
header_params = Hash(String, String).new
@@ -452,7 +452,7 @@ module Petstore
452452
local_var_path = "/pet/{petId}/uploadImage".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
453453

454454
# query parameters
455-
query_params = Hash(Symbol, String).new
455+
query_params = Hash(String, String).new
456456

457457
# header parameters
458458
header_params = Hash(String, String).new

samples/client/petstore/crystal/src/petstore/api/store_api.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Petstore
4242
local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/"))
4343

4444
# query parameters
45-
query_params = Hash(Symbol, String).new
45+
query_params = Hash(String, String).new
4646

4747
# header parameters
4848
header_params = Hash(String, String).new
@@ -93,7 +93,7 @@ module Petstore
9393
local_var_path = "/store/inventory"
9494

9595
# query parameters
96-
query_params = Hash(Symbol, String).new
96+
query_params = Hash(String, String).new
9797

9898
# header parameters
9999
header_params = Hash(String, String).new
@@ -160,7 +160,7 @@ module Petstore
160160
local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/"))
161161

162162
# query parameters
163-
query_params = Hash(Symbol, String).new
163+
query_params = Hash(String, String).new
164164

165165
# header parameters
166166
header_params = Hash(String, String).new
@@ -217,7 +217,7 @@ module Petstore
217217
local_var_path = "/store/order"
218218

219219
# query parameters
220-
query_params = Hash(Symbol, String).new
220+
query_params = Hash(String, String).new
221221

222222
# header parameters
223223
header_params = Hash(String, String).new

samples/client/petstore/crystal/src/petstore/api/user_api.cr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Petstore
4242
local_var_path = "/user"
4343

4444
# query parameters
45-
query_params = Hash(Symbol, String).new
45+
query_params = Hash(String, String).new
4646

4747
# header parameters
4848
header_params = Hash(String, String).new
@@ -99,7 +99,7 @@ module Petstore
9999
local_var_path = "/user/createWithArray"
100100

101101
# query parameters
102-
query_params = Hash(Symbol, String).new
102+
query_params = Hash(String, String).new
103103

104104
# header parameters
105105
header_params = Hash(String, String).new
@@ -156,7 +156,7 @@ module Petstore
156156
local_var_path = "/user/createWithList"
157157

158158
# query parameters
159-
query_params = Hash(Symbol, String).new
159+
query_params = Hash(String, String).new
160160

161161
# header parameters
162162
header_params = Hash(String, String).new
@@ -215,7 +215,7 @@ module Petstore
215215
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
216216

217217
# query parameters
218-
query_params = Hash(Symbol, String).new
218+
query_params = Hash(String, String).new
219219

220220
# header parameters
221221
header_params = Hash(String, String).new
@@ -270,7 +270,7 @@ module Petstore
270270
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
271271

272272
# query parameters
273-
query_params = Hash(Symbol, String).new
273+
query_params = Hash(String, String).new
274274

275275
# header parameters
276276
header_params = Hash(String, String).new
@@ -338,9 +338,9 @@ module Petstore
338338
local_var_path = "/user/login"
339339

340340
# query parameters
341-
query_params = Hash(Symbol, String).new
342-
query_params[:"username"] = username
343-
query_params[:"password"] = password
341+
query_params = Hash(String, String).new
342+
query_params["username"] = username
343+
query_params["password"] = password
344344

345345
# header parameters
346346
header_params = Hash(String, String).new
@@ -391,7 +391,7 @@ module Petstore
391391
local_var_path = "/user/logout"
392392

393393
# query parameters
394-
query_params = Hash(Symbol, String).new
394+
query_params = Hash(String, String).new
395395

396396
# header parameters
397397
header_params = Hash(String, String).new
@@ -454,7 +454,7 @@ module Petstore
454454
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
455455

456456
# query parameters
457-
query_params = Hash(Symbol, String).new
457+
query_params = Hash(String, String).new
458458

459459
# header parameters
460460
header_params = Hash(String, String).new

samples/client/petstore/crystal/src/petstore/api_client.cr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ module Petstore
171171
# @param [Hash] query_params Query parameters
172172
# @param [String] auth_names Authentication scheme name
173173
def update_params_for_auth!(header_params, query_params, auth_names)
174-
Array{auth_names}.each do |auth_name|
174+
auth_names.each do |auth_name|
175175
auth_setting = @config.auth_settings[auth_name]
176176
next unless auth_setting
177177
case auth_setting[:in]
@@ -264,7 +264,7 @@ module Petstore
264264
#
265265
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
266266
# the data deserialized from response body (could be nil), response status code and response headers.
267-
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of Symbol => String, form_params = {} of Symbol => String)
267+
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of String => String, form_params = {} of Symbol => String)
268268
#ssl_options = {
269269
# :ca_file => @config.ssl_ca_file,
270270
# :verify => @config.ssl_verify,
@@ -282,6 +282,8 @@ module Petstore
282282
# conn.adapter(Faraday.default_adapter)
283283
#end
284284

285+
update_params_for_auth! header_params, query_params, auth_names
286+
285287
if !post_body.nil? && !post_body.empty?
286288
# use JSON string in the payload
287289
form_or_body = post_body

0 commit comments

Comments
 (0)