Skip to content

Commit b54cc85

Browse files
committed
fix(crystal): fix Model#to_h method
1 parent 4f9f14a commit b54cc85

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ public void processOpts() {
284284
supportingFiles.add(new SupportingFile("api_error.mustache", shardFolder, "api_error.cr"));
285285
supportingFiles.add(new SupportingFile("configuration.mustache", shardFolder, "configuration.cr"));
286286
supportingFiles.add(new SupportingFile("api_client.mustache", shardFolder, "api_client.cr"));
287+
supportingFiles.add(new SupportingFile("recursive_hash.mustache", shardFolder, "recursive_hash.cr"));
287288
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
288289
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
289290
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,38 @@
7676
# Returns the string representation of the object
7777
# @return [String] String presentation of the object
7878
def to_s
79-
to_hash.to_s
79+
to_h.to_s
8080
end
8181

82-
# to_body is an alias to to_hash (backward compatibility)
82+
# to_body is an alias to to_h (backward compatibility)
8383
# @return [Hash] Returns the object in the form of hash
8484
def to_body
85-
to_hash
85+
to_h
8686
end
8787

8888
# Returns the object in the form of hash
8989
# @return [Hash] Returns the object in the form of hash
90-
def to_hash
91-
hash = {{^parent}}{} of Symbol => String{{/parent}}{{#parent}}super{{/parent}}
92-
self.class.attribute_map.each_pair do |attr, param|
93-
value = self.send(attr)
94-
if value.nil?
95-
is_nullable = self.class.openapi_nullable.includes?(attr)
96-
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
97-
end
98-
99-
hash[param] = _to_hash(value)
100-
end
101-
hash
90+
def to_h
91+
hash = NetboxClient::RecursiveHash.new
92+
{{#vars}}
93+
hash["{{{baseName}}}"] = _to_h({{{name}}})
94+
{{/vars}}
95+
hash.to_h
10296
end
10397

10498
# Outputs non-array value in the form of hash
105-
# For object, use to_hash. Otherwise, just return the value
99+
# For object, use to_h. Otherwise, just return the value
106100
# @param [Object] value Any valid value
107101
# @return [Hash] Returns the value in the form of hash
108-
def _to_hash(value)
109-
if value.is_a?(Array)
110-
value.compact.map { |v| _to_hash(v) }
111-
elsif value.is_a?(Hash)
112-
({} of Symbol => String).tap do |hash|
113-
value.each { |k, v| hash[k] = _to_hash(v) }
114-
end
115-
elsif value.respond_to? :to_hash
116-
value.to_hash
102+
private def _to_h(value)
103+
if value.is_a?(Hash)
104+
hash = NetboxClient::RecursiveHash.new
105+
value.each { |k, v| hash[k] = _to_h(v) }
106+
hash
107+
elsif value.is_a?(Array)
108+
value.compact.map { |v| _to_h(v) }
109+
elsif value.responds_to?(:to_h)
110+
value.to_h
117111
else
118112
value
119113
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module {{moduleName}}
2+
# Define possible value types for our own AnyHash class (RecursiveHash)
3+
alias ValuesType = Nil |
4+
Bool |
5+
String |
6+
Time |
7+
Int32 |
8+
Int64 |
9+
Float32 |
10+
Float64 |
11+
Array(ValuesType)
12+
13+
# Define our own AnyHash class (RecursiveHash)
14+
# RecursiveHash
15+
AnyHash.define_new klass: :RecursiveHash,
16+
key: String,
17+
value: ValuesType
18+
end

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# {{#lambdaPrefixWithHash}}{{> api_info}}{{/lambdaPrefixWithHash}}
22

33
# Dependencies
4+
require "any_hash"
45
require "crest"
56
require "log"
67

0 commit comments

Comments
 (0)