diff --git a/modules/openapi-generator/src/main/resources/crystal/api.mustache b/modules/openapi-generator/src/main/resources/crystal/api.mustache index 8b8daabb5f3..787189be854 100644 --- a/modules/openapi-generator/src/main/resources/crystal/api.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/api.mustache @@ -128,9 +128,9 @@ module {{moduleName}} local_var_path = "{{{path}}}"{{#pathParams}}.sub("{" + "{{baseName}}" + "}", URI.encode({{paramName}}.to_s){{^strictSpecBehavior}}.gsub("%2F", "/"){{/strictSpecBehavior}}){{/pathParams}} # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new {{#queryParams}} - query_params[:"{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}} + query_params["{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}} {{/queryParams}} # header parameters diff --git a/modules/openapi-generator/src/main/resources/crystal/api_client.mustache b/modules/openapi-generator/src/main/resources/crystal/api_client.mustache index 6831e51bae0..9852faa90f9 100644 --- a/modules/openapi-generator/src/main/resources/crystal/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/api_client.mustache @@ -163,7 +163,7 @@ module {{moduleName}} # @param [Hash] query_params Query parameters # @param [String] auth_names Authentication scheme name def update_params_for_auth!(header_params, query_params, auth_names) - Array{auth_names}.each do |auth_name| + auth_names.each do |auth_name| auth_setting = @config.auth_settings[auth_name] next unless auth_setting case auth_setting[:in] @@ -256,7 +256,7 @@ module {{moduleName}} # # @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # the data deserialized from response body (could be nil), response status code and response headers. - 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) + 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) #ssl_options = { # :ca_file => @config.ssl_ca_file, # :verify => @config.ssl_verify, @@ -274,6 +274,8 @@ module {{moduleName}} # conn.adapter(Faraday.default_adapter) #end + update_params_for_auth! header_params, query_params, auth_names + if !post_body.nil? && !post_body.empty? # use JSON string in the payload form_or_body = post_body diff --git a/modules/openapi-generator/src/main/resources/crystal/configuration.mustache b/modules/openapi-generator/src/main/resources/crystal/configuration.mustache index cc2e15e93fa..eec2509376d 100644 --- a/modules/openapi-generator/src/main/resources/crystal/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/crystal/configuration.mustache @@ -30,7 +30,7 @@ module {{moduleName}} # @return [Hash] key: parameter name, value: parameter value (API key) # # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string) - # config.api_key[:"api_key"] = "xxx" + # config.api_key[:api_key] = "xxx" property api_key : Hash(Symbol, String) # Defines API key prefixes used with API Key authentications. @@ -38,7 +38,7 @@ module {{moduleName}} # @return [Hash] key: parameter name, value: API key prefix # # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers) - # config.api_key_prefix[:"api_key"] = "Token" + # config.api_key_prefix[:api_key] = "Token" property api_key_prefix : Hash(Symbol, String) # Defines the username used with HTTP basic authentication. @@ -183,10 +183,10 @@ module {{moduleName}} # Gets API key (with prefix if set). # @param [String] param_name the parameter name of API key auth def api_key_with_prefix(param_name) - if @api_key_prefix[param_name] - "#{@api_key_prefix[param_name]} #{@api_key[param_name]}" + if prefix = @api_key_prefix[param_name]? + "#{prefix} #{@api_key[param_name]}" else - @api_key[param_name] + @api_key[param_name]? || "" end end @@ -204,7 +204,7 @@ module {{moduleName}} type: "api_key", in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}, key: "{{keyParamName}}", - value: api_key_with_prefix("{{keyParamName}}") + value: api_key_with_prefix(:{{keyParamName}}) }, {{/isApiKey}} {{#isBasic}} diff --git a/samples/client/petstore/crystal/spec/api/pet_api_spec.cr b/samples/client/petstore/crystal/spec/api/pet_api_spec.cr index 23bdfa81847..5c5f56295ac 100644 --- a/samples/client/petstore/crystal/spec/api/pet_api_spec.cr +++ b/samples/client/petstore/crystal/spec/api/pet_api_spec.cr @@ -79,7 +79,16 @@ describe "PetApi" do describe "get_pet_by_id test" do it "should work" do # assertion here. ref: https://crystal-lang.org/reference/guides/testing.html - api_instance = Petstore::PetApi.new + + config = Petstore::Configuration.new + config.access_token = "yyy" + config.api_key[:api_key] = "xxx" + config.api_key_prefix[:api_key] = "Token" + + api_client = Petstore::ApiClient.new(config) + + api_instance = Petstore::PetApi.new(api_client) + # create a pet to start with pet_id = Int64.new(91829) 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") diff --git a/samples/client/petstore/crystal/spec/api_client_spec.cr b/samples/client/petstore/crystal/spec/api_client_spec.cr new file mode 100644 index 00000000000..18209417eab --- /dev/null +++ b/samples/client/petstore/crystal/spec/api_client_spec.cr @@ -0,0 +1,56 @@ +require "./spec_helper" + +describe Petstore::ApiClient do + describe "#update_params_for_auth!" do + describe "oauth2" do + it "should add 'Authorization' to header" do + config = Petstore::Configuration.new + config.access_token = "xxx" + + header_params = {} of String => String + query_params = {} of String => String + + api_client = Petstore::ApiClient.new(config) + api_client.update_params_for_auth!(header_params, query_params, ["petstore_auth"]) + + header_params["Authorization"].should eq "Bearer xxx" + query_params.size.should eq 0 + end + end + + describe "api_key" do + context "without api_key_prefix" do + it "should add 'api_key' to header" do + config = Petstore::Configuration.new + config.api_key[:api_key] = "xxx" + + header_params = {} of String => String + query_params = {} of String => String + + api_client = Petstore::ApiClient.new(config) + api_client.update_params_for_auth!(header_params, query_params, ["api_key"]) + + header_params["api_key"].should eq "xxx" + query_params.empty?.should be_true + end + end + + context "with api_key_prefix" do + it "should add 'api_key' to header" do + config = Petstore::Configuration.new + config.api_key[:api_key] = "xxx" + config.api_key_prefix[:api_key] = "Token" + + header_params = {} of String => String + query_params = {} of String => String + + api_client = Petstore::ApiClient.new(config) + api_client.update_params_for_auth!(header_params, query_params, ["api_key"]) + + header_params["api_key"].should eq "Token xxx" + query_params.empty?.should be_true + end + end + end + end +end diff --git a/samples/client/petstore/crystal/src/petstore/api/pet_api.cr b/samples/client/petstore/crystal/src/petstore/api/pet_api.cr index 7e672a21a61..41466efaf1b 100644 --- a/samples/client/petstore/crystal/src/petstore/api/pet_api.cr +++ b/samples/client/petstore/crystal/src/petstore/api/pet_api.cr @@ -40,7 +40,7 @@ module Petstore local_var_path = "/pet" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -99,7 +99,7 @@ module Petstore local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -157,8 +157,8 @@ module Petstore local_var_path = "/pet/findByStatus" # query parameters - query_params = Hash(Symbol, String).new - query_params[:"status"] = @api_client.build_collection_param(status, :csv) + query_params = Hash(String, String).new + query_params["status"] = @api_client.build_collection_param(status, :csv) # header parameters header_params = Hash(String, String).new @@ -217,8 +217,8 @@ module Petstore local_var_path = "/pet/findByTags" # query parameters - query_params = Hash(Symbol, String).new - query_params[:"tags"] = @api_client.build_collection_param(tags, :csv) + query_params = Hash(String, String).new + query_params["tags"] = @api_client.build_collection_param(tags, :csv) # header parameters header_params = Hash(String, String).new @@ -277,7 +277,7 @@ module Petstore local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -334,7 +334,7 @@ module Petstore local_var_path = "/pet" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -393,7 +393,7 @@ module Petstore local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -452,7 +452,7 @@ module Petstore local_var_path = "/pet/{petId}/uploadImage".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new diff --git a/samples/client/petstore/crystal/src/petstore/api/store_api.cr b/samples/client/petstore/crystal/src/petstore/api/store_api.cr index 126dbab7bf4..03f45309463 100644 --- a/samples/client/petstore/crystal/src/petstore/api/store_api.cr +++ b/samples/client/petstore/crystal/src/petstore/api/store_api.cr @@ -42,7 +42,7 @@ module Petstore local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -93,7 +93,7 @@ module Petstore local_var_path = "/store/inventory" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -160,7 +160,7 @@ module Petstore local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -217,7 +217,7 @@ module Petstore local_var_path = "/store/order" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new diff --git a/samples/client/petstore/crystal/src/petstore/api/user_api.cr b/samples/client/petstore/crystal/src/petstore/api/user_api.cr index 2b6d6acdace..0b2468c50d3 100644 --- a/samples/client/petstore/crystal/src/petstore/api/user_api.cr +++ b/samples/client/petstore/crystal/src/petstore/api/user_api.cr @@ -42,7 +42,7 @@ module Petstore local_var_path = "/user" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -99,7 +99,7 @@ module Petstore local_var_path = "/user/createWithArray" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -156,7 +156,7 @@ module Petstore local_var_path = "/user/createWithList" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -215,7 +215,7 @@ module Petstore local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -270,7 +270,7 @@ module Petstore local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -338,9 +338,9 @@ module Petstore local_var_path = "/user/login" # query parameters - query_params = Hash(Symbol, String).new - query_params[:"username"] = username - query_params[:"password"] = password + query_params = Hash(String, String).new + query_params["username"] = username + query_params["password"] = password # header parameters header_params = Hash(String, String).new @@ -391,7 +391,7 @@ module Petstore local_var_path = "/user/logout" # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new @@ -454,7 +454,7 @@ module Petstore local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) # query parameters - query_params = Hash(Symbol, String).new + query_params = Hash(String, String).new # header parameters header_params = Hash(String, String).new diff --git a/samples/client/petstore/crystal/src/petstore/api_client.cr b/samples/client/petstore/crystal/src/petstore/api_client.cr index 75fe8faa203..a0f4a469c48 100644 --- a/samples/client/petstore/crystal/src/petstore/api_client.cr +++ b/samples/client/petstore/crystal/src/petstore/api_client.cr @@ -171,7 +171,7 @@ module Petstore # @param [Hash] query_params Query parameters # @param [String] auth_names Authentication scheme name def update_params_for_auth!(header_params, query_params, auth_names) - Array{auth_names}.each do |auth_name| + auth_names.each do |auth_name| auth_setting = @config.auth_settings[auth_name] next unless auth_setting case auth_setting[:in] @@ -264,7 +264,7 @@ module Petstore # # @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # the data deserialized from response body (could be nil), response status code and response headers. - 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) + 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) #ssl_options = { # :ca_file => @config.ssl_ca_file, # :verify => @config.ssl_verify, @@ -282,6 +282,8 @@ module Petstore # conn.adapter(Faraday.default_adapter) #end + update_params_for_auth! header_params, query_params, auth_names + if !post_body.nil? && !post_body.empty? # use JSON string in the payload form_or_body = post_body diff --git a/samples/client/petstore/crystal/src/petstore/configuration.cr b/samples/client/petstore/crystal/src/petstore/configuration.cr index e02acd2e507..b3d1c1ca202 100644 --- a/samples/client/petstore/crystal/src/petstore/configuration.cr +++ b/samples/client/petstore/crystal/src/petstore/configuration.cr @@ -38,7 +38,7 @@ module Petstore # @return [Hash] key: parameter name, value: parameter value (API key) # # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string) - # config.api_key[:"api_key"] = "xxx" + # config.api_key[:api_key] = "xxx" property api_key : Hash(Symbol, String) # Defines API key prefixes used with API Key authentications. @@ -46,7 +46,7 @@ module Petstore # @return [Hash] key: parameter name, value: API key prefix # # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers) - # config.api_key_prefix[:"api_key"] = "Token" + # config.api_key_prefix[:api_key] = "Token" property api_key_prefix : Hash(Symbol, String) # Defines the username used with HTTP basic authentication. @@ -191,10 +191,10 @@ module Petstore # Gets API key (with prefix if set). # @param [String] param_name the parameter name of API key auth def api_key_with_prefix(param_name) - if @api_key_prefix[param_name] - "#{@api_key_prefix[param_name]} #{@api_key[param_name]}" + if prefix = @api_key_prefix[param_name]? + "#{prefix} #{@api_key[param_name]}" else - @api_key[param_name] + @api_key[param_name]? || "" end end @@ -210,7 +210,7 @@ module Petstore type: "api_key", in: "header", key: "api_key", - value: api_key_with_prefix("api_key") + value: api_key_with_prefix(:api_key) }, "petstore_auth" => { type: "oauth2",