Skip to content

Commit f18219d

Browse files
committed
fix(crystal): implement cookie Auth
1 parent 478b119 commit f18219d

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

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

130+
# cookie parameters
131+
cookie_params = Hash(String, String).new
132+
130133
# query parameters
131134
query_params = Hash(String, String).new
132135
{{#queryParams}}
@@ -170,6 +173,7 @@ module {{moduleName}}
170173
auth_names,
171174
header_params,
172175
query_params,
176+
cookie_params,
173177
form_params)
174178
if @api_client.config.debugging
175179
Log.debug {"API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ module {{moduleName}}
5050
# @param [Hash] header_params Header parameters
5151
# @param [Hash] query_params Query parameters
5252
# @param [String] auth_names Authentication scheme name
53-
def update_params_for_auth!(header_params, query_params, auth_names)
53+
def update_params_for_auth!(header_params, query_params, cookie_params, auth_names)
5454
auth_names.each do |auth_name|
5555
auth_setting = @config.auth_settings[auth_name]
5656
next unless auth_setting
5757
case auth_setting[:in]
5858
when "header" then header_params[auth_setting[:key]] = auth_setting[:value]
5959
when "query" then query_params[auth_setting[:key]] = auth_setting[:value]
60-
else raise ArgumentError.new("Authentication token must be in `query` of `header`")
60+
when "cookie" then cookie_params[auth_setting[:key]] = auth_setting[:value]
61+
else raise ArgumentError.new("Authentication token must be in `cookie`, `query` or `header`")
6162
end
6263
end
6364
end
@@ -119,7 +120,7 @@ module {{moduleName}}
119120
#
120121
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
121122
# the data deserialized from response body (could be nil), response status code and response headers.
122-
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))
123+
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))
123124
#ssl_options = {
124125
# :ca_file => @config.ssl_ca_file,
125126
# :verify => @config.ssl_verify,
@@ -128,7 +129,7 @@ module {{moduleName}}
128129
# :client_key => @config.ssl_client_key
129130
#}
130131

131-
update_params_for_auth! header_params, query_params, auth_names
132+
update_params_for_auth! header_params, query_params, cookie_params, auth_names
132133

133134
if !post_body.nil? && !post_body.empty?
134135
# use JSON string in the payload
@@ -143,7 +144,7 @@ module {{moduleName}}
143144
build_request_url(path, operation),
144145
params: query_params,
145146
headers: header_params,
146-
#cookies: cookie_params, # TODO add cookies support
147+
cookies: cookie_params,
147148
form: form_or_body,
148149
logging: @config.debugging,
149150
handle_errors: false

0 commit comments

Comments
 (0)