Skip to content

Commit 1f432ad

Browse files
authored
Merge pull request #661 from johnteske/ts-fetch-tests
[typescript-fetch] Add api test generation support
2 parents 614cc91 + 072788a commit 1f432ad

File tree

10 files changed

+161
-7
lines changed

10 files changed

+161
-7
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.util.Date;
66

77
import io.swagger.codegen.v3.CliOption;
8+
import io.swagger.codegen.v3.CodegenConstants;
89
import io.swagger.codegen.v3.CodegenModel;
10+
import io.swagger.codegen.v3.CodegenParameter;
911
import io.swagger.codegen.v3.SupportingFile;
1012
import io.swagger.v3.oas.models.media.ArraySchema;
1113
import io.swagger.v3.oas.models.media.BinarySchema;
@@ -76,6 +78,7 @@ public void processOpts() {
7678

7779
supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
7880
supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts"));
81+
supportingFiles.add(new SupportingFile("api_test.mustache", "", "api_test.spec.ts"));
7982
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
8083
supportingFiles.add(new SupportingFile("custom.d.mustache", "", "custom.d.ts"));
8184
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
@@ -143,6 +146,15 @@ private boolean isLanguageGenericType(String type) {
143146
return false;
144147
}
145148

149+
@Override
150+
public void postProcessParameter(CodegenParameter parameter) {
151+
super.postProcessParameter(parameter);
152+
153+
String type = applyLocalTypeMapping(parameter.dataType);
154+
parameter.dataType = type;
155+
parameter.getVendorExtensions().put(CodegenConstants.IS_PRIMITIVE_TYPE_EXT_NAME, isLanguagePrimitive(type));
156+
}
157+
146158
private void addNpmPackageGeneration() {
147159
if (additionalProperties.containsKey(NPM_NAME)) {
148160
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{>licenseInfo}}
2+
3+
import * as api from "./api"
4+
import { Configuration } from "./configuration"
5+
6+
const config: Configuration = {}
7+
8+
{{#apiInfo}}
9+
{{#apis}}
10+
{{#operations}}
11+
describe("{{classname}}", () => {
12+
let instance: api.{{classname}}
13+
beforeEach(function() {
14+
instance = new api.{{classname}}(config)
15+
});
16+
17+
{{#operation}}
18+
test("{{operationId}}", () => {
19+
{{#allParams}}
20+
const {{{paramName}}}: {{^isPrimitiveType}}api.{{/isPrimitiveType}}{{{dataType}}} = {{>api_test_default_value}}
21+
{{/allParams}}
22+
return expect(instance.{{operationId}}({{#allParams}}{{{paramName}}}, {{/allParams}}{})).resolves.toBe(null)
23+
})
24+
{{/operation}}
25+
})
26+
27+
{{/operations}}
28+
{{/apis}}
29+
{{/apiInfo}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#example}}{{#isString}}"{{{example}}}"{{/isString}}{{^isString}}{{#isUuid}}"{{{example}}}"{{/isUuid}}{{^isUuid}}{{{example}}}{{/isUuid}}{{/isString}}{{/example}}{{^example}}undefined{{/example}}

src/main/resources/handlebars/typescript-fetch/licenseInfo.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
66
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
77
*
8-
* NOTE: This class is auto generated by the swagger code generator program.
8+
* NOTE: This file is auto generated by the swagger code generator program.
99
* https://github.com/swagger-api/swagger-codegen.git
10-
* Do not edit the class manually.
10+
* Do not edit the file manually.
1111
*/

src/main/resources/handlebars/typescript-fetch/package.mustache

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@
1414
"typings": "./dist/index.d.ts",
1515
"scripts" : {
1616
"build": "tsc --outDir dist/",
17+
"test": "jest",
1718
"prepublishOnly": "npm run build"
1819
},
1920
"dependencies": {
2021
"portable-fetch": "^3.0.0"
2122
},
2223
"devDependencies": {
23-
"@types/node": "^8.0.9",
24-
"typescript": "^2.0"
25-
}{{#npmRepository}},{{/npmRepository}}
26-
{{#npmRepository}}
24+
"@types/jest": "^25.2.1",
25+
"@types/node": "^13.13.0",
26+
"jest": "^25.4.0",
27+
"ts-jest": "^25.4.0",
28+
"typescript": "^3.8.3"
29+
},
30+
"jest": {
31+
"transform": {
32+
"^.+\\.tsx?$": "ts-jest"
33+
},
34+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
35+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
36+
}{{#npmRepository}},
2737
"publishConfig":{
2838
"registry":"{{npmRepository}}"
2939
}

src/main/resources/handlebars/typescript-fetch/tsconfig.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"exclude": [
1616
"dist",
17-
"node_modules"
17+
"node_modules",
18+
"**/*.spec.ts"
1819
]
1920
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{>licenseInfo}}
2+
3+
import * as api from "./api"
4+
import { Configuration } from "./configuration"
5+
6+
const config: Configuration = {}
7+
8+
{{#apiInfo}}
9+
{{#apis}}
10+
{{#operations}}
11+
describe("{{classname}}", () => {
12+
let instance: api.{{classname}}
13+
beforeEach(function() {
14+
instance = new api.{{classname}}(config)
15+
});
16+
17+
{{#operation}}
18+
test("{{operationId}}", () => {
19+
{{#allParams}}
20+
const {{{paramName}}}: {{^isPrimitiveType}}api.{{/isPrimitiveType}}{{{dataType}}} = {{>api_test_default_value}}
21+
{{/allParams}}
22+
return expect(instance.{{operationId}}({{#allParams}}{{{paramName}}}, {{/allParams}}{})).resolves.toBe(null)
23+
})
24+
{{/operation}}
25+
})
26+
27+
{{/operations}}
28+
{{/apis}}
29+
{{/apiInfo}}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* {{{appName}}}
3+
* {{{appDescription}}}
4+
*
5+
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
6+
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
7+
*
8+
* NOTE: This file is auto generated by the swagger code generator program.
9+
* https://github.com/swagger-api/swagger-codegen.git
10+
* Do not edit the file manually.
11+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "{{npmName}}",
3+
"version": "{{npmVersion}}",
4+
"description": "swagger client for {{npmName}}",
5+
"author": "Swagger Codegen Contributors",
6+
"keywords": [
7+
"fetch",
8+
"typescript",
9+
"swagger-client",
10+
"{{npmName}}"
11+
],
12+
"license": "Unlicense",
13+
"main": "./dist/index.js",
14+
"typings": "./dist/index.d.ts",
15+
"scripts" : {
16+
"build": "tsc --outDir dist/",
17+
"test": "jest",
18+
"prepublishOnly": "npm run build"
19+
},
20+
"dependencies": {
21+
"portable-fetch": "^3.0.0"
22+
},
23+
"devDependencies": {
24+
"@types/jest": "^25.2.1",
25+
"@types/node": "^13.13.0",
26+
"jest": "^25.4.0",
27+
"ts-jest": "^25.4.0",
28+
"typescript": "^3.8.3"
29+
},
30+
"jest": {
31+
"transform": {
32+
"^.+\\.tsx?$": "ts-jest"
33+
},
34+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
35+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
36+
}{{#npmRepository}},
37+
"publishConfig":{
38+
"registry":"{{npmRepository}}"
39+
}
40+
{{/npmRepository}}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}",
5+
"module": "commonjs",
6+
"noImplicitAny": true,
7+
"outDir": "dist",
8+
"rootDir": "."{{^supportsES6}},
9+
"lib": [
10+
"es6",
11+
"dom"
12+
]
13+
{{/supportsES6}}
14+
},
15+
"exclude": [
16+
"dist",
17+
"node_modules",
18+
"**/*.spec.ts"
19+
]
20+
}

0 commit comments

Comments
 (0)