forked from loafle/openapi-generator-original
Fix, tests for Issue #2258 "[JavaScript] Generator options and template improvements"
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package io.swagger.codegen.javascript;
|
||||
|
||||
import io.swagger.codegen.AbstractOptionsTest;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.options.JavaScriptOptionsProvider;
|
||||
import io.swagger.codegen.languages.JavascriptClientCodegen;
|
||||
import io.swagger.codegen.options.OptionsProvider;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class JavaScriptClientOptionsTest extends AbstractOptionsTest {
|
||||
@Tested
|
||||
private JavascriptClientCodegen clientCodegen;
|
||||
|
||||
public JavaScriptClientOptionsTest() {
|
||||
super(new JavaScriptOptionsProvider());
|
||||
}
|
||||
|
||||
protected JavaScriptClientOptionsTest(OptionsProvider optionsProvider) {
|
||||
super(optionsProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
// Commented generic options not yet supported by JavaScript codegen.
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setModelPackage(JavaScriptOptionsProvider.MODEL_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setApiPackage(JavaScriptOptionsProvider.API_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(JavaScriptOptionsProvider.SORT_PARAMS_VALUE));
|
||||
times = 1;
|
||||
// clientCodegen.setInvokerPackage(JavaScriptOptionsProvider.INVOKER_PACKAGE_VALUE);
|
||||
// times = 1;
|
||||
// clientCodegen.setGroupId(JavaScriptOptionsProvider.GROUP_ID_VALUE);
|
||||
// times = 1;
|
||||
// clientCodegen.setArtifactId(JavaScriptOptionsProvider.ARTIFACT_ID_VALUE);
|
||||
// times = 1;
|
||||
// clientCodegen.setArtifactVersion(JavaScriptOptionsProvider.ARTIFACT_VERSION_VALUE);
|
||||
// times = 1;
|
||||
clientCodegen.setSourceFolder(JavaScriptOptionsProvider.SOURCE_FOLDER_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setLocalVariablePrefix(JavaScriptOptionsProvider.LOCAL_PREFIX_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setProjectName(JavaScriptOptionsProvider.PROJECT_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setModuleName(JavaScriptOptionsProvider.MODULE_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setProjectDescription(JavaScriptOptionsProvider.PROJECT_DESCRIPTION_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setProjectVersion(JavaScriptOptionsProvider.PROJECT_VERSION_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setProjectLicenseName(JavaScriptOptionsProvider.PROJECT_LICENSE_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setUsePromises(Boolean.valueOf(JavaScriptOptionsProvider.USE_PROMISES_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setUseInheritance(Boolean.valueOf(JavaScriptOptionsProvider.USE_INHERITANCE_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setEmitModelMethods(Boolean.valueOf(JavaScriptOptionsProvider.EMIT_MODEL_METHODS_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setEmitJSDoc(Boolean.valueOf(JavaScriptOptionsProvider.EMIT_JS_DOC_VALUE));
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package io.swagger.codegen.javascript;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.languages.JavascriptClientCodegen;
|
||||
import io.swagger.models.ComposedModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.RefModel;
|
||||
import io.swagger.models.properties.StringProperty;
|
||||
|
||||
public class JavaScriptInheritanceTest {
|
||||
@SuppressWarnings("static-method")
|
||||
@Test(description = "convert a composed model with inheritance enabled")
|
||||
public void javascriptInheritanceTest() {
|
||||
ModelImpl base = new ModelImpl();
|
||||
base.addProperty("baseProp", new StringProperty().required(true));
|
||||
ModelImpl intf1 = new ModelImpl();
|
||||
intf1.addProperty("intf1Prop", new StringProperty());
|
||||
ModelImpl intf2 = new ModelImpl();
|
||||
intf2.addProperty("intf2Prop", new StringProperty().required(true));
|
||||
ModelImpl child = new ModelImpl();
|
||||
child.addProperty("childProp", new StringProperty().required(true));
|
||||
|
||||
final Map<String, Model> allDefinitions = new HashMap<String, Model>();
|
||||
allDefinitions.put("Base", base);
|
||||
allDefinitions.put("Interface1", intf1);
|
||||
allDefinitions.put("Interface2", intf2);
|
||||
|
||||
final Model model = new ComposedModel().parent(new RefModel("Base"))
|
||||
.interfaces(Arrays.asList(new RefModel("Interface1"), new RefModel("Interface2")))
|
||||
.child(child);
|
||||
|
||||
final JavascriptClientCodegen codegen = new JavascriptClientCodegen();
|
||||
codegen.setUseInheritance(true);
|
||||
|
||||
final CodegenModel cm = codegen.fromModel("sample", model, allDefinitions);
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.parent, "Base");
|
||||
Assert.assertEquals(cm.interfaces, Arrays.asList("Interface1", "Interface2"));
|
||||
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
Assert.assertEquals(cm.vars.get(0).name, "childProp");
|
||||
Assert.assertEquals(cm.allVars.size(), 4);
|
||||
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
|
||||
for (int i = 0; i < allVars.length; i++) {
|
||||
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
|
||||
}
|
||||
Assert.assertEquals(cm.mandatory, Sets.newHashSet("childProp"));
|
||||
Assert.assertEquals(cm.allMandatory, Sets.newHashSet("baseProp", "intf2Prop", "childProp"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
@Test(description = "convert a composed model with inheritance disabled")
|
||||
public void javascriptNoInheritanceTest() {
|
||||
ModelImpl base = new ModelImpl();
|
||||
base.addProperty("baseProp", new StringProperty().required(true));
|
||||
ModelImpl intf1 = new ModelImpl();
|
||||
intf1.addProperty("intf1Prop", new StringProperty());
|
||||
ModelImpl intf2 = new ModelImpl();
|
||||
intf2.addProperty("intf2Prop", new StringProperty().required(true));
|
||||
ModelImpl child = new ModelImpl();
|
||||
child.addProperty("childProp", new StringProperty().required(true));
|
||||
|
||||
final Map<String, Model> allDefinitions = new HashMap<String, Model>();
|
||||
allDefinitions.put("Base", base);
|
||||
allDefinitions.put("Interface1", intf1);
|
||||
allDefinitions.put("Interface2", intf2);
|
||||
|
||||
final Model model = new ComposedModel().parent(new RefModel("Base"))
|
||||
.interfaces(Arrays.asList(new RefModel("Interface1"), new RefModel("Interface2")))
|
||||
.child(child);
|
||||
|
||||
final JavascriptClientCodegen codegen = new JavascriptClientCodegen();
|
||||
codegen.setUseInheritance(false);
|
||||
|
||||
final CodegenModel cm = codegen.fromModel("sample", model, allDefinitions);
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.parent, "Base");
|
||||
Assert.assertEquals(cm.interfaces, Arrays.asList("Interface1", "Interface2"));
|
||||
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
|
||||
Assert.assertEquals(cm.vars.size(), 4);
|
||||
Assert.assertEquals(cm.allVars.size(), 4);
|
||||
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
|
||||
for (int i = 0; i < allVars.length; i++) {
|
||||
Assert.assertEquals(cm.vars.get(i).name, allVars[i]);
|
||||
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
|
||||
}
|
||||
Assert.assertEquals(cm.mandatory, Sets.newHashSet("baseProp", "intf2Prop", "childProp"));
|
||||
Assert.assertEquals(cm.allMandatory, Sets.newHashSet("baseProp", "intf2Prop", "childProp"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.swagger.codegen.javascript;
|
||||
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenProperty;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.languages.JavaClientCodegen;
|
||||
import io.swagger.codegen.languages.JavascriptClientCodegen;
|
||||
import io.swagger.models.ComposedModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.RefModel;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.models.properties.StringProperty;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class JavaScriptModelEnumTest {
|
||||
@Test(description = "convert a JavaScript model with an enum")
|
||||
public void converterTest() {
|
||||
final StringProperty enumProperty = new StringProperty();
|
||||
enumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
|
||||
final ModelImpl model = new ModelImpl().property("name", enumProperty);
|
||||
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty enumVar = cm.vars.get(0);
|
||||
Assert.assertEquals(enumVar.baseName, "name");
|
||||
Assert.assertEquals(enumVar.datatype, "String");
|
||||
Assert.assertEquals(enumVar.datatypeWithEnum, "NameEnum");
|
||||
Assert.assertEquals(enumVar.name, "name");
|
||||
Assert.assertEquals(enumVar.defaultValue, null);
|
||||
Assert.assertEquals(enumVar.baseType, "String");
|
||||
Assert.assertTrue(enumVar.isEnum);
|
||||
}
|
||||
|
||||
@Test(description = "not override identical parent enums")
|
||||
public void overrideEnumTest() {
|
||||
final StringProperty identicalEnumProperty = new StringProperty();
|
||||
identicalEnumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
|
||||
|
||||
final StringProperty subEnumProperty = new StringProperty();
|
||||
subEnumProperty.setEnum(Arrays.asList("SUB1", "SUB2", "SUB3"));
|
||||
|
||||
// Add one enum property to the parent
|
||||
final Map<String, Property> parentProperties = new HashMap<String, Property>();
|
||||
parentProperties.put("sharedThing", identicalEnumProperty);
|
||||
|
||||
// Add TWO enums to the subType model; one of which is identical to the one in parent class
|
||||
final Map<String, Property> subProperties = new HashMap<String, Property>();
|
||||
subProperties.put("sharedThing", identicalEnumProperty);
|
||||
subProperties.put("unsharedThing", identicalEnumProperty);
|
||||
|
||||
final ModelImpl parentModel = new ModelImpl();
|
||||
parentModel.setProperties(parentProperties);
|
||||
parentModel.name("parentModel");
|
||||
|
||||
final ModelImpl subModel = new ModelImpl();
|
||||
subModel.setProperties(subProperties);
|
||||
subModel.name("subModel");
|
||||
|
||||
final ComposedModel model = new ComposedModel()
|
||||
.parent(new RefModel(parentModel.getName()))
|
||||
.child(subModel)
|
||||
.interfaces(new ArrayList<RefModel>());
|
||||
|
||||
final DefaultCodegen codegen = new JavaClientCodegen();
|
||||
final Map<String, Model> allModels = new HashMap<String, Model>();
|
||||
allModels.put(parentModel.getName(), parentModel);
|
||||
allModels.put(subModel.getName(), subModel);
|
||||
|
||||
final CodegenModel cm = codegen.fromModel("sample", model, allModels);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.parent, "ParentModel");
|
||||
Assert.assertTrue(cm.imports.contains("ParentModel"));
|
||||
|
||||
// Assert that only the unshared/uninherited enum remains
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
final CodegenProperty enumVar = cm.vars.get(0);
|
||||
Assert.assertEquals(enumVar.baseName, "unsharedThing");
|
||||
Assert.assertEquals(enumVar.datatype, "String");
|
||||
Assert.assertEquals(enumVar.datatypeWithEnum, "UnsharedThingEnum");
|
||||
Assert.assertTrue(enumVar.isEnum);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,491 @@
|
||||
package io.swagger.codegen.javascript;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenProperty;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.languages.JavascriptClientCodegen;
|
||||
import io.swagger.models.ArrayModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.parameters.QueryParameter;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.ByteArrayProperty;
|
||||
import io.swagger.models.properties.DateTimeProperty;
|
||||
import io.swagger.models.properties.IntegerProperty;
|
||||
import io.swagger.models.properties.LongProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.RefProperty;
|
||||
import io.swagger.models.properties.StringProperty;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class JavaScriptModelTest {
|
||||
@Test(description = "convert a simple java model")
|
||||
public void simpleModelTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("id", new LongProperty())
|
||||
.property("name", new StringProperty()
|
||||
.example("Tony"))
|
||||
.property("createdAt", new DateTimeProperty())
|
||||
.required("id")
|
||||
.required("name");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 3);
|
||||
|
||||
final List<CodegenProperty> vars = cm.vars;
|
||||
|
||||
final CodegenProperty property1 = vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "id");
|
||||
Assert.assertEquals(property1.getter, "getId");
|
||||
Assert.assertEquals(property1.setter, "setId");
|
||||
Assert.assertEquals(property1.datatype, "Integer");
|
||||
Assert.assertEquals(property1.name, "id");
|
||||
Assert.assertEquals(property1.defaultValue, null);
|
||||
Assert.assertEquals(property1.baseType, "Integer");
|
||||
Assert.assertTrue(property1.hasMore);
|
||||
Assert.assertTrue(property1.required);
|
||||
Assert.assertTrue(property1.isNotContainer);
|
||||
|
||||
final CodegenProperty property2 = vars.get(1);
|
||||
Assert.assertEquals(property2.baseName, "name");
|
||||
Assert.assertEquals(property2.getter, "getName");
|
||||
Assert.assertEquals(property2.setter, "setName");
|
||||
Assert.assertEquals(property2.datatype, "String");
|
||||
Assert.assertEquals(property2.name, "name");
|
||||
Assert.assertEquals(property2.defaultValue, null);
|
||||
Assert.assertEquals(property2.baseType, "String");
|
||||
Assert.assertEquals(property2.example, "Tony");
|
||||
Assert.assertTrue(property2.hasMore);
|
||||
Assert.assertTrue(property2.required);
|
||||
Assert.assertTrue(property2.isNotContainer);
|
||||
|
||||
final CodegenProperty property3 = vars.get(2);
|
||||
Assert.assertEquals(property3.baseName, "createdAt");
|
||||
Assert.assertEquals(property3.getter, "getCreatedAt");
|
||||
Assert.assertEquals(property3.setter, "setCreatedAt");
|
||||
Assert.assertEquals(property3.datatype, "Date");
|
||||
Assert.assertEquals(property3.name, "createdAt");
|
||||
Assert.assertEquals(property3.defaultValue, null);
|
||||
Assert.assertEquals(property3.baseType, "Date");
|
||||
Assert.assertNull(property3.hasMore);
|
||||
Assert.assertNull(property3.required);
|
||||
Assert.assertTrue(property3.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with list property")
|
||||
public void listPropertyTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("id", new LongProperty())
|
||||
.property("urls", new ArrayProperty()
|
||||
.items(new StringProperty()))
|
||||
.required("id");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 2);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(1);
|
||||
Assert.assertEquals(property.baseName, "urls");
|
||||
Assert.assertEquals(property.getter, "getUrls");
|
||||
Assert.assertEquals(property.setter, "setUrls");
|
||||
Assert.assertEquals(property.datatype, "[String]");
|
||||
Assert.assertEquals(property.name, "urls");
|
||||
// FIXME: should an array property have an empty array as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"[]"*/null);
|
||||
Assert.assertEquals(property.baseType, "Array");
|
||||
Assert.assertEquals(property.containerType, "array");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with a map property")
|
||||
public void mapPropertyTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("translations", new MapProperty()
|
||||
.additionalProperties(new StringProperty()))
|
||||
.required("id");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "translations");
|
||||
Assert.assertEquals(property.getter, "getTranslations");
|
||||
Assert.assertEquals(property.setter, "setTranslations");
|
||||
Assert.assertEquals(property.datatype, "{String: String}");
|
||||
Assert.assertEquals(property.name, "translations");
|
||||
// FIXME: should a map property have an empty object as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"{}"*/null);
|
||||
Assert.assertEquals(property.baseType, "Object");
|
||||
Assert.assertEquals(property.containerType, "map");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with a map with complex list property")
|
||||
public void mapWithListPropertyTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("translations",
|
||||
new MapProperty().additionalProperties(new ArrayProperty().items(new RefProperty("Pet"))))
|
||||
.required("id");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "translations");
|
||||
Assert.assertEquals(property.getter, "getTranslations");
|
||||
Assert.assertEquals(property.setter, "setTranslations");
|
||||
Assert.assertEquals(property.datatype, "{String: [Pet]}");
|
||||
Assert.assertEquals(property.name, "translations");
|
||||
// FIXME: should a map property have an empty object as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"{}"*/null);
|
||||
Assert.assertEquals(property.baseType, "Object");
|
||||
Assert.assertEquals(property.containerType, "map");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with a 2D list property")
|
||||
public void list2DPropertyTest() {
|
||||
final Model model = new ModelImpl().name("sample").property("list2D", new ArrayProperty().items(
|
||||
new ArrayProperty().items(new RefProperty("Pet"))));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "list2D");
|
||||
Assert.assertEquals(property.getter, "getList2D");
|
||||
Assert.assertEquals(property.setter, "setList2D");
|
||||
Assert.assertEquals(property.datatype, "[[Pet]]");
|
||||
Assert.assertEquals(property.name, "list2D");
|
||||
// FIXME: should an array property have an empty array as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"[]"*/null);
|
||||
Assert.assertEquals(property.baseType, "Array");
|
||||
Assert.assertEquals(property.containerType, "array");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with complex properties")
|
||||
public void complexPropertiesTest() {
|
||||
final Model model = new ModelImpl().description("a sample model")
|
||||
.property("children", new RefProperty("#/definitions/Children"));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "children");
|
||||
Assert.assertEquals(property.getter, "getChildren");
|
||||
Assert.assertEquals(property.setter, "setChildren");
|
||||
Assert.assertEquals(property.datatype, "Children");
|
||||
Assert.assertEquals(property.name, "children");
|
||||
Assert.assertEquals(property.defaultValue, null);
|
||||
Assert.assertEquals(property.baseType, "Children");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with complex list property")
|
||||
public void complexListPropertyTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("children", new ArrayProperty().items(new RefProperty("#/definitions/Children")));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "children");
|
||||
Assert.assertEquals(property.complexType, "Children");
|
||||
Assert.assertEquals(property.getter, "getChildren");
|
||||
Assert.assertEquals(property.setter, "setChildren");
|
||||
// FIXME: what should datatype be for a JavaScript array?
|
||||
// Assert.assertEquals(property.datatype, "Array<Children>");
|
||||
Assert.assertEquals(property.name, "children");
|
||||
// FIXME: should an array property have an empty array as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"[]"*/null);
|
||||
Assert.assertEquals(property.baseType, "Array");
|
||||
Assert.assertEquals(property.containerType, "array");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with complex map property")
|
||||
public void complexMapPropertyTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("children", new MapProperty().additionalProperties(new RefProperty("#/definitions/Children")));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "a sample model");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "children");
|
||||
Assert.assertEquals(property.complexType, "Children");
|
||||
Assert.assertEquals(property.getter, "getChildren");
|
||||
Assert.assertEquals(property.setter, "setChildren");
|
||||
// TODO: create a functional test to see whether map properties actually work.
|
||||
Assert.assertEquals(property.datatype, /*"Object<String, Children>"*/"{String: Children}");
|
||||
Assert.assertEquals(property.name, "children");
|
||||
// FIXME: should a map property have an empty object as its default value? What if the property is required?
|
||||
Assert.assertEquals(property.defaultValue, /*"{}"*/ null);
|
||||
Assert.assertEquals(property.baseType, "Object");
|
||||
Assert.assertEquals(property.containerType, "map");
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
Assert.assertNull(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert an array model")
|
||||
public void arrayModelTest() {
|
||||
final Model model = new ArrayModel()
|
||||
.description("an array model")
|
||||
.items(new RefProperty("#/definitions/Children"));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "an array model");
|
||||
Assert.assertEquals(cm.vars.size(), 0);
|
||||
Assert.assertEquals(cm.parent, "Array<Children>");
|
||||
Assert.assertEquals(cm.imports.size(), 1);
|
||||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
|
||||
}
|
||||
|
||||
@Test(description = "convert a map model")
|
||||
public void mapModelTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("an map model")
|
||||
.additionalProperties(new RefProperty("#/definitions/Children"));
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.description, "an map model");
|
||||
Assert.assertEquals(cm.vars.size(), 0);
|
||||
Assert.assertEquals(cm.parent, "Object<String, Children>");
|
||||
Assert.assertEquals(cm.imports.size(), 1);
|
||||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with uppercase property names")
|
||||
public void upperCaseNamesTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a model with uppercase property names")
|
||||
.property("NAME", new StringProperty())
|
||||
.required("NAME");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "NAME");
|
||||
Assert.assertEquals(property.getter, "getNAME");
|
||||
Assert.assertEquals(property.setter, "setNAME");
|
||||
Assert.assertEquals(property.datatype, "String");
|
||||
Assert.assertEquals(property.name, "NAME");
|
||||
Assert.assertEquals(property.defaultValue, null);
|
||||
Assert.assertEquals(property.baseType, "String");
|
||||
Assert.assertNull(property.hasMore);
|
||||
Assert.assertTrue(property.required);
|
||||
Assert.assertTrue(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with a 2nd char uppercase property names")
|
||||
public void secondCharUpperCaseNamesTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a model with a 2nd char uppercase property names")
|
||||
.property("pId", new StringProperty())
|
||||
.required("pId");
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "pId");
|
||||
Assert.assertEquals(property.getter, "getPId");
|
||||
Assert.assertEquals(property.setter, "setPId");
|
||||
Assert.assertEquals(property.datatype, "String");
|
||||
Assert.assertEquals(property.name, "pId");
|
||||
Assert.assertEquals(property.defaultValue, null);
|
||||
Assert.assertEquals(property.baseType, "String");
|
||||
Assert.assertNull(property.hasMore);
|
||||
Assert.assertTrue(property.required);
|
||||
Assert.assertTrue(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert hyphens per issue 503")
|
||||
public void hyphensTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("created-at", new DateTimeProperty());
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "created-at");
|
||||
Assert.assertEquals(property.getter, "getCreatedAt");
|
||||
Assert.assertEquals(property.setter, "setCreatedAt");
|
||||
Assert.assertEquals(property.name, "createdAt");
|
||||
}
|
||||
|
||||
@Test(description = "convert query[password] to queryPassword")
|
||||
public void squareBracketsTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("query[password]", new StringProperty());
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "query[password]");
|
||||
Assert.assertEquals(property.getter, "getQueryPassword");
|
||||
Assert.assertEquals(property.setter, "setQueryPassword");
|
||||
Assert.assertEquals(property.name, "queryPassword");
|
||||
}
|
||||
|
||||
@Test(description = "properly escape names per 567")
|
||||
public void escapeNamesTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
.property("created-at", new DateTimeProperty());
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("with.dots", model);
|
||||
|
||||
Assert.assertEquals(cm.classname, "WithDots");
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with binary data")
|
||||
public void binaryDataTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("model with binary")
|
||||
.property("inputBinaryData", new ByteArrayProperty());
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "inputBinaryData");
|
||||
Assert.assertEquals(property.getter, "getInputBinaryData");
|
||||
Assert.assertEquals(property.setter, "setInputBinaryData");
|
||||
Assert.assertEquals(property.datatype, "String");
|
||||
Assert.assertEquals(property.name, "inputBinaryData");
|
||||
Assert.assertEquals(property.defaultValue, null);
|
||||
Assert.assertEquals(property.baseType, "String");
|
||||
Assert.assertNull(property.hasMore);
|
||||
Assert.assertNull(property.required);
|
||||
Assert.assertTrue(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "translate an invalid param name")
|
||||
public void invalidParamNameTest() {
|
||||
final Model model = new ModelImpl()
|
||||
.description("a model with a 2nd char uppercase property name")
|
||||
.property("_", new StringProperty());
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
|
||||
final CodegenProperty property = cm.vars.get(0);
|
||||
Assert.assertEquals(property.baseName, "_");
|
||||
Assert.assertEquals(property.getter, "getU");
|
||||
Assert.assertEquals(property.setter, "setU");
|
||||
Assert.assertEquals(property.datatype, "String");
|
||||
Assert.assertEquals(property.name, "u");
|
||||
Assert.assertEquals(property.defaultValue, null);
|
||||
Assert.assertEquals(property.baseType, "String");
|
||||
Assert.assertNull(property.hasMore);
|
||||
Assert.assertTrue(property.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a parameter")
|
||||
public void convertParameterTest() {
|
||||
final QueryParameter parameter = new QueryParameter()
|
||||
.property(new IntegerProperty())
|
||||
.name("limit")
|
||||
.required(true);
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenParameter cm = codegen.fromParameter(parameter, null);
|
||||
|
||||
Assert.assertNull(cm.allowableValues);
|
||||
}
|
||||
|
||||
@DataProvider(name = "modelNames")
|
||||
public static Object[][] primeNumbers() {
|
||||
return new Object[][] {
|
||||
{"sample", "Sample"},
|
||||
{"sample_name", "SampleName"},
|
||||
{"sample__name", "SampleName"},
|
||||
{"/sample", "Sample"},
|
||||
{"\\sample", "Sample"},
|
||||
{"sample.name", "SampleName"},
|
||||
{"_sample", "Sample"},
|
||||
{"Sample", "Sample"},
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "modelNames", description = "avoid inner class")
|
||||
public void modelNameTest(String name, String expectedName) {
|
||||
final Model model = new ModelImpl();
|
||||
final DefaultCodegen codegen = new JavascriptClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel(name, model);
|
||||
|
||||
Assert.assertEquals(cm.name, name);
|
||||
Assert.assertEquals(cm.classname, expectedName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.swagger.codegen.options;
|
||||
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.options.OptionsProvider;
|
||||
import io.swagger.codegen.languages.JavascriptClientCodegen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaScriptOptionsProvider implements OptionsProvider {
|
||||
public static final String ARTIFACT_ID_VALUE = "swagger-javascript-client-test";
|
||||
public static final String MODEL_PACKAGE_VALUE = "model";
|
||||
public static final String API_PACKAGE_VALUE = "api";
|
||||
// public static final String INVOKER_PACKAGE_VALUE = "js";
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
public static final String GROUP_ID_VALUE = "io.swagger.test";
|
||||
public static final String ARTIFACT_VERSION_VALUE = "1.0.0-SNAPSHOT";
|
||||
public static final String SOURCE_FOLDER_VALUE = "src/main/javascript";
|
||||
public static final String LOCAL_PREFIX_VALUE = "_";
|
||||
// public static final String SERIALIZABLE_MODEL_VALUE = "false";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
|
||||
public static final String PROJECT_NAME_VALUE = "JavaScript Client Test";
|
||||
public static final String MODULE_NAME_VALUE = "JavaScriptClient";
|
||||
public static final String PROJECT_DESCRIPTION_VALUE = "Tests JavaScript code generator options";
|
||||
public static final String PROJECT_VERSION_VALUE = "1.0.0";
|
||||
public static final String PROJECT_LICENSE_NAME_VALUE = "Apache";
|
||||
public static final String USE_PROMISES_VALUE = "true";
|
||||
public static final String USE_INHERITANCE_VALUE = "false";
|
||||
public static final String EMIT_MODEL_METHODS_VALUE = "true";
|
||||
public static final String EMIT_JS_DOC_VALUE = "false";
|
||||
|
||||
private ImmutableMap<String, String> options;
|
||||
|
||||
/**
|
||||
* Create an options provider with the default options.
|
||||
*/
|
||||
public JavaScriptOptionsProvider() {
|
||||
// Commented generic options not yet supported by JavaScript codegen.
|
||||
options = new ImmutableMap.Builder<String, String>()
|
||||
.put(CodegenConstants.MODEL_PACKAGE, MODEL_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.API_PACKAGE, API_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
// .put(CodegenConstants.INVOKER_PACKAGE, INVOKER_PACKAGE_VALUE)
|
||||
// .put(CodegenConstants.GROUP_ID, GROUP_ID_VALUE)
|
||||
// .put(CodegenConstants.ARTIFACT_ID, ARTIFACT_ID_VALUE)
|
||||
// .put(CodegenConstants.ARTIFACT_VERSION, ARTIFACT_VERSION_VALUE)
|
||||
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
|
||||
.put(CodegenConstants.LOCAL_VARIABLE_PREFIX, LOCAL_PREFIX_VALUE)
|
||||
// .put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
|
||||
.put(JavascriptClientCodegen.PROJECT_NAME, PROJECT_NAME_VALUE)
|
||||
.put(JavascriptClientCodegen.MODULE_NAME, MODULE_NAME_VALUE)
|
||||
.put(JavascriptClientCodegen.PROJECT_DESCRIPTION, PROJECT_DESCRIPTION_VALUE)
|
||||
.put(JavascriptClientCodegen.PROJECT_VERSION, PROJECT_VERSION_VALUE)
|
||||
.put(JavascriptClientCodegen.PROJECT_LICENSE_NAME, PROJECT_LICENSE_NAME_VALUE)
|
||||
.put(JavascriptClientCodegen.USE_PROMISES, USE_PROMISES_VALUE)
|
||||
.put(JavascriptClientCodegen.USE_INHERITANCE, USE_INHERITANCE_VALUE)
|
||||
.put(JavascriptClientCodegen.EMIT_MODEL_METHODS, EMIT_MODEL_METHODS_VALUE)
|
||||
.put(JavascriptClientCodegen.EMIT_JS_DOC, EMIT_JS_DOC_VALUE)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the default options, but override the ones found in additionalOptions.
|
||||
*/
|
||||
public JavaScriptOptionsProvider(Map<String, String> additionalOptions) {
|
||||
options = new ImmutableMap.Builder<String, String>()
|
||||
.putAll(options)
|
||||
.putAll(additionalOptions)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return "javascript";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user