Skip to content

Commit

Permalink
Merge pull request #17 from samchon/features/downgrade
Browse files Browse the repository at this point in the history
Fix bug of v3 downgrader about const type
  • Loading branch information
samchon authored Apr 23, 2024
2 parents 0a7e87d + 99d3dcd commit 9e989a6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 7 additions & 6 deletions src/internal/OpenApiV3Downgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>).type === value,
(u as OpenApiV3.IJsonSchema.__ISignificant<any>).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 {
Expand Down
23 changes: 23 additions & 0 deletions test/features/test_downgrade_v20.ts
Original file line number Diff line number Diff line change
@@ -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"],
});
};
23 changes: 23 additions & 0 deletions test/features/test_downgrade_v30.ts
Original file line number Diff line number Diff line change
@@ -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"],
});
};
5 changes: 5 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down Expand Up @@ -68,6 +70,9 @@ const main = async (): Promise<void> => {
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);
Expand Down

0 comments on commit 9e989a6

Please sign in to comment.