Skip to content

Commit 50be973

Browse files
committed
[ruby] Introduce ApiModelBase
The Ruby code that is generated copies numerous methods into every model. This creates bloat both in file size and run-time memory usage. This commit introduces a base class for all models to store common methods. This commit's focus is only on static methods that do not change between models.
1 parent ddb15d4 commit 50be973

File tree

256 files changed

+1185
-17864
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+1185
-17864
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public void processOpts() {
271271
supportingFiles.add(new SupportingFile("gem.mustache", libFolder, gemName + ".rb"));
272272
String gemFolder = libFolder + File.separator + gemName;
273273
supportingFiles.add(new SupportingFile("api_error.mustache", gemFolder, "api_error.rb"));
274+
supportingFiles.add(new SupportingFile("api_model_base.mustache", gemFolder, "api_model_base.rb"));
274275
supportingFiles.add(new SupportingFile("version.mustache", gemFolder, "version.rb"));
275276
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
276277
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=begin
2+
{{> api_info}}
3+
4+
=end
5+
6+
module {{moduleName}}
7+
class ApiModelBase
8+
# Deserializes the data based on type
9+
# @param string type Data type
10+
# @param string value Value to be deserialized
11+
# @return [Object] Deserialized data
12+
def self._deserialize(type, value)
13+
case type.to_sym
14+
when :Time
15+
Time.parse(value)
16+
when :Date
17+
Date.parse(value)
18+
when :String
19+
value.to_s
20+
when :Integer
21+
value.to_i
22+
when :Float
23+
value.to_f
24+
when :Boolean
25+
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
26+
true
27+
else
28+
false
29+
end
30+
when :Object
31+
# generic object (usually a Hash), return directly
32+
value
33+
when /\AArray<(?<inner_type>.+)>\z/
34+
inner_type = Regexp.last_match[:inner_type]
35+
value.map { |v| _deserialize(inner_type, v) }
36+
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
37+
k_type = Regexp.last_match[:k_type]
38+
v_type = Regexp.last_match[:v_type]
39+
{}.tap do |hash|
40+
value.each do |k, v|
41+
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
42+
end
43+
end
44+
else # model
45+
# models (e.g. Pet) or oneOf
46+
klass = {{moduleName}}.const_get(type)
47+
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
48+
end
49+
end
50+
51+
# Returns the string representation of the object
52+
# @return [String] String presentation of the object
53+
def to_s
54+
to_hash.to_s
55+
end
56+
57+
# to_body is an alias to to_hash (backward compatibility)
58+
# @return [Hash] Returns the object in the form of hash
59+
def to_body
60+
to_hash
61+
end
62+
63+
# Outputs non-array value in the form of hash
64+
# For object, use to_hash. Otherwise, just return the value
65+
# @param [Object] value Any valid value
66+
# @return [Hash] Returns the value in the form of hash
67+
def _to_hash(value)
68+
if value.is_a?(Array)
69+
value.compact.map { |v| _to_hash(v) }
70+
elsif value.is_a?(Hash)
71+
{}.tap do |hash|
72+
value.each { |k, v| hash[k] = _to_hash(v) }
73+
end
74+
elsif value.respond_to? :to_hash
75+
value.to_hash
76+
else
77+
value
78+
end
79+
end
80+
end
81+
end

modules/openapi-generator/src/main/resources/ruby-client/base_object.mustache

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,6 @@
2424
new(transformed_hash)
2525
end
2626

27-
# Deserializes the data based on type
28-
# @param string type Data type
29-
# @param string value Value to be deserialized
30-
# @return [Object] Deserialized data
31-
def self._deserialize(type, value)
32-
case type.to_sym
33-
when :Time
34-
Time.parse(value)
35-
when :Date
36-
Date.parse(value)
37-
when :String
38-
value.to_s
39-
when :Integer
40-
value.to_i
41-
when :Float
42-
value.to_f
43-
when :Boolean
44-
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
45-
true
46-
else
47-
false
48-
end
49-
when :Object
50-
# generic object (usually a Hash), return directly
51-
value
52-
when /\AArray<(?<inner_type>.+)>\z/
53-
inner_type = Regexp.last_match[:inner_type]
54-
value.map { |v| _deserialize(inner_type, v) }
55-
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
56-
k_type = Regexp.last_match[:k_type]
57-
v_type = Regexp.last_match[:v_type]
58-
{}.tap do |hash|
59-
value.each do |k, v|
60-
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
61-
end
62-
end
63-
else # model
64-
# models (e.g. Pet) or oneOf
65-
klass = {{moduleName}}.const_get(type)
66-
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
67-
end
68-
end
69-
70-
# Returns the string representation of the object
71-
# @return [String] String presentation of the object
72-
def to_s
73-
to_hash.to_s
74-
end
75-
76-
# to_body is an alias to to_hash (backward compatibility)
77-
# @return [Hash] Returns the object in the form of hash
78-
def to_body
79-
to_hash
80-
end
81-
8227
# Returns the object in the form of hash
8328
# @return [Hash] Returns the object in the form of hash
8429
def to_hash
@@ -94,21 +39,3 @@
9439
end
9540
hash
9641
end
97-
98-
# Outputs non-array value in the form of hash
99-
# For object, use to_hash. Otherwise, just return the value
100-
# @param [Object] value Any valid value
101-
# @return [Hash] Returns the value in the form of hash
102-
def _to_hash(value)
103-
if value.is_a?(Array)
104-
value.compact.map { |v| _to_hash(v) }
105-
elsif value.is_a?(Hash)
106-
{}.tap do |hash|
107-
value.each { |k, v| hash[k] = _to_hash(v) }
108-
end
109-
elsif value.respond_to? :to_hash
110-
value.to_hash
111-
else
112-
value
113-
end
114-
end

modules/openapi-generator/src/main/resources/ruby-client/gem.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Common files
77
require '{{gemName}}/api_client'
88
require '{{gemName}}/api_error'
9+
require '{{gemName}}/api_model_base'
910
require '{{gemName}}/version'
1011
require '{{gemName}}/configuration'
1112

modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{#description}}
22
# {{{.}}}
33
{{/description}}
4-
class {{classname}}{{#parent}} < {{{.}}}{{/parent}}
4+
class {{classname}}{{#parent}} < {{{.}}}{{/parent}}{{^parent}} < ApiModelBase{{/parent}}
55
{{#vars}}
66
{{#description}}
77
# {{{.}}}

samples/client/echo_api/ruby-faraday/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lib/openapi_client/api/path_api.rb
3434
lib/openapi_client/api/query_api.rb
3535
lib/openapi_client/api_client.rb
3636
lib/openapi_client/api_error.rb
37+
lib/openapi_client/api_model_base.rb
3738
lib/openapi_client/configuration.rb
3839
lib/openapi_client/models/bird.rb
3940
lib/openapi_client/models/category.rb

samples/client/echo_api/ruby-faraday/lib/openapi_client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Common files
1414
require 'openapi_client/api_client'
1515
require 'openapi_client/api_error'
16+
require 'openapi_client/api_model_base'
1617
require 'openapi_client/version'
1718
require 'openapi_client/configuration'
1819

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
=begin
2+
#Echo Server API
3+
4+
#Echo Server API
5+
6+
The version of the OpenAPI document: 0.1.0
7+
8+
Generated by: https://openapi-generator.tech
9+
Generator version: 7.17.0-SNAPSHOT
10+
11+
=end
12+
13+
module OpenapiClient
14+
class ApiModelBase
15+
# Deserializes the data based on type
16+
# @param string type Data type
17+
# @param string value Value to be deserialized
18+
# @return [Object] Deserialized data
19+
def self._deserialize(type, value)
20+
case type.to_sym
21+
when :Time
22+
Time.parse(value)
23+
when :Date
24+
Date.parse(value)
25+
when :String
26+
value.to_s
27+
when :Integer
28+
value.to_i
29+
when :Float
30+
value.to_f
31+
when :Boolean
32+
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
33+
true
34+
else
35+
false
36+
end
37+
when :Object
38+
# generic object (usually a Hash), return directly
39+
value
40+
when /\AArray<(?<inner_type>.+)>\z/
41+
inner_type = Regexp.last_match[:inner_type]
42+
value.map { |v| _deserialize(inner_type, v) }
43+
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
44+
k_type = Regexp.last_match[:k_type]
45+
v_type = Regexp.last_match[:v_type]
46+
{}.tap do |hash|
47+
value.each do |k, v|
48+
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
49+
end
50+
end
51+
else # model
52+
# models (e.g. Pet) or oneOf
53+
klass = OpenapiClient.const_get(type)
54+
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
55+
end
56+
end
57+
58+
# Returns the string representation of the object
59+
# @return [String] String presentation of the object
60+
def to_s
61+
to_hash.to_s
62+
end
63+
64+
# to_body is an alias to to_hash (backward compatibility)
65+
# @return [Hash] Returns the object in the form of hash
66+
def to_body
67+
to_hash
68+
end
69+
70+
# Outputs non-array value in the form of hash
71+
# For object, use to_hash. Otherwise, just return the value
72+
# @param [Object] value Any valid value
73+
# @return [Hash] Returns the value in the form of hash
74+
def _to_hash(value)
75+
if value.is_a?(Array)
76+
value.compact.map { |v| _to_hash(v) }
77+
elsif value.is_a?(Hash)
78+
{}.tap do |hash|
79+
value.each { |k, v| hash[k] = _to_hash(v) }
80+
end
81+
elsif value.respond_to? :to_hash
82+
value.to_hash
83+
else
84+
value
85+
end
86+
end
87+
end
88+
end

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/bird.rb

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
require 'time'
1515

1616
module OpenapiClient
17-
class Bird
17+
class Bird < ApiModelBase
1818
attr_accessor :size
1919

2020
attr_accessor :color
@@ -135,61 +135,6 @@ def self.build_from_hash(attributes)
135135
new(transformed_hash)
136136
end
137137

138-
# Deserializes the data based on type
139-
# @param string type Data type
140-
# @param string value Value to be deserialized
141-
# @return [Object] Deserialized data
142-
def self._deserialize(type, value)
143-
case type.to_sym
144-
when :Time
145-
Time.parse(value)
146-
when :Date
147-
Date.parse(value)
148-
when :String
149-
value.to_s
150-
when :Integer
151-
value.to_i
152-
when :Float
153-
value.to_f
154-
when :Boolean
155-
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
156-
true
157-
else
158-
false
159-
end
160-
when :Object
161-
# generic object (usually a Hash), return directly
162-
value
163-
when /\AArray<(?<inner_type>.+)>\z/
164-
inner_type = Regexp.last_match[:inner_type]
165-
value.map { |v| _deserialize(inner_type, v) }
166-
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
167-
k_type = Regexp.last_match[:k_type]
168-
v_type = Regexp.last_match[:v_type]
169-
{}.tap do |hash|
170-
value.each do |k, v|
171-
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
172-
end
173-
end
174-
else # model
175-
# models (e.g. Pet) or oneOf
176-
klass = OpenapiClient.const_get(type)
177-
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
178-
end
179-
end
180-
181-
# Returns the string representation of the object
182-
# @return [String] String presentation of the object
183-
def to_s
184-
to_hash.to_s
185-
end
186-
187-
# to_body is an alias to to_hash (backward compatibility)
188-
# @return [Hash] Returns the object in the form of hash
189-
def to_body
190-
to_hash
191-
end
192-
193138
# Returns the object in the form of hash
194139
# @return [Hash] Returns the object in the form of hash
195140
def to_hash
@@ -206,24 +151,6 @@ def to_hash
206151
hash
207152
end
208153

209-
# Outputs non-array value in the form of hash
210-
# For object, use to_hash. Otherwise, just return the value
211-
# @param [Object] value Any valid value
212-
# @return [Hash] Returns the value in the form of hash
213-
def _to_hash(value)
214-
if value.is_a?(Array)
215-
value.compact.map { |v| _to_hash(v) }
216-
elsif value.is_a?(Hash)
217-
{}.tap do |hash|
218-
value.each { |k, v| hash[k] = _to_hash(v) }
219-
end
220-
elsif value.respond_to? :to_hash
221-
value.to_hash
222-
else
223-
value
224-
end
225-
end
226-
227154
end
228155

229156
end

0 commit comments

Comments
 (0)