Skip to content

Commit f61244b

Browse files
committed
Add tests
1 parent e579027 commit f61244b

File tree

4 files changed

+287
-19
lines changed

4 files changed

+287
-19
lines changed

src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptFetchClientCodegen.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
import io.swagger.v3.oas.models.media.MapSchema;
1414
import io.swagger.v3.oas.models.media.ObjectSchema;
1515
import io.swagger.v3.oas.models.media.Schema;
16+
import io.swagger.v3.parser.util.SchemaTypeUtil;
1617

1718
import org.apache.commons.lang3.StringUtils;
18-
// import org.slf4j.Logger;
19-
// import org.slf4j.LoggerFactory;
2019

2120
public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen {
22-
23-
// private static Logger LOGGER =
24-
// LoggerFactory.getLogger(TypeScriptAngularClientCodegen.class);
25-
2621
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
2722

2823
public static final String NPM_NAME = "npmName";
@@ -43,14 +38,12 @@ public TypeScriptFetchClientCodegen() {
4338
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
4439
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
4540
"Use this property to set an url your private npmRepo in the package.json"));
46-
// this.cliOptions.add(new CliOption(SNAPSHOT,
47-
// "When setting this property to true the version will be suffixed with
48-
// -SNAPSHOT.yyyyMMddHHmm",
49-
// BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
50-
// this.cliOptions.add(new CliOption(WITH_INTERFACES,
51-
// "Setting this property to true will generate interfaces next to the default
52-
// class implementations.",
53-
// BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
41+
this.cliOptions.add(new CliOption(SNAPSHOT,
42+
"When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm",
43+
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
44+
this.cliOptions.add(new CliOption(WITH_INTERFACES,
45+
"Setting this property to true will generate interfaces next to the default class implementations.",
46+
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
5447
}
5548

5649
@Override
@@ -175,11 +168,6 @@ private void addNpmPackageGeneration() {
175168
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
176169
}
177170

178-
private String getIndexDirectory() {
179-
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
180-
return indexPackage.replace('.', File.separatorChar);
181-
}
182-
183171
public String getNpmName() {
184172
return npmName;
185173
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.swagger.codegen.v3.generators.options;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import io.swagger.codegen.v3.CodegenConstants;
5+
import io.swagger.codegen.v3.generators.typescript.TypeScriptAngularClientCodegen;
6+
7+
import java.util.Map;
8+
9+
public class TypeScriptFetchClientOptionsProvider implements OptionsProvider {
10+
public static final String SUPPORTS_ES6_VALUE = "false";
11+
public static final String SORT_PARAMS_VALUE = "false";
12+
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
13+
public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase";
14+
private static final String NMP_NAME = "npmName";
15+
private static final String NMP_VERSION = "1.1.2";
16+
private static final String NPM_REPOSITORY = "https://registry.npmjs.org";
17+
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
18+
private static final String PROVIDED_IN_ROOT = "true";
19+
public static final String NG_VERSION = "2";
20+
21+
@Override
22+
public String getLanguage() {
23+
return "typescript-fetch";
24+
}
25+
26+
@Override
27+
public Map<String, String> createOptions() {
28+
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
29+
return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
30+
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
31+
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE)
32+
.put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE)
33+
.put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME)
34+
.put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION)
35+
.put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
36+
.put(TypeScriptAngularClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
37+
.put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY)
38+
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE).build();
39+
}
40+
41+
@Override
42+
public boolean isServer() {
43+
return false;
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.swagger.codegen.v3.generators.typescript.fetch;
2+
3+
import io.swagger.codegen.v3.CodegenConfig;
4+
import io.swagger.codegen.v3.generators.AbstractOptionsTest;
5+
import io.swagger.codegen.v3.generators.options.TypeScriptFetchClientOptionsProvider;
6+
import io.swagger.codegen.v3.generators.typescript.TypeScriptFetchClientCodegen;
7+
import mockit.Expectations;
8+
import mockit.Tested;
9+
10+
public class TypeScriptFetchClientOptionsTest extends AbstractOptionsTest {
11+
12+
@Tested
13+
private TypeScriptFetchClientCodegen clientCodegen;
14+
15+
public TypeScriptFetchClientOptionsTest() {
16+
super(new TypeScriptFetchClientOptionsProvider());
17+
}
18+
19+
@Override
20+
protected CodegenConfig getCodegenConfig() {
21+
return clientCodegen;
22+
}
23+
24+
@SuppressWarnings("unused")
25+
@Override
26+
protected void setExpectations() {
27+
new Expectations(clientCodegen) {
28+
{
29+
clientCodegen.setSortParamsByRequiredFlag(
30+
Boolean.valueOf(TypeScriptFetchClientOptionsProvider.SORT_PARAMS_VALUE));
31+
times = 1;
32+
clientCodegen.setModelPropertyNaming(TypeScriptFetchClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE);
33+
times = 1;
34+
clientCodegen.setSupportsES6(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.SUPPORTS_ES6_VALUE));
35+
times = 1;
36+
}
37+
};
38+
}
39+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package io.swagger.codegen.v3.generators.typescript.fetch;
2+
3+
import com.google.common.collect.Sets;
4+
import io.swagger.codegen.v3.CodegenConstants;
5+
import io.swagger.codegen.v3.CodegenModel;
6+
import io.swagger.codegen.v3.CodegenProperty;
7+
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
8+
import io.swagger.codegen.v3.generators.typescript.TypeScriptFetchClientCodegen;
9+
import io.swagger.v3.oas.models.Components;
10+
import io.swagger.v3.oas.models.OpenAPI;
11+
import io.swagger.v3.oas.models.media.ArraySchema;
12+
import io.swagger.v3.oas.models.media.DateSchema;
13+
import io.swagger.v3.oas.models.media.DateTimeSchema;
14+
import io.swagger.v3.oas.models.media.IntegerSchema;
15+
import io.swagger.v3.oas.models.media.MapSchema;
16+
import io.swagger.v3.oas.models.media.Schema;
17+
import io.swagger.v3.oas.models.media.StringSchema;
18+
import io.swagger.v3.parser.util.SchemaTypeUtil;
19+
import org.testng.Assert;
20+
import org.testng.annotations.Test;
21+
22+
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
23+
24+
@SuppressWarnings("static-method")
25+
public class TypeScriptFetchModelTest /** extends AbstractCodegenTest */
26+
{
27+
28+
@Test(description = "convert a simple TypeScript Fetch model")
29+
public void simpleModelTest() {
30+
final Schema model = new Schema().description("a sample model")
31+
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
32+
.addProperties("name", new StringSchema()).addProperties("createdAt", new DateTimeSchema())
33+
.addProperties("birthDate", new DateSchema()).addRequiredItem("id").addRequiredItem("name");
34+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
35+
final CodegenModel cm = codegen.fromModel("sample", model);
36+
37+
Assert.assertEquals(cm.name, "sample");
38+
Assert.assertEquals(cm.classname, "Sample");
39+
Assert.assertEquals(cm.description, "a sample model");
40+
Assert.assertEquals(cm.vars.size(), 4);
41+
42+
final CodegenProperty property1 = cm.vars.get(0);
43+
Assert.assertEquals(property1.baseName, "id");
44+
Assert.assertEquals(property1.datatype, "number");
45+
Assert.assertEquals(property1.name, "id");
46+
Assert.assertEquals(property1.defaultValue, "undefined");
47+
Assert.assertEquals(property1.baseType, "number");
48+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.HAS_MORE_EXT_NAME));
49+
Assert.assertTrue(property1.required);
50+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
51+
52+
final CodegenProperty property2 = cm.vars.get(1);
53+
Assert.assertEquals(property2.baseName, "name");
54+
Assert.assertEquals(property2.datatype, "string");
55+
Assert.assertEquals(property2.name, "name");
56+
Assert.assertEquals(property2.defaultValue, "undefined");
57+
Assert.assertEquals(property2.baseType, "string");
58+
Assert.assertTrue(getBooleanValue(property2, CodegenConstants.HAS_MORE_EXT_NAME));
59+
Assert.assertTrue(property2.required);
60+
Assert.assertTrue(getBooleanValue(property2, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
61+
62+
final CodegenProperty property3 = cm.vars.get(2);
63+
Assert.assertEquals(property3.baseName, "createdAt");
64+
Assert.assertEquals(property3.complexType, null);
65+
Assert.assertEquals(property3.datatype, "Date");
66+
Assert.assertEquals(property3.name, "createdAt");
67+
Assert.assertEquals(property3.baseType, "Date");
68+
Assert.assertEquals(property3.defaultValue, "undefined");
69+
Assert.assertTrue(getBooleanValue(property3, CodegenConstants.HAS_MORE_EXT_NAME));
70+
Assert.assertFalse(property3.required);
71+
Assert.assertTrue(getBooleanValue(property3, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
72+
73+
final CodegenProperty property4 = cm.vars.get(3);
74+
Assert.assertEquals(property4.baseName, "birthDate");
75+
Assert.assertEquals(property4.complexType, null);
76+
Assert.assertEquals(property4.datatype, "string");
77+
Assert.assertEquals(property4.name, "birthDate");
78+
Assert.assertEquals(property4.baseType, "string");
79+
Assert.assertEquals(property4.defaultValue, "undefined");
80+
Assert.assertFalse(getBooleanValue(property4, CodegenConstants.HAS_MORE_EXT_NAME));
81+
Assert.assertFalse(property4.required);
82+
Assert.assertTrue(getBooleanValue(property4, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
83+
84+
}
85+
86+
@Test(description = "convert a model with list property")
87+
public void listPropertyTest() {
88+
final Schema model = new Schema().description("a sample model")
89+
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
90+
.addProperties("urls", new ArraySchema().items(new StringSchema())).addRequiredItem("id");
91+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
92+
final CodegenModel cm = codegen.fromModel("sample", model);
93+
94+
Assert.assertEquals(cm.name, "sample");
95+
Assert.assertEquals(cm.classname, "Sample");
96+
Assert.assertEquals(cm.description, "a sample model");
97+
Assert.assertEquals(cm.vars.size(), 2);
98+
99+
final CodegenProperty property1 = cm.vars.get(0);
100+
Assert.assertEquals(property1.baseName, "id");
101+
Assert.assertEquals(property1.datatype, "number");
102+
Assert.assertEquals(property1.name, "id");
103+
Assert.assertEquals(property1.defaultValue, "undefined");
104+
Assert.assertEquals(property1.baseType, "number");
105+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.HAS_MORE_EXT_NAME));
106+
Assert.assertTrue(property1.required);
107+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
108+
109+
final CodegenProperty property2 = cm.vars.get(1);
110+
Assert.assertEquals(property2.baseName, "urls");
111+
Assert.assertEquals(property2.datatype, "Array<string>");
112+
Assert.assertEquals(property2.name, "urls");
113+
Assert.assertEquals(property2.baseType, "Array");
114+
Assert.assertFalse(getBooleanValue(property2, CodegenConstants.HAS_MORE_EXT_NAME));
115+
Assert.assertFalse(property2.required);
116+
Assert.assertFalse(getBooleanValue(property2, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
117+
}
118+
119+
@Test(description = "convert a model with complex property")
120+
public void complexPropertyTest() {
121+
final Schema model = new Schema().description("a sample model").addProperties("children",
122+
new Schema().$ref("#/components/schemas/Children"));
123+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
124+
codegen.preprocessOpenAPI(new OpenAPI().components(new Components()));
125+
final CodegenModel cm = codegen.fromModel("sample", model);
126+
127+
Assert.assertEquals(cm.name, "sample");
128+
Assert.assertEquals(cm.classname, "Sample");
129+
Assert.assertEquals(cm.description, "a sample model");
130+
Assert.assertEquals(cm.vars.size(), 1);
131+
132+
final CodegenProperty property1 = cm.vars.get(0);
133+
Assert.assertEquals(property1.baseName, "children");
134+
Assert.assertEquals(property1.datatype, "Children");
135+
Assert.assertEquals(property1.name, "children");
136+
Assert.assertEquals(property1.defaultValue, "undefined");
137+
Assert.assertEquals(property1.baseType, "Children");
138+
Assert.assertFalse(property1.required);
139+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
140+
}
141+
142+
@Test(description = "convert a model with complex list property")
143+
public void complexListPropertyTest() {
144+
final Schema model = new Schema().description("a sample model").addProperties("children",
145+
new ArraySchema().items(new Schema().$ref("#/components/schemas/Children")));
146+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
147+
codegen.preprocessOpenAPI(new OpenAPI().components(new Components()));
148+
final CodegenModel cm = codegen.fromModel("sample", model);
149+
150+
Assert.assertEquals(cm.name, "sample");
151+
Assert.assertEquals(cm.classname, "Sample");
152+
Assert.assertEquals(cm.description, "a sample model");
153+
Assert.assertEquals(cm.vars.size(), 1);
154+
155+
final CodegenProperty property1 = cm.vars.get(0);
156+
Assert.assertEquals(property1.baseName, "children");
157+
Assert.assertEquals(property1.complexType, "Children");
158+
Assert.assertEquals(property1.datatype, "Array<Children>");
159+
Assert.assertEquals(property1.name, "children");
160+
Assert.assertEquals(property1.baseType, "Array");
161+
Assert.assertFalse(property1.required);
162+
Assert.assertTrue(getBooleanValue(property1, CodegenConstants.IS_CONTAINER_EXT_NAME));
163+
}
164+
165+
@Test(description = "convert an array model")
166+
public void arrayModelTest() {
167+
final Schema model = new ArraySchema().items(new Schema().$ref("#/components/schemas/Children"))
168+
.description("an array model");
169+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
170+
codegen.preprocessOpenAPI(new OpenAPI().components(new Components()));
171+
172+
final CodegenModel cm = codegen.fromModel("sample", model);
173+
174+
Assert.assertEquals(cm.name, "sample");
175+
Assert.assertEquals(cm.classname, "Sample");
176+
Assert.assertEquals(cm.description, "an array model");
177+
Assert.assertEquals(cm.vars.size(), 0);
178+
}
179+
180+
@Test(description = "convert a map model")
181+
public void mapModelTest() {
182+
final Schema model = new MapSchema().description("a map model")
183+
.additionalProperties(new Schema().$ref("#/components/schemas/Children"));
184+
final DefaultCodegenConfig codegen = new TypeScriptFetchClientCodegen();
185+
codegen.preprocessOpenAPI(new OpenAPI().components(new Components()));
186+
final CodegenModel cm = codegen.fromModel("sample", model);
187+
188+
Assert.assertEquals(cm.name, "sample");
189+
Assert.assertEquals(cm.classname, "Sample");
190+
Assert.assertEquals(cm.description, "a map model");
191+
Assert.assertEquals(cm.vars.size(), 0);
192+
Assert.assertEquals(cm.imports.size(), 1);
193+
Assert.assertEquals(cm.additionalPropertiesType, "Children");
194+
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
195+
}
196+
}

0 commit comments

Comments
 (0)