From 469ba4d7bf8f42141f98747c00b223d509a413c0 Mon Sep 17 00:00:00 2001 From: sup39 Date: Sat, 30 Jul 2022 20:29:25 +0900 Subject: [PATCH] Use `JSON.stringify()` for pure object in `STP._string` --- README.md | 2 ++ dist/utils/StrictTypeParser.js | 8 ++++++-- lib/utils/StrictTypeParser.ts | 7 ++++++- package.json | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3fd3c2a..d60ed5e 100644 --- a/README.md +++ b/README.md @@ -608,6 +608,8 @@ interface Task { This tool only supports `application/json` type for request and response body. Any other type like `multipart/form` or `image/*` are **not supported** and will be ignored. ## Versions +#### 2.1.3 +- Use `JSON.stringify()` for pure object in `STP._string` #### 2.1.2 - Fix axios query of array type in IClientAPI (add `{arrayFormat: 'repeat'}`) #### 2.1.1 diff --git a/dist/utils/StrictTypeParser.js b/dist/utils/StrictTypeParser.js index a5fcdc6..ebac502 100644 --- a/dist/utils/StrictTypeParser.js +++ b/dist/utils/StrictTypeParser.js @@ -19,6 +19,7 @@ exports.StrictTypeParser = void 0; var FullDate_1 = require("./FullDate"); var StrictTypeParser; (function (StrictTypeParser) { + var toStringDefault = {}.toString; var BadValueError = /** @class */ (function (_super) { __extends(BadValueError, _super); function BadValueError(label, message) { @@ -69,8 +70,11 @@ var StrictTypeParser; function _string(x, label) { if (typeof x === 'string') return x; - if (typeof x === 'object') - return x.toString(); + if (typeof x === 'object') { + return x.toString === toStringDefault ? + JSON.stringify(x) : // pure object => JSON + x.toString(); + } throw new BadTypeError(label, 'string', x); } StrictTypeParser._string = _string; diff --git a/lib/utils/StrictTypeParser.ts b/lib/utils/StrictTypeParser.ts index 8381945..d937613 100644 --- a/lib/utils/StrictTypeParser.ts +++ b/lib/utils/StrictTypeParser.ts @@ -1,6 +1,7 @@ import {FullDate} from './FullDate'; export module StrictTypeParser { + const toStringDefault = {}.toString; export class BadValueError extends Error { constructor(public label: string, message: string) { super(message); @@ -35,7 +36,11 @@ export module StrictTypeParser { } export function _string(x: any, label: string): string { if (typeof x === 'string') return x; - if (typeof x === 'object') return x.toString(); + if (typeof x === 'object') { + return x.toString === toStringDefault ? + JSON.stringify(x) : // pure object => JSON + x.toString(); + } throw new BadTypeError(label, 'string', x); } export function _boolean(x: any, label: string): boolean { diff --git a/package.json b/package.json index e83c4ef..5394646 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sup39/api-ts-gen", - "version": "2.1.2", + "version": "2.1.3", "description": "OpenAPI code generator for TypeScript", "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts",