Skip to content

Commit 10f0df3

Browse files
authored
Release 9.1.0 (#256)
* feat: improve --type-suffix --type-prefix options * fix: critical problem based on it.utils.require() * internal: critical fix of type prefix/suffix for typeSuffixPrefix manual test * fix: critical bug linked with type prefix\suffix with extracted params * chore: change test script in github workflow * bump: up version to 9.1.0; docs: update CHANGELOG;
1 parent 0cbcbba commit 10f0df3

File tree

43 files changed

+2424
-433
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2424
-433
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434

3535
# Runs a set of commands using the runners shell
3636
- name: test
37-
run: npm run test-all
37+
run: npm run test-all-extended

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@
5959
"cwd": "${workspaceFolder}",
6060
"runtimeExecutable": "npm",
6161
"runtimeArgs": ["run-script", "test-all"]
62+
},
63+
{
64+
"name": "Debug test-all-extended",
65+
"type": "node",
66+
"request": "launch",
67+
"cwd": "${workspaceFolder}",
68+
"runtimeExecutable": "npm",
69+
"runtimeArgs": ["run-script", "test-all-extended"]
70+
},
71+
{
72+
"name": "Debug test:--extract-request-body",
73+
"type": "node",
74+
"request": "launch",
75+
"cwd": "${workspaceFolder}",
76+
"runtimeExecutable": "npm",
77+
"runtimeArgs": ["run-script", "test:--extract-request-body"]
78+
},
79+
{
80+
"name": "Debug test:--type-suffix--type-prefix",
81+
"type": "node",
82+
"request": "launch",
83+
"cwd": "${workspaceFolder}",
84+
"runtimeExecutable": "npm",
85+
"runtimeArgs": ["run-script", "test:--type-suffix--type-prefix"]
6286
}
6387
]
6488
}

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# next release
22

3+
# 9.1.0
4+
5+
Fixes:
6+
- Critical bug linked with `templateRequire` in ETA templates
7+
- Critical bugs linked with `--type-prefix`, `--type-suffix`
8+
9+
Internal:
10+
- Improve manual testing scripts
11+
312
# 9.0.2
413

514
Fixes:

package-lock.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "9.0.2",
3+
"version": "9.1.0",
44
"description": "Generate typescript/javascript api from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -9,9 +9,12 @@
99
"node:debug": "node --nolazy swagger-test-cli/generate.js",
1010
"contributors": "all-contributors generate",
1111
"cli:help": "node index.js -h",
12-
"test-all": "node ./scriptsRunner.js generate validate test:*",
12+
"test-all": "node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
13+
"test-all-extended": "node --unhandled-rejections=strict ./scriptsRunner.js generate-extended validate generate validate test:*",
1314
"test-specific": "node ./scriptsRunner.js generate validate test:*",
15+
"prepare": "npm run test-all-extended",
1416
"generate": "node tests/generate.js",
17+
"generate-extended": "node tests/generate-extended.js",
1518
"generate:debug": "node --nolazy tests/generate.js",
1619
"validate": "node tests/validate.js",
1720
"validate:debug": "node --nolazy tests/validate.js",
@@ -27,6 +30,7 @@
2730
"test:--modular": "node tests/spec/modular/test.js",
2831
"test:--single-http-client": "node tests/spec/singleHttpClient/test.js",
2932
"test:--extract-request-params": "node tests/spec/extractRequestParams/test.js",
33+
"test:--extract-request-body": "node tests/spec/extractRequestBody/test.js",
3034
"test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
3135
"test:--default-response": "node tests/spec/defaultResponse/test.js",
3236
"test:--js": "node tests/spec/js/test.js",
@@ -43,6 +47,7 @@
4347
"main": "src/index.js",
4448
"devDependencies": {
4549
"@types/lodash": "^4.14.166",
50+
"@types/node": "^15.0.2",
4651
"@types/prettier": "^2.1.6",
4752
"all-contributors-cli": "^6.19.0",
4853
"husky": "^4.3.6",

scriptsRunner.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ const execute = (scriptName) =>
1818
process.stderr.write(data);
1919
});
2020

21+
spawned.on("error", (error) => {
22+
console.error(error);
23+
});
24+
25+
spawned.on("message", (message) => {
26+
console.log(message);
27+
});
28+
2129
spawned.on("close", (code) => {
2230
if (code) {
2331
reject(code);
@@ -28,18 +36,22 @@ const execute = (scriptName) =>
2836
});
2937

3038
const run = async () => {
31-
for (const scriptName of packageScripts) {
32-
for (const command of commands) {
33-
if (scriptName === command) {
34-
await execute(scriptName);
35-
}
36-
37-
if (command.includes("*")) {
38-
const commandPart = command.replace("*", "");
39-
// TODO: refactor
40-
if (scriptName.startsWith(commandPart) || scriptName.endsWith(commandPart)) {
39+
for (const command of commands) {
40+
for (const scriptName of packageScripts) {
41+
try {
42+
if (scriptName === command) {
4143
await execute(scriptName);
4244
}
45+
46+
if (command.includes("*")) {
47+
const commandPart = command.replace("*", "");
48+
// TODO: refactor
49+
if (scriptName.startsWith(commandPart) || scriptName.endsWith(commandPart)) {
50+
await execute(scriptName);
51+
}
52+
}
53+
} catch (e) {
54+
process.exit(1);
4355
}
4456
}
4557
}

src/modelNames.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,52 @@ const fixModelName = (name) => {
2727
return name;
2828
};
2929

30-
const formatModelName = (name) => {
30+
/**
31+
*
32+
* @param {string} name
33+
* @param {{ ignorePrefix?: boolean; ignoreSuffix?: boolean }} options
34+
* @returns
35+
*/
36+
const formatModelName = (name, options) => {
37+
const typePrefix = options && options.ignorePrefix ? "" : config.typePrefix;
38+
const typeSuffix = options && options.ignoreSuffix ? "" : config.typeSuffix;
39+
const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
40+
3141
if (typeof name !== "string") {
3242
logger.warn("wrong name of the model name", name);
3343
return name;
3444
}
3545

3646
if (/^([A-Z_]{1,})$/g.test(name)) {
37-
return name;
47+
return _.compact([typePrefix, name, typeSuffix]).join("_");
3848
}
3949

40-
if (formattedModelNamesMap.has(name)) {
41-
return formattedModelNamesMap.get(name);
50+
if (formattedModelNamesMap.has(hashKey)) {
51+
return formattedModelNamesMap.get(hashKey);
4252
}
4353

4454
const fixedModelName = fixModelName(name);
4555

4656
const formattedModelName = _.replace(
47-
_.startCase(`${config.typePrefix}_${fixedModelName}_${config.typeSuffix}`),
57+
_.startCase(`${typePrefix}_${fixedModelName}_${typeSuffix}`),
4858
/\s/g,
4959
"",
5060
);
5161
const modelName = config.hooks.onFormatTypeName(formattedModelName, name) || formattedModelName;
5262

53-
formattedModelNamesMap.set(name, modelName);
63+
formattedModelNamesMap.set(hashKey, modelName);
5464

5565
return modelName;
5666
};
5767

68+
const formatEnumKey = (key) =>
69+
formatModelName(key, {
70+
ignorePrefix: true,
71+
ignoreSuffix: true,
72+
});
73+
5874
module.exports = {
5975
formatModelName: formatModelName,
76+
formatEnumKey: formatEnumKey,
6077
isValidName,
6178
};

src/modelTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const prepareModelType = (typeInfo) => {
1313
let { typeIdentifier, name: originalName, content, type, description } = typeData;
1414

1515
const resultContent = formatters[type] ? formatters[type](content) : content;
16-
const name = formatModelName(originalName || typeInfo.typeName);
16+
const name = formatModelName(originalName);
1717

1818
return {
1919
typeIdentifier,

src/render/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ module.exports = {
1919
fmtToJSDocLine: require("./fmtToJSDocLine"),
2020
NameResolver: NameResolver,
2121
_: require("lodash"),
22-
require: require("./templateRequire"),
22+
require: require("./templateRequire").templateRequire,
2323
};

src/routes.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ const getSchemaFromRequestType = (requestInfo) => {
4646
return null;
4747
};
4848

49-
const getTypeFromRequestInfo = ({ requestInfo, parsedSchemas, operationId, defaultType }) => {
49+
const getTypeFromRequestInfo = ({
50+
requestInfo,
51+
parsedSchemas,
52+
operationId,
53+
defaultType,
54+
typeName,
55+
}) => {
5056
// TODO: make more flexible pick schema without content type
5157
const schema = getSchemaFromRequestType(requestInfo);
5258
const refTypeInfo = getRefType(requestInfo);
5359

5460
if (schema) {
55-
const content = getInlineParseContent(schema, "none");
61+
const content = getInlineParseContent(schema, typeName);
5662
const foundedSchemaByName = _.find(
5763
parsedSchemas,
5864
(parsedSchema) => formatModelName(parsedSchema.name) === content,
@@ -81,9 +87,12 @@ const getTypeFromRequestInfo = ({ requestInfo, parsedSchemas, operationId, defau
8187
return formatModelName(refTypeInfo.typeName);
8288
case "responses":
8389
case "requestBodies":
84-
return getInlineParseContent(getSchemaFromRequestType(refTypeInfo.rawTypeData), "none");
90+
return getInlineParseContent(
91+
getSchemaFromRequestType(refTypeInfo.rawTypeData),
92+
refTypeInfo.typeName || null,
93+
);
8594
default:
86-
return getInlineParseContent(refTypeInfo.rawTypeData, "none");
95+
return getInlineParseContent(refTypeInfo.rawTypeData, refTypeInfo.typeName || null);
8796
}
8897
}
8998

@@ -396,13 +405,23 @@ const getRequestBodyInfo = (routeInfo, routeParams, parsedSchemas, routeName) =>
396405
);
397406
let contentKind = getContentKind(contentTypes);
398407

408+
let typeName = null;
409+
410+
if (config.extractRequestBody) {
411+
typeName = config.componentTypeNameResolver.resolve([
412+
classNameCase(`${routeName.usage} Payload`),
413+
classNameCase(`${routeName.usage} Body`),
414+
classNameCase(`${routeName.usage} Input`),
415+
]);
416+
}
417+
399418
if (routeParams.formData.length) {
400419
contentKind = CONTENT_KIND.FORM_DATA;
401420
schema = convertRouteParamsIntoObject(routeParams.formData);
402-
type = getInlineParseContent(schema);
421+
type = getInlineParseContent(schema, typeName);
403422
} else if (contentKind === CONTENT_KIND.FORM_DATA) {
404423
schema = getSchemaFromRequestType(requestBody);
405-
type = getInlineParseContent(schema);
424+
type = getInlineParseContent(schema, typeName);
406425
} else if (requestBody) {
407426
schema = getSchemaFromRequestType(requestBody);
408427
type = checkAndAddNull(
@@ -411,6 +430,7 @@ const getRequestBodyInfo = (routeInfo, routeParams, parsedSchemas, routeName) =>
411430
requestInfo: requestBody,
412431
parsedSchemas,
413432
operationId,
433+
typeName,
414434
}),
415435
);
416436

@@ -423,13 +443,8 @@ const getRequestBodyInfo = (routeInfo, routeParams, parsedSchemas, routeName) =>
423443
}
424444

425445
if (schema && !schema.$ref && config.extractRequestBody) {
426-
const typeName = config.componentTypeNameResolver.resolve([
427-
classNameCase(`${routeName.usage} Payload`),
428-
classNameCase(`${routeName.usage} Body`),
429-
classNameCase(`${routeName.usage} Input`),
430-
]);
431446
schema = createComponent("schemas", typeName, { ...schema });
432-
type = typeName;
447+
type = getInlineParseContent(schema);
433448
}
434449

435450
return {

0 commit comments

Comments
 (0)