Merge branch 'develop_2.0' of github.com:wordnik/swagger-codegen into develop_2.0

This commit is contained in:
Tony Tam 2014-10-01 23:22:43 -07:00
commit c10cf7bf7f
4 changed files with 42 additions and 4 deletions

View File

@ -18,6 +18,7 @@ public class CodegenProperty {
public Double minimum, maximum, exclusiveMinimum, exclusiveMaximum;
public Boolean hasMore = null, required = null, secondaryParam = null;
public Boolean isPrimitiveType, isContainer, isNotContainer;
public boolean isEnum;
public List<String> _enum;
public Map<String, Object> allowableValues;
}

View File

@ -4,6 +4,7 @@ import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.parameters.*;
import com.wordnik.swagger.models.properties.*;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.io.File;
@ -443,6 +444,7 @@ public class DefaultCodegen {
if(sp.getEnum() != null) {
List<String> _enum = sp.getEnum();
property._enum = _enum;
property.isEnum = true;
// legacy support
Map<String, Object> allowableValues = new HashMap<String, Object>();
@ -451,7 +453,9 @@ public class DefaultCodegen {
}
}
property.datatype = getTypeDeclaration(p);
property.datatype = property.isEnum
? StringUtils.capitalize(property.name) + "Enum"
: getTypeDeclaration(p);
property.baseType = getSwaggerType(p);
if(p instanceof ArrayProperty) {

View File

@ -17,10 +17,10 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{#allowableValues}}
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{#isEnum}}
//{{^min}}public enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };
{{/min}}{{/allowableValues}}{{/vars}}
public enum {{datatype}} { {{#_enum}}{{.}}{{^-last}}, {{/-last}}{{/_enum}} };
{{/isEnum}}{{/vars}}
{{#vars}}
@ApiModelProperty(required = {{required}}, value = "{{{description}}}")

View File

@ -0,0 +1,33 @@
package Java
import com.wordnik.swagger.codegen.languages.JavaClientCodegen
import com.wordnik.swagger.models._
import com.wordnik.swagger.models.properties._
import org.junit.runner.RunWith
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.junit.JUnitRunner
import scala.collection.JavaConverters._
@RunWith(classOf[JUnitRunner])
class JavaModelEnumTest extends FlatSpec with Matchers {
it should "convert a java model with an enum" in {
val enumProperty = new StringProperty()
enumProperty.setEnum(List("VALUE1", "VALUE2", "VALUE3").asJava)
val model = new ModelImpl()
.property("name", enumProperty)
val codegen = new JavaClientCodegen()
val cm = codegen.fromModel("sample", model)
cm.vars.size should be(1)
val enumVar = cm.vars.get(0)
enumVar.baseName should be("name")
enumVar.datatype should be("NameEnum")
enumVar.name should be("name")
enumVar.defaultValue should be("null")
enumVar.baseType should be("String")
enumVar.isEnum should equal(true)
}
}