Skip to content

Commit

Permalink
Merge pull request #1671 from jfiala/master
Browse files Browse the repository at this point in the history
Add basic Javascript SDK support
  • Loading branch information
wing328 committed Dec 7, 2015
2 parents 19b409e + b0e3be8 commit e2679b2
Show file tree
Hide file tree
Showing 10 changed files with 780 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/swagger-codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.settings/
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class CodegenModel {
public String unescapedDescription;
public String defaultValue;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>();

// list of all required parameters
public Set<String> mandatory = new HashSet<String>();

public Set<String> imports = new HashSet<String>();
public Boolean hasVars, emptyVars, hasMoreModels, hasEnums;
public ExternalDocs externalDocs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class CodegenProperty {
public String baseName, complexType, getter, setter, description, datatype, datatypeWithEnum,
name, min, max, defaultValue, baseType, containerType;
name, min, max, defaultValue, defaultValueWithParam, baseType, containerType;

public String unescapedDescription;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Function;
import com.google.common.collect.Lists;

import io.swagger.codegen.examples.ExampleGenerator;
import io.swagger.models.ArrayModel;
import io.swagger.models.ComposedModel;
Expand Down Expand Up @@ -42,11 +43,13 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.Json;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -574,6 +577,52 @@ public String toDefaultValue(Property p) {
return "null";
}
}

/**
* Return the property initialized from a data object
* Useful for initialization with a plain object in Javascript
*
* @param name Name of the property object
* @param p Swagger property object
* @return string presentation of the default value of the property
*/
public String toDefaultValueWithParam(String name, Property p) {
if (p instanceof StringProperty) {
return " = data." + name + ";";
} else if (p instanceof BooleanProperty) {
return " = data." + name + ";";
} else if (p instanceof DateProperty) {
return " = data." + name + ";";
} else if (p instanceof DateTimeProperty) {
return " = data." + name + ";";
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return " = data." + name + ";";
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return " = data." + name + ";";
} else if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return " = data." + name + ";";
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return " = data." + name + ";";
} else {
return " = data." + name + ";";
}
}

/**
* returns the swagger type for the property
Expand Down Expand Up @@ -835,6 +884,8 @@ public CodegenProperty fromProperty(String name, Property p) {
property.setter = "set" + getterAndSetterCapitalize(name);
property.example = p.getExample();
property.defaultValue = toDefaultValue(p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);

property.jsonSchema = Json.pretty(p);
property.isReadOnly = p.getReadOnly();

Expand Down Expand Up @@ -1830,6 +1881,9 @@ private void addVars(CodegenModel m, Map<String, Property> properties, Collectio
m.vars.add(cp);
}
}

m.mandatory = mandatory;

} else {
m.emptyVars = true;
m.hasVars = false;
Expand Down
Loading

0 comments on commit e2679b2

Please sign in to comment.