Fix syntax errors and method calls in code generated by Crystal client generator (#8644)

* Fix string interpolation

* Fix syntax error and s/include\?/includes\?/

* Fix more `.include?` and `.key?` calls

* update samples

Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
Jamie Gaskins 2021-02-08 06:02:55 -05:00 committed by GitHub
parent d57aa95b80
commit d4c76df79f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 32 additions and 32 deletions

View File

@ -55,7 +55,7 @@ module {{moduleName}}
{{^isContainer}} {{^isContainer}}
# verify enum value # verify enum value
allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}] allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
if @api_client.config.client_side_validation && !allowable_values.include?({{{paramName}}}) if @api_client.config.client_side_validation && !allowable_values.includes?({{{paramName}}})
raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}") raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}")
end end
{{/isContainer}} {{/isContainer}}
@ -66,13 +66,13 @@ module {{moduleName}}
{{#isEnum}} {{#isEnum}}
{{#collectionFormat}} {{#collectionFormat}}
allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}] allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
if @api_client.config.client_side_validation && {{{paramName}}} && {{{paramName}}}.all? { |item| allowable_values.include?(item) } if @api_client.config.client_side_validation && {{{paramName}}} && {{{paramName}}}.all? { |item| allowable_values.includes?(item) }
raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must include one of #{allowable_values}") raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must include one of #{allowable_values}")
end end
{{/collectionFormat}} {{/collectionFormat}}
{{^collectionFormat}} {{^collectionFormat}}
allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}] allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
if @api_client.config.client_side_validation && {{{paramName}}} && !allowable_values.include?({{{paramName}}}]) if @api_client.config.client_side_validation && {{{paramName}}} && !allowable_values.includes?({{{paramName}}})
raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}") raise ArgumentError.new("invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}")
end end
{{/collectionFormat}} {{/collectionFormat}}

View File

@ -342,7 +342,7 @@ module {{moduleName}}
:verbose => @config.debugging :verbose => @config.debugging
} }
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].includes?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update body: req_body req_opts.update body: req_body
if @config.debugging if @config.debugging

View File

@ -81,7 +81,7 @@
:verbose => @config.debugging :verbose => @config.debugging
} }
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].includes?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update body: req_body req_opts.update body: req_body
if @config.debugging if @config.debugging

View File

@ -73,7 +73,7 @@
# set custom cert, if provided # set custom cert, if provided
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].includes?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update body: req_body req_opts.update body: req_body
if @config.debugging if @config.debugging

View File

@ -14,7 +14,7 @@
super(attributes) super(attributes)
{{/parent}} {{/parent}}
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -92,7 +92,7 @@
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -334,12 +334,12 @@ module {{moduleName}}
server = servers[index] server = servers[index]
url = server[:url] url = server[:url]
return url unless server.key? :variables return url unless server.has_key? :variables
# go through variable and assign a value # go through variable and assign a value
server[:variables].each do |name, variable| server[:variables].each do |name, variable|
if variables.key?(name) if variables.has_key?(name)
if (!server[:variables][name].key?(:enum_values) || server[:variables][name][:enum_values].include?(variables[name])) if (!server[:variables][name].has_key?(:enum_values) || server[:variables][name][:enum_values].includes?(variables[name]))
url.gsub! "{" + name.to_s + "}", variables[name] url.gsub! "{" + name.to_s + "}", variables[name]
else else
raise ArgumentError.new("The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}.") raise ArgumentError.new("The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}.")

View File

@ -30,7 +30,7 @@
end end
def valid?(value) def valid?(value)
!value || allowable_values.include?(value) !value || allowable_values.includes?(value)
end end
end end
@ -225,7 +225,7 @@
{{/isNullable}} {{/isNullable}}
{{#maxLength}} {{#maxLength}}
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}} if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
raise ArgumentError.new("invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.") raise ArgumentError.new("invalid value for \"{{{name}}}\", the character length must be smaller than or equal to {{{maxLength}}}.")
end end
{{/maxLength}} {{/maxLength}}

View File

@ -75,7 +75,7 @@
end end
end end
openapi_one_of.include?(:AnyType) ? data : nil openapi_one_of.includes?(:AnyType) ? data : nil
{{/discriminator}} {{/discriminator}}
end end
{{^discriminator}} {{^discriminator}}

View File

@ -350,7 +350,7 @@ module Petstore
:verbose => @config.debugging :verbose => @config.debugging
} }
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].includes?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update body: req_body req_opts.update body: req_body
if @config.debugging if @config.debugging

View File

@ -249,12 +249,12 @@ module Petstore
server = servers[index] server = servers[index]
url = server[:url] url = server[:url]
return url unless server.key? :variables return url unless server.has_key? :variables
# go through variable and assign a value # go through variable and assign a value
server[:variables].each do |name, variable| server[:variables].each do |name, variable|
if variables.key?(name) if variables.has_key?(name)
if (!server[:variables][name].key?(:enum_values) || server[:variables][name][:enum_values].include?(variables[name])) if (!server[:variables][name].has_key?(:enum_values) || server[:variables][name][:enum_values].includes?(variables[name]))
url.gsub! "{" + name.to_s + "}", variables[name] url.gsub! "{" + name.to_s + "}", variables[name]
else else
raise ArgumentError.new("The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}.") raise ArgumentError.new("The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}.")

View File

@ -78,7 +78,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -156,7 +156,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -90,7 +90,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -168,7 +168,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -57,7 +57,7 @@ module Petstore
end end
def valid?(value) def valid?(value)
!value || allowable_values.include?(value) !value || allowable_values.includes?(value)
end end
end end
@ -129,7 +129,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -207,7 +207,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -57,7 +57,7 @@ module Petstore
end end
def valid?(value) def valid?(value)
!value || allowable_values.include?(value) !value || allowable_values.includes?(value)
end end
end end
@ -139,7 +139,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -217,7 +217,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -73,7 +73,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -151,7 +151,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end

View File

@ -104,7 +104,7 @@ module Petstore
def build_from_hash(attributes) def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash) return nil unless attributes.is_a?(Hash)
self.class.openapi_types.each_pair do |key, type| self.class.openapi_types.each_pair do |key, type|
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key) if !attributes[self.class.attribute_map[key]]? && self.class.openapi_nullable.includes?(key)
self.send("#{key}=", nil) self.send("#{key}=", nil)
elsif type =~ /\AArray<(.*)>/i elsif type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute # check to ensure the input is an array given that the attribute
@ -182,7 +182,7 @@ module Petstore
self.class.attribute_map.each_pair do |attr, param| self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr) value = self.send(attr)
if value.nil? if value.nil?
is_nullable = self.class.openapi_nullable.include?(attr) is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end end