From 99d3dcda32a0459533f1fac3b6c0943071eefcc3 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Tue, 23 Apr 2024 14:13:40 +0900 Subject: [PATCH] Fix bug of v3 downgrader about const type --- package.json | 3 ++- src/internal/OpenApiV3Downgrader.ts | 13 +++++++------ test/features/test_downgrade_v20.ts | 23 +++++++++++++++++++++++ test/features/test_downgrade_v30.ts | 23 +++++++++++++++++++++++ test/index.ts | 5 +++++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 test/features/test_downgrade_v20.ts create mode 100644 test/features/test_downgrade_v30.ts diff --git a/package.json b/package.json index 14e3273..f079161 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@samchon/openapi", - "version": "0.1.19", + "version": "0.1.20", "description": "OpenAPI definitions and converters for 'typia' and 'nestia'.", "main": "./lib/index.js", "typings": "./lib/index.d.ts", @@ -29,6 +29,7 @@ }, "homepage": "https://github.com/samchon/openapi", "devDependencies": { + "@nestia/e2e": "^0.4.3", "@types/node": "^20.12.7", "prettier": "^3.2.5", "rimraf": "^5.0.5", diff --git a/src/internal/OpenApiV3Downgrader.ts b/src/internal/OpenApiV3Downgrader.ts index 9501c6a..c273207 100644 --- a/src/internal/OpenApiV3Downgrader.ts +++ b/src/internal/OpenApiV3Downgrader.ts @@ -258,26 +258,27 @@ export namespace OpenApiV3Downgrader { const insert = (value: any): void => { const matched: OpenApiV3.IJsonSchema.INumber | undefined = union.find( (u) => - (u as OpenApiV3.IJsonSchema.__ISignificant).type === value, + (u as OpenApiV3.IJsonSchema.__ISignificant).type === + typeof value, ) as OpenApiV3.IJsonSchema.INumber | undefined; if (matched !== undefined) { matched.enum ??= []; matched.enum.push(value); } else union.push({ type: typeof value as "number", enum: [value] }); - if (OpenApiTypeChecker.isConstant(schema)) insert(schema.const); - else if (OpenApiTypeChecker.isOneOf(schema)) - schema.oneOf.forEach(insert); }; + if (OpenApiTypeChecker.isConstant(schema)) insert(schema.const); + else if (OpenApiTypeChecker.isOneOf(schema)) + for (const u of schema.oneOf) + if (OpenApiTypeChecker.isConstant(u)) insert(u.const); }; visit(input); visitConstant(input); - if (nullable) + if (nullable === true) for (const u of union) if (OpenApiTypeChecker.isReference(u)) downgradeNullableReference(collection)(u); else (u as OpenApiV3.IJsonSchema.IArray).nullable = true; - if (nullable === true && union.length === 0) return { type: "null", ...attribute }; return { diff --git a/test/features/test_downgrade_v20.ts b/test/features/test_downgrade_v20.ts new file mode 100644 index 0000000..c3da255 --- /dev/null +++ b/test/features/test_downgrade_v20.ts @@ -0,0 +1,23 @@ +import { OpenApi, SwaggerV2 } from "../../src"; +import { TestValidator } from "@nestia/e2e"; +import { SwaggerV2Downgrader } from "../../src/internal/SwaggerV2Downgrader"; + +export const test_downgrade_v20 = () => { + const schema: OpenApi.IJsonSchema = { + oneOf: [{ const: "a" }, { const: "b" }, { const: "c" }], + title: "something", + description: "nothing", + }; + const downgraded: SwaggerV2.IJsonSchema = SwaggerV2Downgrader.downgradeSchema( + { + original: {}, + downgraded: {}, + }, + )(schema); + TestValidator.equals("enum")(downgraded)({ + type: "string", + title: "something", + description: "nothing", + enum: ["a", "b", "c"], + }); +}; diff --git a/test/features/test_downgrade_v30.ts b/test/features/test_downgrade_v30.ts new file mode 100644 index 0000000..da97c89 --- /dev/null +++ b/test/features/test_downgrade_v30.ts @@ -0,0 +1,23 @@ +import { OpenApi, OpenApiV3 } from "../../src"; +import { OpenApiV3Downgrader } from "../../src/internal/OpenApiV3Downgrader"; +import { TestValidator } from "@nestia/e2e"; + +export const test_downgrade_v30 = () => { + const schema: OpenApi.IJsonSchema = { + oneOf: [{ const: "a" }, { const: "b" }, { const: "c" }], + title: "something", + description: "nothing", + }; + const downgraded: OpenApiV3.IJsonSchema = OpenApiV3Downgrader.downgradeSchema( + { + original: {}, + downgraded: {}, + }, + )(schema); + TestValidator.equals("enum")(downgraded)({ + type: "string", + title: "something", + description: "nothing", + enum: ["a", "b", "c"], + }); +}; diff --git a/test/index.ts b/test/index.ts index 5d42503..08ddbef 100644 --- a/test/index.ts +++ b/test/index.ts @@ -3,6 +3,8 @@ import path from "path"; import typia from "typia"; import { OpenApi, OpenApiV3, OpenApiV3_1, SwaggerV2 } from "../src"; +import { test_downgrade_v30 } from "./features/test_downgrade_v30"; +import { test_downgrade_v20 } from "./features/test_downgrade_v20"; const CONVERTED: string = `${__dirname}/../../examples/converted`; @@ -68,6 +70,9 @@ const main = async (): Promise => { await iterate(`${__dirname}/../../examples/v2.0`); await iterate(`${__dirname}/../../examples/v3.0`); await iterate(`${__dirname}/../../examples/v3.1`); + + test_downgrade_v20(); + test_downgrade_v30(); }; main().catch((exp) => { console.error(exp);