Add default value to Ruby model

This commit is contained in:
xhh 2015-12-08 17:56:06 +08:00
parent a866c7fef4
commit 21bd4fbbc1
7 changed files with 47 additions and 14 deletions

View File

@ -6,9 +6,7 @@ import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenType; import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile; import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.*;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -186,6 +184,43 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
return super.getTypeDeclaration(p); return super.getTypeDeclaration(p);
} }
@Override
public String toDefaultValue(Property p) {
if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof BooleanProperty) {
BooleanProperty bp = (BooleanProperty) p;
if (bp.getDefault() != null) {
return bp.getDefault().toString();
}
} else if (p instanceof StringProperty) {
StringProperty sp = (StringProperty) p;
if (sp.getDefault() != null) {
return "\"" + escapeText(sp.getDefault()) + "\"";
}
}
return null;
}
@Override @Override
public String getSwaggerType(Property p) { public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p); String swaggerType = super.getSwaggerType(p);
@ -204,10 +239,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
return type; return type;
} }
public String toDefaultValue(Property p) {
return "null";
}
@Override @Override
public String toVarName(String name) { public String toVarName(String name) {
// sanitize name // sanitize name

View File

@ -21,7 +21,7 @@ module {{moduleName}}
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
@ -30,7 +30,9 @@ module {{moduleName}}
if attributes[:'{{{baseName}}}'] if attributes[:'{{{baseName}}}']
{{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array) {{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
self.{{{name}}} = value self.{{{name}}} = value
end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}} end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}}{{#defaultValue}}
else
self.{{{name}}} = {{{defaultValue}}}{{/defaultValue}}
end end
{{/vars}} {{/vars}}
end end

View File

@ -25,7 +25,7 @@ module Petstore
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

View File

@ -41,7 +41,7 @@ module Petstore
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

View File

@ -41,7 +41,7 @@ module Petstore
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

View File

@ -25,7 +25,7 @@ module Petstore
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

View File

@ -49,7 +49,7 @@ module Petstore
end end
def initialize(attributes = {}) def initialize(attributes = {})
return if !attributes.is_a?(Hash) || attributes.empty? return unless attributes.is_a?(Hash)
# convert string to symbol for hash key # convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}