Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby client [2.X] Added inheritance/polymorphism (allOf) to ruby client #11969

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,6 +3,7 @@
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
Expand Down Expand Up @@ -61,6 +62,7 @@ public RubyClientCodegen() {
// at the moment
importMapping.clear();

this.supportsInheritance = true;
modelPackage = "models";
apiPackage = "api";
outputFolder = "generated-code" + File.separator + "ruby";
Expand Down Expand Up @@ -258,6 +260,11 @@ public void processOpts() {
//writeOptional(outputFolder, new SupportingFile("base_object_spec.mustache", specFolder, "base_object_spec.rb"));
}

@Override
public String toModelImport(String name) {
return this.toModelFilename(name);
}

Comment on lines +263 to +267
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the import statement to use the file name rather than the model name as that is what Ruby uses to import the additional class

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);
Expand Down Expand Up @@ -605,6 +612,13 @@ public String toEnumName(CodegenProperty property) {
}
}

@Override
protected void addImport(CodegenModel m, String type) {
if (m.parent != null && m.parent.equals(type)) {
super.addImport(m, type);
}
}

Comment on lines +615 to +621
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only adds the parent class for importing as the ruby client does not need to know the type of the values it works with, but does need to import the parent class

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# @return [Object] Returns the model itself
def build_from_hash(attributes)
return nil unless attributes.is_a?(Hash)
{{#parent}}
super(attributes)
{{/parent}}
self.class.swagger_types.each_pair do |key, type|
if type =~ /\AArray<(.*)>/i
# check to ensure the input is an array given that the attribute
Expand Down Expand Up @@ -75,7 +78,7 @@
# Returns the object in the form of hash
# @return [Hash] Returns the object in the form of hash
def to_hash
hash = {}
hash = {{^parent}}{}{{/parent}}{{#parent}}super{{/parent}}
self.class.attribute_map.each_pair do |attr, param|
value = self.send(attr)
next if value.nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
=end

require 'date'
{{#imports}}require_relative '{{import}}'
{{/imports}}

module {{moduleName}}
{{#models}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#description}}
# {{{description}}}
{{/description}}
class {{classname}}
class {{classname}}{{#parent}} < {{parent}}{{/parent}}
{{#vars}}
{{#description}}
# {{{description}}}
Expand Down Expand Up @@ -36,18 +36,18 @@
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
{{#vars}}
{{#allVars}}
:'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
{{/allVars}}
}
end

# Attribute type mapping.
def self.swagger_types
{
{{#vars}}
{{#allVars}}
:'{{{name}}}' => :'{{{datatype}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
{{/allVars}}
Comment on lines +39 to +50
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the way the deserializer works in the api_client, the parent values must be present in swagger_types and attribute_map

}
end

Expand All @@ -58,6 +58,11 @@

# convert string to symbol for hash key
attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
{{#parent}}

# call parent's initialize
super(attributes)
{{/parent}}
Comment on lines +61 to +65
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent constructor is called with the values passed in to the constructor of the child

{{#vars}}

if attributes.has_key?(:'{{{baseName}}}')
Expand Down Expand Up @@ -85,7 +90,7 @@
# Show invalid properties with the reasons. Usually used together with valid?
# @return Array for valid properties with the reasons
def list_invalid_properties
invalid_properties = Array.new
invalid_properties = {{^parent}}Array.new{{/parent}}{{#parent}}super{{/parent}}
{{#vars}}
{{#required}}
if @{{{name}}}.nil?
Expand Down Expand Up @@ -260,7 +265,7 @@
def ==(o)
return true if self.equal?(o)
self.class == o.class{{#vars}} &&
{{name}} == o.{{name}}{{/vars}}
{{name}} == o.{{name}}{{/vars}}{{#parent}} && super(o){{/parent}}
end

# @see the `==` method
Expand All @@ -276,4 +281,4 @@
end

{{> base_object}}
end
end