Skip to content

Commit 6cde5c5

Browse files
committed
fix(crystal): update samples
1 parent e13ff7f commit 6cde5c5

File tree

12 files changed

+205
-208
lines changed

12 files changed

+205
-208
lines changed

samples/client/petstore/crystal/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ src/petstore/models/order.cr
2020
src/petstore/models/pet.cr
2121
src/petstore/models/tag.cr
2222
src/petstore/models/user.cr
23+
src/petstore/recursive_hash.cr

samples/client/petstore/crystal/shard.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ description: |
66
-
77
crystal: ">= 0.35.1"
88
dependencies:
9+
any_hash:
10+
github: Sija/any_hash.cr
911
crest:
1012
github: mamantoha/crest
1113
version: ~> 1.3.13

samples/client/petstore/crystal/src/petstore.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#
1010

1111
# Dependencies
12+
require "any_hash"
1213
require "crest"
1314
require "log"
1415

samples/client/petstore/crystal/src/petstore/models/another_property_name_mapping.cr

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Petstore
3333

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

3939
# Show invalid properties with the reasons. Usually used together with valid?
@@ -147,44 +147,39 @@ module Petstore
147147
# Returns the string representation of the object
148148
# @return [String] String presentation of the object
149149
def to_s
150-
to_hash.to_s
150+
to_h.to_s
151151
end
152152

153-
# to_body is an alias to to_hash (backward compatibility)
153+
# to_body is an alias to to_h (backward compatibility)
154154
# @return [Hash] Returns the object in the form of hash
155155
def to_body
156-
to_hash
156+
to_h
157157
end
158158

159159
# Returns the object in the form of hash
160160
# @return [Hash] Returns the object in the form of hash
161-
def to_hash
162-
hash = {} of Symbol => String
163-
self.class.attribute_map.each_pair do |attr, param|
164-
value = self.send(attr)
165-
if value.nil?
166-
is_nullable = self.class.openapi_nullable.includes?(attr)
167-
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
168-
end
169-
170-
hash[param] = _to_hash(value)
171-
end
172-
hash
161+
def to_h
162+
hash = NetboxClient::RecursiveHash.new
163+
hash["http_debug_operation"] = _to_h(http_debug_operation)
164+
hash["_type"] = _to_h(underscore_type)
165+
hash["type"] = _to_h(_type)
166+
hash["type_"] = _to_h(type_with_underscore)
167+
hash.to_h
173168
end
174169

175170
# Outputs non-array value in the form of hash
176-
# For object, use to_hash. Otherwise, just return the value
171+
# For object, use to_h. Otherwise, just return the value
177172
# @param [Object] value Any valid value
178173
# @return [Hash] Returns the value in the form of hash
179-
def _to_hash(value)
180-
if value.is_a?(Array)
181-
value.compact.map { |v| _to_hash(v) }
182-
elsif value.is_a?(Hash)
183-
({} of Symbol => String).tap do |hash|
184-
value.each { |k, v| hash[k] = _to_hash(v) }
185-
end
186-
elsif value.respond_to? :to_hash
187-
value.to_hash
174+
private def _to_h(value)
175+
if value.is_a?(Hash)
176+
hash = NetboxClient::RecursiveHash.new
177+
value.each { |k, v| hash[k] = _to_h(v) }
178+
hash
179+
elsif value.is_a?(Array)
180+
value.compact.map { |v| _to_h(v) }
181+
elsif value.responds_to?(:to_h)
182+
value.to_h
188183
else
189184
value
190185
end

samples/client/petstore/crystal/src/petstore/models/api_response.cr

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Petstore
3131

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

3737
# Show invalid properties with the reasons. Usually used together with valid?
@@ -144,44 +144,38 @@ module Petstore
144144
# Returns the string representation of the object
145145
# @return [String] String presentation of the object
146146
def to_s
147-
to_hash.to_s
147+
to_h.to_s
148148
end
149149

150-
# to_body is an alias to to_hash (backward compatibility)
150+
# to_body is an alias to to_h (backward compatibility)
151151
# @return [Hash] Returns the object in the form of hash
152152
def to_body
153-
to_hash
153+
to_h
154154
end
155155

156156
# Returns the object in the form of hash
157157
# @return [Hash] Returns the object in the form of hash
158-
def to_hash
159-
hash = {} of Symbol => String
160-
self.class.attribute_map.each_pair do |attr, param|
161-
value = self.send(attr)
162-
if value.nil?
163-
is_nullable = self.class.openapi_nullable.includes?(attr)
164-
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
165-
end
166-
167-
hash[param] = _to_hash(value)
168-
end
169-
hash
158+
def to_h
159+
hash = NetboxClient::RecursiveHash.new
160+
hash["code"] = _to_h(code)
161+
hash["type"] = _to_h(_type)
162+
hash["message"] = _to_h(message)
163+
hash.to_h
170164
end
171165

172166
# Outputs non-array value in the form of hash
173-
# For object, use to_hash. Otherwise, just return the value
167+
# For object, use to_h. Otherwise, just return the value
174168
# @param [Object] value Any valid value
175169
# @return [Hash] Returns the value in the form of hash
176-
def _to_hash(value)
177-
if value.is_a?(Array)
178-
value.compact.map { |v| _to_hash(v) }
179-
elsif value.is_a?(Hash)
180-
({} of Symbol => String).tap do |hash|
181-
value.each { |k, v| hash[k] = _to_hash(v) }
182-
end
183-
elsif value.respond_to? :to_hash
184-
value.to_hash
170+
private def _to_h(value)
171+
if value.is_a?(Hash)
172+
hash = NetboxClient::RecursiveHash.new
173+
value.each { |k, v| hash[k] = _to_h(v) }
174+
hash
175+
elsif value.is_a?(Array)
176+
value.compact.map { |v| _to_h(v) }
177+
elsif value.responds_to?(:to_h)
178+
value.to_h
185179
else
186180
value
187181
end

samples/client/petstore/crystal/src/petstore/models/category.cr

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Petstore
2828

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

3434
# Show invalid properties with the reasons. Usually used together with valid?
@@ -157,44 +157,37 @@ module Petstore
157157
# Returns the string representation of the object
158158
# @return [String] String presentation of the object
159159
def to_s
160-
to_hash.to_s
160+
to_h.to_s
161161
end
162162

163-
# to_body is an alias to to_hash (backward compatibility)
163+
# to_body is an alias to to_h (backward compatibility)
164164
# @return [Hash] Returns the object in the form of hash
165165
def to_body
166-
to_hash
166+
to_h
167167
end
168168

169169
# Returns the object in the form of hash
170170
# @return [Hash] Returns the object in the form of hash
171-
def to_hash
172-
hash = {} of Symbol => String
173-
self.class.attribute_map.each_pair do |attr, param|
174-
value = self.send(attr)
175-
if value.nil?
176-
is_nullable = self.class.openapi_nullable.includes?(attr)
177-
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
178-
end
179-
180-
hash[param] = _to_hash(value)
181-
end
182-
hash
171+
def to_h
172+
hash = NetboxClient::RecursiveHash.new
173+
hash["id"] = _to_h(id)
174+
hash["name"] = _to_h(name)
175+
hash.to_h
183176
end
184177

185178
# Outputs non-array value in the form of hash
186-
# For object, use to_hash. Otherwise, just return the value
179+
# For object, use to_h. Otherwise, just return the value
187180
# @param [Object] value Any valid value
188181
# @return [Hash] Returns the value in the form of hash
189-
def _to_hash(value)
190-
if value.is_a?(Array)
191-
value.compact.map { |v| _to_hash(v) }
192-
elsif value.is_a?(Hash)
193-
({} of Symbol => String).tap do |hash|
194-
value.each { |k, v| hash[k] = _to_hash(v) }
195-
end
196-
elsif value.respond_to? :to_hash
197-
value.to_hash
182+
private def _to_h(value)
183+
if value.is_a?(Hash)
184+
hash = NetboxClient::RecursiveHash.new
185+
value.each { |k, v| hash[k] = _to_h(v) }
186+
hash
187+
elsif value.is_a?(Array)
188+
value.compact.map { |v| _to_h(v) }
189+
elsif value.responds_to?(:to_h)
190+
value.to_h
198191
else
199192
value
200193
end

samples/client/petstore/crystal/src/petstore/models/format_test.cr

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module Petstore
7272

7373
# Initializes the object
7474
# @param [Hash] attributes Model attributes in the form of hash
75-
def initialize(@number : Float64, @byte : String, @date : Time, @password : String, @integer : Int32?, @int32 : Int32?, @int64 : Int64?, @float : Float32?, @double : Float64?, @decimal : BigDecimal?, @string : String?, @binary : ::File?, @date_time : Time?, @uuid : String?, @pattern_with_digits : String?, @pattern_with_digits_and_delimiter : String?)
75+
def initialize(@number : Float64, @byte : String, @date : Time, @password : String, @integer : Int32? = nil, @int32 : Int32? = nil, @int64 : Int64? = nil, @float : Float32? = nil, @double : Float64? = nil, @decimal : BigDecimal? = nil, @string : String? = nil, @binary : ::File? = nil, @date_time : Time? = nil, @uuid : String? = nil, @pattern_with_digits : String? = nil, @pattern_with_digits_and_delimiter : String? = nil)
7676
end
7777

7878
# Show invalid properties with the reasons. Usually used together with valid?
@@ -393,44 +393,51 @@ module Petstore
393393
# Returns the string representation of the object
394394
# @return [String] String presentation of the object
395395
def to_s
396-
to_hash.to_s
396+
to_h.to_s
397397
end
398398

399-
# to_body is an alias to to_hash (backward compatibility)
399+
# to_body is an alias to to_h (backward compatibility)
400400
# @return [Hash] Returns the object in the form of hash
401401
def to_body
402-
to_hash
402+
to_h
403403
end
404404

405405
# Returns the object in the form of hash
406406
# @return [Hash] Returns the object in the form of hash
407-
def to_hash
408-
hash = {} of Symbol => String
409-
self.class.attribute_map.each_pair do |attr, param|
410-
value = self.send(attr)
411-
if value.nil?
412-
is_nullable = self.class.openapi_nullable.includes?(attr)
413-
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
414-
end
415-
416-
hash[param] = _to_hash(value)
417-
end
418-
hash
407+
def to_h
408+
hash = NetboxClient::RecursiveHash.new
409+
hash["integer"] = _to_h(integer)
410+
hash["int32"] = _to_h(int32)
411+
hash["int64"] = _to_h(int64)
412+
hash["number"] = _to_h(number)
413+
hash["float"] = _to_h(float)
414+
hash["double"] = _to_h(double)
415+
hash["decimal"] = _to_h(decimal)
416+
hash["string"] = _to_h(string)
417+
hash["byte"] = _to_h(byte)
418+
hash["binary"] = _to_h(binary)
419+
hash["date"] = _to_h(date)
420+
hash["dateTime"] = _to_h(date_time)
421+
hash["uuid"] = _to_h(uuid)
422+
hash["password"] = _to_h(password)
423+
hash["pattern_with_digits"] = _to_h(pattern_with_digits)
424+
hash["pattern_with_digits_and_delimiter"] = _to_h(pattern_with_digits_and_delimiter)
425+
hash.to_h
419426
end
420427

421428
# Outputs non-array value in the form of hash
422-
# For object, use to_hash. Otherwise, just return the value
429+
# For object, use to_h. Otherwise, just return the value
423430
# @param [Object] value Any valid value
424431
# @return [Hash] Returns the value in the form of hash
425-
def _to_hash(value)
426-
if value.is_a?(Array)
427-
value.compact.map { |v| _to_hash(v) }
428-
elsif value.is_a?(Hash)
429-
({} of Symbol => String).tap do |hash|
430-
value.each { |k, v| hash[k] = _to_hash(v) }
431-
end
432-
elsif value.respond_to? :to_hash
433-
value.to_hash
432+
private def _to_h(value)
433+
if value.is_a?(Hash)
434+
hash = NetboxClient::RecursiveHash.new
435+
value.each { |k, v| hash[k] = _to_h(v) }
436+
hash
437+
elsif value.is_a?(Array)
438+
value.compact.map { |v| _to_h(v) }
439+
elsif value.responds_to?(:to_h)
440+
value.to_h
434441
else
435442
value
436443
end

0 commit comments

Comments
 (0)