Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -284,6 +284,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("api_error.mustache", shardFolder, "api_error.cr"));
supportingFiles.add(new SupportingFile("configuration.mustache", shardFolder, "configuration.cr"));
supportingFiles.add(new SupportingFile("api_client.mustache", shardFolder, "api_client.cr"));
supportingFiles.add(new SupportingFile("recursive_hash.mustache", shardFolder, "recursive_hash.cr"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,44 +76,38 @@
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
to_h.to_s
end

# to_body is an alias to to_hash (backward compatibility)
# to_body is an alias to to_h (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
to_h
end

# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
def to_hash
hash = {{^parent}}{} of Symbol => String{{/parent}}{{#parent}}super{{/parent}}
self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr)
if value.nil?
is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end

hash[param] = _to_hash(value)
end
hash
def to_h
hash = NetboxClient::RecursiveHash.new
{{#vars}}
hash["{{{baseName}}}"] = _to_h({{{name}}})
{{/vars}}
hash.to_h
end

# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# For object, use to_h. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
def _to_hash(value)
if value.is_a?(Array)
value.compact.map { |v| _to_hash(v) }
elsif value.is_a?(Hash)
({} of Symbol => String).tap do |hash|
value.each { |k, v| hash[k] = _to_hash(v) }
end
elsif value.respond_to? :to_hash
value.to_hash
private def _to_h(value)
if value.is_a?(Hash)
hash = NetboxClient::RecursiveHash.new
value.each { |k, v| hash[k] = _to_h(v) }
hash
elsif value.is_a?(Array)
value.compact.map { |v| _to_h(v) }
elsif value.responds_to?(:to_h)
value.to_h
else
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
{{/discriminator}}
# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize({{#requiredVars}}@{{{name}}} : {{{dataType}}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}@{{{name}}} : {{{dataType}}}?{{^-last}}, {{/-last}}{{/optionalVars}})
def initialize({{#requiredVars}}@{{{name}}} : {{{dataType}}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}@{{{name}}} : {{{dataType}}}? = nil{{^-last}}, {{/-last}}{{/optionalVars}})
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module {{moduleName}}
# Define possible value types for our own AnyHash class (RecursiveHash)
alias ValuesType = Nil |
Bool |
String |
Time |
Int32 |
Int64 |
Float32 |
Float64 |
Array(ValuesType)

# Define our own AnyHash class (RecursiveHash)
# RecursiveHash
AnyHash.define_new klass: :RecursiveHash,
key: String,
value: ValuesType
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ description: |
- {{{ shardDescription}}}
crystal: ">= 0.35.1"
dependencies:
any_hash:
github: Sija/any_hash.cr
crest:
github: mamantoha/crest
version: ~> 1.3.13
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# {{#lambdaPrefixWithHash}}{{> api_info}}{{/lambdaPrefixWithHash}}

# Dependencies
require "any_hash"
require "crest"
require "log"

Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/crystal/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ src/petstore/models/order.cr
src/petstore/models/pet.cr
src/petstore/models/tag.cr
src/petstore/models/user.cr
src/petstore/recursive_hash.cr
2 changes: 2 additions & 0 deletions samples/client/petstore/crystal/shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ description: |
-
crystal: ">= 0.35.1"
dependencies:
any_hash:
github: Sija/any_hash.cr
crest:
github: mamantoha/crest
version: ~> 1.3.13
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/crystal/src/petstore.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#

# Dependencies
require "any_hash"
require "crest"
require "log"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Petstore

# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(@http_debug_operation : String?, @underscore_type : String?, @_type : String?, @type_with_underscore : String?)
def initialize(@http_debug_operation : String? = nil, @underscore_type : String? = nil, @_type : String? = nil, @type_with_underscore : String? = nil)
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand Down Expand Up @@ -147,44 +147,39 @@ module Petstore
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
to_h.to_s
end

# to_body is an alias to to_hash (backward compatibility)
# to_body is an alias to to_h (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
to_h
end

# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
def to_hash
hash = {} of Symbol => String
self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr)
if value.nil?
is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end

hash[param] = _to_hash(value)
end
hash
def to_h
hash = NetboxClient::RecursiveHash.new
hash["http_debug_operation"] = _to_h(http_debug_operation)
hash["_type"] = _to_h(underscore_type)
hash["type"] = _to_h(_type)
hash["type_"] = _to_h(type_with_underscore)
hash.to_h
end

# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# For object, use to_h. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
def _to_hash(value)
if value.is_a?(Array)
value.compact.map { |v| _to_hash(v) }
elsif value.is_a?(Hash)
({} of Symbol => String).tap do |hash|
value.each { |k, v| hash[k] = _to_hash(v) }
end
elsif value.respond_to? :to_hash
value.to_hash
private def _to_h(value)
if value.is_a?(Hash)
hash = NetboxClient::RecursiveHash.new
value.each { |k, v| hash[k] = _to_h(v) }
hash
elsif value.is_a?(Array)
value.compact.map { |v| _to_h(v) }
elsif value.responds_to?(:to_h)
value.to_h
else
value
end
Expand Down
46 changes: 20 additions & 26 deletions samples/client/petstore/crystal/src/petstore/models/api_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Petstore

# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(@code : Int32?, @_type : String?, @message : String?)
def initialize(@code : Int32? = nil, @_type : String? = nil, @message : String? = nil)
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand Down Expand Up @@ -144,44 +144,38 @@ module Petstore
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
to_h.to_s
end

# to_body is an alias to to_hash (backward compatibility)
# to_body is an alias to to_h (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
to_h
end

# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
def to_hash
hash = {} of Symbol => String
self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr)
if value.nil?
is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end

hash[param] = _to_hash(value)
end
hash
def to_h
hash = NetboxClient::RecursiveHash.new
hash["code"] = _to_h(code)
hash["type"] = _to_h(_type)
hash["message"] = _to_h(message)
hash.to_h
end

# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# For object, use to_h. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
def _to_hash(value)
if value.is_a?(Array)
value.compact.map { |v| _to_hash(v) }
elsif value.is_a?(Hash)
({} of Symbol => String).tap do |hash|
value.each { |k, v| hash[k] = _to_hash(v) }
end
elsif value.respond_to? :to_hash
value.to_hash
private def _to_h(value)
if value.is_a?(Hash)
hash = NetboxClient::RecursiveHash.new
value.each { |k, v| hash[k] = _to_h(v) }
hash
elsif value.is_a?(Array)
value.compact.map { |v| _to_h(v) }
elsif value.responds_to?(:to_h)
value.to_h
else
value
end
Expand Down
45 changes: 19 additions & 26 deletions samples/client/petstore/crystal/src/petstore/models/category.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Petstore

# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(@id : Int64?, @name : String?)
def initialize(@id : Int64? = nil, @name : String? = nil)
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand Down Expand Up @@ -157,44 +157,37 @@ module Petstore
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
to_h.to_s
end

# to_body is an alias to to_hash (backward compatibility)
# to_body is an alias to to_h (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
to_h
end

# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
def to_hash
hash = {} of Symbol => String
self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr)
if value.nil?
is_nullable = self.class.openapi_nullable.includes?(attr)
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
end

hash[param] = _to_hash(value)
end
hash
def to_h
hash = NetboxClient::RecursiveHash.new
hash["id"] = _to_h(id)
hash["name"] = _to_h(name)
hash.to_h
end

# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# For object, use to_h. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
def _to_hash(value)
if value.is_a?(Array)
value.compact.map { |v| _to_hash(v) }
elsif value.is_a?(Hash)
({} of Symbol => String).tap do |hash|
value.each { |k, v| hash[k] = _to_hash(v) }
end
elsif value.respond_to? :to_hash
value.to_hash
private def _to_h(value)
if value.is_a?(Hash)
hash = NetboxClient::RecursiveHash.new
value.each { |k, v| hash[k] = _to_h(v) }
hash
elsif value.is_a?(Array)
value.compact.map { |v| _to_h(v) }
elsif value.responds_to?(:to_h)
value.to_h
else
value
end
Expand Down
Loading
Loading