-
Notifications
You must be signed in to change notification settings - Fork 6k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -61,6 +62,7 @@ public RubyClientCodegen() { | |
// at the moment | ||
importMapping.clear(); | ||
|
||
this.supportsInheritance = true; | ||
modelPackage = "models"; | ||
apiPackage = "api"; | ||
outputFolder = "generated-code" + File.separator + "ruby"; | ||
|
@@ -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); | ||
} | ||
|
||
@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); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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}}} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
end | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}}}') | ||
|
@@ -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? | ||
|
@@ -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 | ||
|
@@ -276,4 +281,4 @@ | |
end | ||
|
||
{{> base_object}} | ||
end | ||
end |
There was a problem hiding this comment.
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