Skip to content

Commit 45b93bc

Browse files
authored
Release 5.1.3 (#167)
* chore: add full github swagger scheme * chore: add recursive schema * fix: problem of numeric schema names * chore: add exception for not found route name template * feat: processing x-nullable field * bump: up version to 5.1.3; docs: update CHANGELOG
1 parent 5535462 commit 45b93bc

17 files changed

+106056
-27
lines changed

CHANGELOG.md

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

3+
# 5.1.3
4+
5+
Fixes:
6+
- Ignored `x-nullable` field
7+
- Schema type names which starts with number or special characters
8+
39
# 5.1.2
410

511
Fixes:

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "5.1.2",
3+
"version": "5.1.3",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",

src/modelNames.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const checkAndRenameModelName = (name) => {
2020
}
2121

2222
if (!isValidName(name)) {
23+
if (!/^[a-zA-Z_$]/g.test(name)) {
24+
name = `Type ${name}`;
25+
}
26+
2327
// specific replaces for TSOA 3.x
2428
if (name.includes("."))
2529
name = name

src/routeNames.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const getRouteName = (routeInfo) => {
66
const { routeNameDuplicatesMap, templatesToRender } = config;
77
const routeNameTemplate = templatesToRender.routeName;
88

9-
const routeName = routeNameTemplate
10-
? renderTemplate(routeNameTemplate, {
11-
routeInfo: routeInfo,
12-
utils: require("./render/utils"),
13-
})
14-
: null;
9+
if (!routeNameTemplate) {
10+
throw new Error("🥵 route name template (route-name.eta) not found");
11+
}
12+
13+
const routeName = renderTemplate(routeNameTemplate, {
14+
routeInfo: routeInfo,
15+
utils: require("./render/utils"),
16+
});
1517

1618
const duplicateIdentifier = `${moduleName}|${routeName}`;
1719

src/routes.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const _ = require("lodash");
2-
const { types, parseSchema, getRefType, getInlineParseContent } = require("./schema");
2+
const {
3+
types,
4+
parseSchema,
5+
getRefType,
6+
getInlineParseContent,
7+
checkAndAddNull,
8+
} = require("./schema");
39
const { checkAndRenameModelName } = require("./modelNames");
410
const {
511
DEFAULT_BODY_ARG_NAME,
@@ -90,12 +96,15 @@ const getRequestInfoTypes = ({ requestInfos, parsedSchemas, operationId, default
9096
...(requestInfo || {}),
9197
contentTypes: contentTypes,
9298
contentKind: getContentKind(contentTypes),
93-
type: getTypeFromRequestInfo({
99+
type: checkAndAddNull(
94100
requestInfo,
95-
parsedSchemas,
96-
operationId,
97-
defaultType,
98-
}),
101+
getTypeFromRequestInfo({
102+
requestInfo,
103+
parsedSchemas,
104+
operationId,
105+
defaultType,
106+
}),
107+
),
99108
description: formatDescription(requestInfo.description || "", true),
100109
status: _.isNaN(+status) ? status : +status,
101110
isSuccess: isSuccessStatus(status),
@@ -378,11 +387,14 @@ const getRequestBodyInfo = (routeInfo, routeParams, parsedSchemas) => {
378387
type = getInlineParseContent(schema);
379388
} else if (requestBody) {
380389
schema = requestBody;
381-
type = getTypeFromRequestInfo({
382-
requestInfo: requestBody,
383-
parsedSchemas,
384-
operationId,
385-
});
390+
type = checkAndAddNull(
391+
requestBody,
392+
getTypeFromRequestInfo({
393+
requestInfo: requestBody,
394+
parsedSchemas,
395+
operationId,
396+
}),
397+
);
386398

387399
// TODO: Refactor that.
388400
// It needed for cases when swagger schema is not declared request body type as form data

src/schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const getInternalSchemaType = (schema) => {
7676

7777
const checkAndAddNull = (schema, value) => {
7878
const { nullable, type } = schema || {};
79-
return (nullable || type === TS_KEYWORDS.NULL) &&
79+
return (nullable || !!_.get(schema, "x-nullable") || type === TS_KEYWORDS.NULL) &&
8080
_.isString(value) &&
8181
!value.includes(` ${TS_KEYWORDS.NULL}`) &&
8282
!value.includes(`${TS_KEYWORDS.NULL} `)
@@ -386,4 +386,5 @@ module.exports = {
386386
getType,
387387
getRefType,
388388
SCHEMA_TYPES,
389+
checkAndAddNull,
389390
};

tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ As you see above here is two folders:
1111
- [**`v3.0`**](./generated/v3.0) - generated api modules for OA 3.0 schemas from above folder
1212

1313

14-
Most schemas taken from [apis.guru](https://apis.guru/openapi-directory/), [swagger.io github repo](https://swagger.io/) and [up-banking](https://github.com/up-banking/api)
14+
Most schemas taken from [apis.guru](https://apis.guru/openapi-directory/), [swagger.io github repo](https://swagger.io/), [Github api description](https://github.com/github/rest-api-description) and [up-banking](https://github.com/up-banking/api)

tests/generated/v2.0/another-schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
235235
* @request GET:/api/Foo/GetBarDescriptions
236236
*/
237237
fooGetBarDescriptions: (params: RequestParams = {}) =>
238-
this.request<string[], any>({
238+
this.request<string[] | null, any>({
239239
path: `/api/Foo/GetBarDescriptions`,
240240
method: "GET",
241241
format: "json",
@@ -250,7 +250,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
250250
* @request GET:/api/Foo/GetBar
251251
*/
252252
fooGetBar: (query: { id: number }, params: RequestParams = {}) =>
253-
this.request<Bar, any>({
253+
this.request<Bar | null, any>({
254254
path: `/api/Foo/GetBar`,
255255
method: "GET",
256256
query: query,
@@ -265,7 +265,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
265265
* @name FooSetBar
266266
* @request POST:/api/Foo/SetBar
267267
*/
268-
fooSetBar: (value: Bar, params: RequestParams = {}) =>
268+
fooSetBar: (value: Bar | null, params: RequestParams = {}) =>
269269
this.request<void, any>({
270270
path: `/api/Foo/SetBar`,
271271
method: "POST",

0 commit comments

Comments
 (0)