Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[crystal-lang] Various fixes for Crystal client #21045

Merged
merged 6 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module {{moduleName}}
{{/required}}
{{/allParams}}
# @return [{{{returnType}}}{{^returnType}}nil{{/returnType}}]
def {{operationId}}({{#allParams}}{{paramName}} : {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
def {{operationId}}({{#allParams}}{{paramName}} : {{{dataType}}}{{^required}}?{{/required}}{{^required}} = nil{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
{{#returnType}}data, _status_code, _headers = {{/returnType}}{{operationId}}_with_http_info({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}})
{{#returnType}}data{{/returnType}}{{^returnType}}nil{{/returnType}}
end
Expand All @@ -40,7 +40,7 @@ module {{moduleName}}
{{/required}}
{{/allParams}}
# @return [Array<({{{returnType}}}{{^returnType}}nil{{/returnType}}, Integer, Hash)>] {{#returnType}}{{{.}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
def {{operationId}}_with_http_info({{#allParams}}{{paramName}} : {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
def {{operationId}}_with_http_info({{#allParams}}{{paramName}} : {{{dataType}}}{{^required}}?{{/required}}{{^required}} = nil{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
if @api_client.config.debugging
Log.debug {"Calling API: {{classname}}.{{operationId}} ..."}
end
Expand Down Expand Up @@ -127,10 +127,13 @@ module {{moduleName}}
# resource path
local_var_path = "{{{path}}}"{{#pathParams}}.sub("{" + "{{baseName}}" + "}", URI.encode_path({{paramName}}.to_s){{^strictSpecBehavior}}.gsub("%2F", "/"){{/strictSpecBehavior}}){{/pathParams}}

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new
{{#queryParams}}
query_params["{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}.to_s unless {{{paramName}}}.nil?{{/collectionFormat}}
query_params["{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}) unless {{{paramName}}}.nil?{{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}.to_s unless {{{paramName}}}.nil?{{/collectionFormat}}
{{/queryParams}}

# header parameters
Expand Down Expand Up @@ -170,6 +173,7 @@ module {{moduleName}}
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ module {{moduleName}}
# @param [Hash] header_params Header parameters
# @param [Hash] query_params Query parameters
# @param [String] auth_names Authentication scheme name
def update_params_for_auth!(header_params, query_params, auth_names)
def update_params_for_auth!(header_params, query_params, cookie_params, auth_names)
auth_names.each do |auth_name|
auth_setting = @config.auth_settings[auth_name]
next unless auth_setting
case auth_setting[:in]
when "header" then header_params[auth_setting[:key]] = auth_setting[:value]
when "query" then query_params[auth_setting[:key]] = auth_setting[:value]
else raise ArgumentError.new("Authentication token must be in `query` of `header`")
when "cookie" then cookie_params[auth_setting[:key]] = auth_setting[:value]
else raise ArgumentError.new("Authentication token must be in `cookie`, `query` or `header`")
end
end
end
Expand Down Expand Up @@ -119,7 +120,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 String => String, form_params = {} of Symbol => (String | ::File))
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, cookie_params = {} of String => String, form_params = {} of Symbol => (String | ::File))
#ssl_options = {
# :ca_file => @config.ssl_ca_file,
# :verify => @config.ssl_verify,
Expand All @@ -128,7 +129,7 @@ module {{moduleName}}
# :client_key => @config.ssl_client_key
#}

update_params_for_auth! header_params, query_params, auth_names
update_params_for_auth! header_params, query_params, cookie_params, auth_names

if !post_body.nil? && !post_body.empty?
# use JSON string in the payload
Expand All @@ -143,7 +144,7 @@ module {{moduleName}}
build_request_url(path, operation),
params: query_params,
headers: header_params,
#cookies: cookie_params, # TODO add cookies support
cookies: cookie_params,
form: form_or_body,
logging: @config.debugging,
handle_errors: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "big"
require "json"
require "yaml"
require "time"

module {{moduleName}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{/description}}
class {{classname}}{{#parent}} < {{{.}}}{{/parent}}
include JSON::Serializable
include YAML::Serializable

{{#hasRequired}}
# Required properties
Expand Down Expand Up @@ -269,16 +270,16 @@
{{/vars}}
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
return true if self.same?(o)
self.class == o.class{{#vars}} &&
{{name}} == o.{{name}}{{/vars}}{{#parent}} && super(o){{/parent}}
def ==(other)
return true if self.same?(other)
self.class == other.class{{#vars}} &&
{{name}} == other.{{name}}{{/vars}}{{#parent}} && super(other){{/parent}}
end

# @see the `==` method
# @param [Object] Object to be compared
def eql?(o)
self == o
def eql?(other)
self == other
end

# Calculates hash code according to all attributes.
Expand Down
Binary file modified samples/client/petstore/crystal/bin/ameba
Binary file not shown.
9 changes: 6 additions & 3 deletions samples/client/petstore/crystal/spec/api_client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Spectator.describe Petstore::ApiClient do

header_params = {} of String => String
query_params = {} of String => String
cookie_params = {} of String => String

api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["petstore_auth"])
api_client.update_params_for_auth!(header_params, query_params, cookie_params, ["petstore_auth"])

expect(header_params["Authorization"]).to eq "Bearer xxx"
expect(query_params.size).to eq 0
Expand All @@ -26,9 +27,10 @@ Spectator.describe Petstore::ApiClient do

header_params = {} of String => String
query_params = {} of String => String
cookie_params = {} of String => String

api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
api_client.update_params_for_auth!(header_params, query_params, cookie_params, ["api_key"])

expect(header_params["api_key"]).to eq "xxx"
expect(query_params.empty?).to be_true
Expand All @@ -43,9 +45,10 @@ Spectator.describe Petstore::ApiClient do

header_params = {} of String => String
query_params = {} of String => String
cookie_params = {} of String => String

api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
api_client.update_params_for_auth!(header_params, query_params, cookie_params, ["api_key"])

expect(header_params["api_key"]).to eq "Token xxx"
expect(query_params.empty?).to be_true
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/crystal/src/petstore/api/fake_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ module Petstore
# resource path
local_var_path = "/fake/parameter-name-mapping"

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new
query_params["type"] = _type.to_s unless _type.nil?
Expand Down Expand Up @@ -94,6 +97,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: FakeApi#get_parameter_name_mapping\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down
48 changes: 40 additions & 8 deletions samples/client/petstore/crystal/src/petstore/api/pet_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module Petstore
# resource path
local_var_path = "/pet"

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -71,6 +74,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#add_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand All @@ -82,7 +86,7 @@ module Petstore
#
# @param pet_id [Int64] Pet id to delete
# @return [nil]
def delete_pet(pet_id : Int64, api_key : String?)
def delete_pet(pet_id : Int64, api_key : String? = nil)
delete_pet_with_http_info(pet_id, api_key)
nil
end
Expand All @@ -91,7 +95,7 @@ module Petstore
#
# @param pet_id [Int64] Pet id to delete
# @return [Array<(nil, Integer, Hash)>] nil, response status code and response headers
def delete_pet_with_http_info(pet_id : Int64, api_key : String?)
def delete_pet_with_http_info(pet_id : Int64, api_key : String? = nil)
if @api_client.config.debugging
Log.debug {"Calling API: PetApi.delete_pet ..."}
end
Expand All @@ -102,6 +106,9 @@ module Petstore
# resource path
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode_path(pet_id.to_s).gsub("%2F", "/"))

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -129,6 +136,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#delete_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down Expand Up @@ -160,9 +168,12 @@ module Petstore
# resource path
local_var_path = "/pet/findByStatus"

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new
query_params["status"] = @api_client.build_collection_param(status, :csv)
query_params["status"] = @api_client.build_collection_param(status, :csv) unless status.nil?

# header parameters
header_params = Hash(String, String).new
Expand All @@ -189,6 +200,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#find_pets_by_status\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down Expand Up @@ -220,9 +232,12 @@ module Petstore
# resource path
local_var_path = "/pet/findByTags"

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new
query_params["tags"] = @api_client.build_collection_param(tags, :csv)
query_params["tags"] = @api_client.build_collection_param(tags, :csv) unless tags.nil?

# header parameters
header_params = Hash(String, String).new
Expand All @@ -249,6 +264,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#find_pets_by_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down Expand Up @@ -280,6 +296,9 @@ module Petstore
# resource path
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode_path(pet_id.to_s).gsub("%2F", "/"))

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -308,6 +327,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#get_pet_by_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down Expand Up @@ -339,6 +359,9 @@ module Petstore
# resource path
local_var_path = "/pet"

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -369,6 +392,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#update_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand All @@ -380,7 +404,7 @@ module Petstore
#
# @param pet_id [Int64] ID of pet that needs to be updated
# @return [nil]
def update_pet_with_form(pet_id : Int64, name : String?, status : String?)
def update_pet_with_form(pet_id : Int64, name : String? = nil, status : String? = nil)
update_pet_with_form_with_http_info(pet_id, name, status)
nil
end
Expand All @@ -389,7 +413,7 @@ module Petstore
#
# @param pet_id [Int64] ID of pet that needs to be updated
# @return [Array<(nil, Integer, Hash)>] nil, response status code and response headers
def update_pet_with_form_with_http_info(pet_id : Int64, name : String?, status : String?)
def update_pet_with_form_with_http_info(pet_id : Int64, name : String? = nil, status : String? = nil)
if @api_client.config.debugging
Log.debug {"Calling API: PetApi.update_pet_with_form ..."}
end
Expand All @@ -400,6 +424,9 @@ module Petstore
# resource path
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode_path(pet_id.to_s).gsub("%2F", "/"))

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -430,6 +457,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#update_pet_with_form\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand All @@ -441,7 +469,7 @@ module Petstore
#
# @param pet_id [Int64] ID of pet to update
# @return [ApiResponse]
def upload_file(pet_id : Int64, additional_metadata : String?, file : ::File?)
def upload_file(pet_id : Int64, additional_metadata : String? = nil, file : ::File? = nil)
data, _status_code, _headers = upload_file_with_http_info(pet_id, additional_metadata, file)
data
end
Expand All @@ -450,7 +478,7 @@ module Petstore
#
# @param pet_id [Int64] ID of pet to update
# @return [Array<(ApiResponse, Integer, Hash)>] ApiResponse data, response status code and response headers
def upload_file_with_http_info(pet_id : Int64, additional_metadata : String?, file : ::File?)
def upload_file_with_http_info(pet_id : Int64, additional_metadata : String? = nil, file : ::File? = nil)
if @api_client.config.debugging
Log.debug {"Calling API: PetApi.upload_file ..."}
end
Expand All @@ -461,6 +489,9 @@ module Petstore
# resource path
local_var_path = "/pet/{petId}/uploadImage".sub("{" + "petId" + "}", URI.encode_path(pet_id.to_s).gsub("%2F", "/"))

# cookie parameters
cookie_params = Hash(String, String).new

# query parameters
query_params = Hash(String, String).new

Expand Down Expand Up @@ -493,6 +524,7 @@ module Petstore
auth_names,
header_params,
query_params,
cookie_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: PetApi#upload_file\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
Expand Down
Loading
Loading