-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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] Fix some issues in crystal client templates #10629
Changes from 5 commits
9291810
638b447
961afec
28db021
42d3658
15c8edc
6439537
e6c6666
b825728
4c9ec63
00431a2
ec60ecf
d65cad2
2c17a6f
4825d2a
dce8bbb
e8242dd
acf8050
edd01d3
0192e50
210e149
e575bd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,105 +38,6 @@ module {{moduleName}} | |
(mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil? | ||
end | ||
|
||
# Deserialize the response to the given return type. | ||
# | ||
# @param [Response] response HTTP response | ||
# @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>" | ||
def deserialize(response, return_type) | ||
body = response.body | ||
|
||
# handle file downloading - return the File instance processed in request callbacks | ||
# note that response body is empty when the file is written in chunks in request on_body callback | ||
if return_type == "File" | ||
content_disposition = response.headers["Content-Disposition"].to_s | ||
if content_disposition && content_disposition =~ /filename=/i | ||
filename = content_disposition.match(/filename=[""]?([^""\s]+)[""]?/i).try &.[0] | ||
prefix = sanitize_filename(filename) | ||
else | ||
prefix = "download-" | ||
end | ||
if !prefix.nil? && prefix.ends_with?("-") | ||
prefix = prefix + "-" | ||
end | ||
encoding = response.headers["Content-Encoding"].to_s | ||
|
||
# TODO add file support | ||
raise ApiError.new(code: 0, message: "File response not yet supported in the client.") if return_type | ||
return nil | ||
|
||
#@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) | ||
#@tempfile.write(@stream.join.force_encoding(encoding)) | ||
#@tempfile.close | ||
#Log.info { "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\ | ||
# "with e.g. `FileUtils.cp(tempfile.path, \"/new/file/path\")` otherwise the temp file "\ | ||
# "will be deleted automatically with GC. It's also recommended to delete the temp file "\ | ||
# "explicitly with `tempfile.delete`" } | ||
#return @tempfile | ||
end | ||
|
||
return nil if body.nil? || body.empty? | ||
|
||
# return response body directly for String return type | ||
return body if return_type == "String" | ||
|
||
# ensuring a default content type | ||
content_type = response.headers["Content-Type"] || "application/json" | ||
|
||
raise ApiError.new(code: 0, message: "Content-Type is not supported: #{content_type}") unless json_mime?(content_type) | ||
|
||
begin | ||
data = JSON.parse("[#{body}]")[0] | ||
rescue e : Exception | ||
if %w(String Date Time).includes?(return_type) | ||
data = body | ||
else | ||
raise e | ||
end | ||
end | ||
|
||
convert_to_type data, return_type | ||
end | ||
|
||
# Convert data to the given return type. | ||
# @param [Object] data Data to be converted | ||
# @param [String] return_type Return type | ||
# @return [Mixed] Data in a particular type | ||
def convert_to_type(data, return_type) | ||
return nil if data.nil? | ||
case return_type | ||
when "String" | ||
data.to_s | ||
when "Integer" | ||
data.to_s.to_i | ||
when "Float" | ||
data.to_s.to_f | ||
when "Boolean" | ||
data == true | ||
when "Time" | ||
# parse date time (expecting ISO 8601 format) | ||
Time.parse! data.to_s, "%Y-%m-%dT%H:%M:%S%Z" | ||
when "Date" | ||
# parse date (expecting ISO 8601 format) | ||
Time.parse! data.to_s, "%Y-%m-%d" | ||
when "Object" | ||
# generic object (usually a Hash), return directly | ||
data | ||
when /\AArray<(.+)>\z/ | ||
# e.g. Array<Pet> | ||
sub_type = $1 | ||
data.map { |item| convert_to_type(item, sub_type) } | ||
when /\AHash\<String, (.+)\>\z/ | ||
# e.g. Hash<String, Integer> | ||
sub_type = $1 | ||
({} of Symbol => String).tap do |hash| | ||
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) } | ||
end | ||
else | ||
# models (e.g. Pet) or oneOf | ||
klass = Petstore.const_get(return_type) | ||
klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data) | ||
end | ||
end | ||
|
||
# Sanitize filename by removing path. | ||
# e.g. ../../sun.gif becomes sun.gif | ||
|
@@ -245,18 +146,18 @@ module {{moduleName}} | |
when :pipes | ||
param.join("|") | ||
when :multi | ||
# return the array directly as typhoeus will handle it as expected | ||
param | ||
# TODO: Need to fix this | ||
raise "mult is not supported yet" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
else | ||
fail "unknown collection format: #{collection_format.inspect}" | ||
raise "unknown collection format: #{collection_format.inspect}" | ||
end | ||
end | ||
|
||
# Call an API with given options. | ||
# | ||
# @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) | ||
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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
class {{classname}}{{#allowableValues}}{{#enumVars}} | ||
{{{name}}} = {{{value}}}.freeze{{/enumVars}} | ||
|
||
{{/allowableValues}} | ||
{{{name}}} = {{{value}}} | ||
{{/enumVars}} {{/allowableValues}} | ||
# Builds the enum from string | ||
# @param [String] The enum value in the form of the string | ||
# @return [String] The enum value | ||
|
@@ -13,8 +12,11 @@ | |
# @param [String] The enum value in the form of the string | ||
# @return [String] The enum value | ||
def build_from_hash(value) | ||
constantValues = {{classname}}.constants.select { |c| {{classname}}::const_get(c) == value } | ||
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if constantValues.empty? | ||
value | ||
case value{{#allowableValues}}{{#enumVars}} | ||
when {{{value}}} | ||
{{{name}}}{{/enumVars}}{{/allowableValues}} | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion to allow trailing spaces:
|
||
raise "Invalid ENUM value #{value} for class #{{{classname}}}" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,7 @@ module Petstore | |
# Deletes a pet | ||
# @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 | ||
|
@@ -373,7 +373,7 @@ module Petstore | |
# Updates a pet in the store with form data | ||
# @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 | ||
|
@@ -432,7 +432,7 @@ module Petstore | |
# uploads an image | ||
# @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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cyangle Is this (default to nil) also breaking change when it comes to calling the operations? If yes, please revert the change for the time being? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed it |
||
data, _status_code, _headers = upload_file_with_http_info(pet_id, additional_metadata, file) | ||
data | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
raise
an error sayingmulti
is not supported in the client?