Skip to content

Commit cd141ae

Browse files
committed
fix: Serialize falsey values
Closes #890
1 parent b7ddca7 commit cd141ae

21 files changed

+524
-13
lines changed

src/transform/headers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function transformHeaderObjMap(
1616
const v = headerMap[k];
1717
if (!v.schema) continue;
1818

19-
if (v.description) output += comment(v.description);
19+
if (v.description !== undefined) output += comment(v.description);
2020

2121
const readonly = tsReadonly(options.immutableTypes);
2222
const required = v.required ? "" : "?";

src/transform/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function transformAll(schema: any, ctx: GlobalContext): Record<string, st
117117
if (Object.keys(operations).length) {
118118
for (const id of Object.keys(operations)) {
119119
const { operation, pathItem } = operations[id];
120-
if (operation.description) output.operations += comment(operation.description); // handle comment
120+
if (operation.description !== undefined) output.operations += comment(operation.description); // handle comment
121121
output.operations += ` ${readonly}"${id}": {\n ${transformOperationObj(operation, {
122122
...ctx,
123123
pathItem,

src/transform/operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function transformOperationObj(operation: OperationObject, options: Trans
3131
if (isRef(operation.requestBody)) {
3232
output += ` ${readonly}requestBody: ${operation.requestBody.$ref};\n`;
3333
} else {
34-
if (operation.requestBody.description) output += comment(operation.requestBody.description);
34+
if (operation.requestBody.description !== undefined) output += comment(operation.requestBody.description);
3535
output += ` ${readonly}requestBody: {\n ${transformRequestBodyObj(operation.requestBody, ctx)} }\n`;
3636
}
3737
}

src/transform/parameters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export function transformParametersArray(
5656
for (const [paramName, paramObj] of Object.entries(paramGroup)) {
5757
let paramComment = "";
5858
if (paramObj.deprecated) paramComment += `@deprecated `;
59-
if (paramObj.description) paramComment += paramObj.description;
60-
if (paramComment) output += comment(paramComment);
59+
if (paramObj.description !== undefined) paramComment += paramObj.description;
60+
if (paramComment !== undefined) output += comment(paramComment);
6161

6262
const required = paramObj.required ? `` : `?`;
6363
let paramType = ``;

src/transform/paths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function transformPathsObj(paths: Record<string, PathItemObject>, options
3434
let output = "";
3535

3636
for (const [url, pathItem] of Object.entries(paths)) {
37-
if (pathItem.description) output += comment(pathItem.description); // add comment
37+
if (pathItem.description !== undefined) output += comment(pathItem.description); // add comment
3838

3939
if (pathItem.$ref) {
4040
output += ` ${readonly}"${url}": ${pathItem.$ref};\n`;
@@ -67,7 +67,7 @@ export function transformPathsObj(paths: Record<string, PathItemObject>, options
6767
for (const method of httpMethods) {
6868
const operation = (pathItem as any)[method];
6969
if (!operation) continue; // only allow valid methods
70-
if (operation.description) output += comment(operation.description); // add comment
70+
if (operation.description !== undefined) output += comment(operation.description); // add comment
7171
if (operation.operationId) {
7272
// if operation has operationId, abstract into top-level operations object
7373
operations[operation.operationId] = { operation, pathItem };

src/transform/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function transformRequestBodies(requestBodies: Record<string, RequestBody
66
let output = "";
77

88
for (const [name, requestBody] of Object.entries(requestBodies)) {
9-
if (requestBody && requestBody.description) output += ` ${comment(requestBody.description)}`;
9+
if (requestBody && requestBody.description !== undefined) output += ` ${comment(requestBody.description)}`;
1010
output += ` "${name}": {\n ${transformRequestBodyObj(requestBody, ctx)}\n }\n`;
1111
}
1212

src/transform/responses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function transformResponsesObj(responsesObj: Record<string, any>, ctx: Gl
1313
for (const httpStatusCode of Object.keys(responsesObj)) {
1414
const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; // don’t surround w/ quotes if numeric status code
1515
const response = responsesObj[httpStatusCode];
16-
if (response.description) output += comment(response.description);
16+
if (response.description !== undefined) output += comment(response.description);
1717

1818
if (response.$ref) {
1919
output += ` ${readonly}${statusCode}: ${response.$ref};\n`; // reference

src/transform/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
3333

3434
// 1. Add comment in jsdoc notation
3535
const comment = prepareComment(v);
36-
if (comment) output += comment;
36+
if (comment !== undefined) output += comment;
3737

3838
// 2. name (with “?” if optional property)
3939
const readonly = tsReadonly(options.immutableTypes);

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function prepareComment(v: CommentObject): string | void {
3131
const commentsArray: Array<string> = [];
3232

3333
// * Not JSDOC tags: [title, format]
34-
if (v.title) commentsArray.push(`${v.title} `);
35-
if (v.format) commentsArray.push(`Format: ${v.format} `);
34+
if (v.title !== undefined) commentsArray.push(`${v.title} `);
35+
if (v.format !== undefined) commentsArray.push(`Format: ${v.format} `);
3636

3737
// * JSDOC tags without value
3838
// 'Deprecated' without value
@@ -42,7 +42,7 @@ export function prepareComment(v: CommentObject): string | void {
4242
const supportedJsDocTags: Array<keyof CommentObject> = ["description", "default", "example"];
4343
for (let index = 0; index < supportedJsDocTags.length; index++) {
4444
const field = supportedJsDocTags[index];
45-
if (v[field]) commentsArray.push(`@${field} ${v[field]} `);
45+
if (v[field] !== undefined) commentsArray.push(`@${field} ${v[field]} `);
4646
}
4747

4848
// * JSDOC 'Constant' without value
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
"/test": {
8+
/** */
9+
get: {
10+
parameters: {
11+
query: {
12+
/** */
13+
search: string;
14+
};
15+
};
16+
responses: {
17+
/** */
18+
200: unknown;
19+
};
20+
};
21+
};
22+
}
23+
24+
export interface definitions {
25+
TestSchema: {
26+
/**
27+
* @description
28+
* @default false
29+
* @example false
30+
*/
31+
isEnabled?: boolean;
32+
/**
33+
* @description
34+
* @default 0
35+
* @example 0
36+
*/
37+
count?: number;
38+
};
39+
}
40+
41+
export interface operations {}
42+
43+
export interface external {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export type paths = {
7+
"/test": {
8+
/** */
9+
get: {
10+
parameters: {
11+
query: {
12+
/** */
13+
search: string;
14+
};
15+
};
16+
responses: {
17+
/** */
18+
200: unknown;
19+
};
20+
};
21+
};
22+
};
23+
24+
export type definitions = {
25+
TestSchema: {
26+
/**
27+
* @description
28+
* @default false
29+
* @example false
30+
*/
31+
isEnabled?: boolean;
32+
/**
33+
* @description
34+
* @default 0
35+
* @example 0
36+
*/
37+
count?: number;
38+
};
39+
};
40+
41+
export type operations = {};
42+
43+
export type external = {};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
readonly "/test": {
8+
/** */
9+
readonly get: {
10+
readonly parameters: {
11+
readonly query: {
12+
/** */
13+
readonly search: string;
14+
};
15+
};
16+
readonly responses: {
17+
/** */
18+
readonly 200: unknown;
19+
};
20+
};
21+
};
22+
}
23+
24+
export interface definitions {
25+
readonly TestSchema: {
26+
/**
27+
* @description
28+
* @default false
29+
* @example false
30+
*/
31+
readonly isEnabled?: boolean;
32+
/**
33+
* @description
34+
* @default 0
35+
* @example 0
36+
*/
37+
readonly count?: number;
38+
};
39+
}
40+
41+
export interface operations {}
42+
43+
export interface external {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
"/test": {
8+
/** */
9+
get: {
10+
parameters: {
11+
query: {
12+
/** */
13+
search: string;
14+
};
15+
};
16+
responses: {
17+
/** */
18+
200: unknown;
19+
};
20+
};
21+
};
22+
}
23+
24+
export interface definitions {
25+
TestSchema: {
26+
/**
27+
* @description
28+
* @default false
29+
* @example false
30+
*/
31+
isEnabled?: boolean;
32+
/**
33+
* @description
34+
* @default 0
35+
* @example 0
36+
*/
37+
count?: number;
38+
};
39+
}
40+
41+
export interface operations {}
42+
43+
export interface external {}

test/v2/expected/falsey-example.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
"/test": {
8+
/** */
9+
get: {
10+
parameters: {
11+
query: {
12+
/** */
13+
search: string;
14+
};
15+
};
16+
responses: {
17+
/** */
18+
200: unknown;
19+
};
20+
};
21+
};
22+
}
23+
24+
export interface definitions {
25+
TestSchema: {
26+
/**
27+
* @description
28+
* @default false
29+
* @example false
30+
*/
31+
isEnabled?: boolean;
32+
/**
33+
* @description
34+
* @default 0
35+
* @example 0
36+
*/
37+
count?: number;
38+
};
39+
}
40+
41+
export interface operations {}
42+
43+
export interface external {}

test/v2/specs/falsey-example.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
swagger: '2.0'
2+
info:
3+
title: ''
4+
version: '1.0.0'
5+
paths:
6+
/test:
7+
get:
8+
tags:
9+
- test
10+
description: ''
11+
summary: ''
12+
responses:
13+
200:
14+
description: ''
15+
parameters:
16+
- in: query
17+
name: search
18+
type: string
19+
description: ''
20+
required: true
21+
definitions:
22+
TestSchema:
23+
type: object
24+
properties:
25+
isEnabled:
26+
type: boolean
27+
description: ''
28+
default: false
29+
example: false
30+
count:
31+
type: number
32+
description: ''
33+
default: 0
34+
example: 0

0 commit comments

Comments
 (0)