From 63a4180b356e953ea3685785136f72ca22c481c3 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Mon, 8 Jul 2024 20:35:40 +0900 Subject: [PATCH] Remove `oneOf.discriminator` property. It is because `oneOf.discriminator` is the property only for the every `oneOf` types are `$ref`. --- package.json | 12 +-- src/OpenAiComposer.ts | 13 ++- .../composer/test_composer_schema_oneof.ts | 79 +++++++++++++++++++ 3 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 test/features/composer/test_composer_schema_oneof.ts diff --git a/package.json b/package.json index ba6128d..00e69d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wrtnio/openai-function-schema", - "version": "0.1.3", + "version": "0.1.4", "description": "OpenAI LLM function schema from OpenAPI (Swagger) document", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -37,16 +37,16 @@ "author": "", "license": "ISC", "dependencies": { - "@nestia/fetcher": "^3.4.1", - "@samchon/openapi": "^0.3.0", + "@nestia/fetcher": "^3.5.0", + "@samchon/openapi": "^0.3.1", "commander": "^10.0.0", "inquirer": "^8.2.5", - "typia": "^6.4.0" + "typia": "^6.4.3" }, "devDependencies": { - "@nestia/core": "^3.4.1", + "@nestia/core": "^3.5.0", "@nestia/e2e": "^0.6.0", - "@nestia/sdk": "^3.4.1", + "@nestia/sdk": "^3.5.0", "@nestjs/common": "^10.3.10", "@nestjs/core": "^10.3.10", "@nestjs/platform-express": "^10.3.10", diff --git a/src/OpenAiComposer.ts b/src/OpenAiComposer.ts index 67f85ec..7802423 100644 --- a/src/OpenAiComposer.ts +++ b/src/OpenAiComposer.ts @@ -11,7 +11,7 @@ import { OpenApiV3Downgrader } from "@samchon/openapi/lib/internal/OpenApiV3Down import typia from "typia"; import { OpenAiSchemaSeparator } from "./internal/OpenAiSchemaSeparator"; -import { IOpenAiSchema, ISwaggerOperation } from "./module"; +import { IOpenAiSchema, ISwaggerOperation, OpenAiTypeChecker } from "./module"; import { IOpenAiDocument } from "./structures/IOpenAiDocument"; import { IOpenAiFunction } from "./structures/IOpenAiFunction"; import { ISwagger } from "./structures/ISwagger"; @@ -162,11 +162,18 @@ export namespace OpenAiComposer { new Set(), )(schema); if (escaped === null) return null; - const downgraded = OpenApiV3Downgrader.downgradeSchema({ + const downgraded: IOpenAiSchema = OpenApiV3Downgrader.downgradeSchema({ original: {}, downgraded: {}, })(escaped); - return downgraded as IOpenAiSchema; + OpenAiTypeChecker.visit(downgraded, (schema) => { + if ( + OpenAiTypeChecker.isOneOf(schema) && + (schema as any).discriminator !== undefined + ) + delete (schema as any).discriminator; + }); + return downgraded; }; const composeFunction = diff --git a/test/features/composer/test_composer_schema_oneof.ts b/test/features/composer/test_composer_schema_oneof.ts new file mode 100644 index 0000000..c190349 --- /dev/null +++ b/test/features/composer/test_composer_schema_oneof.ts @@ -0,0 +1,79 @@ +import { TestValidator } from "@nestia/e2e"; +import { OpenApi } from "@samchon/openapi"; +import { IOpenAiSchema, OpenAiComposer } from "@wrtnio/openai-function-schema"; +import typia, { IJsonApplication } from "typia"; + +export const test_composer_schema_oneof = (): void => { + const app: IJsonApplication = + typia.json.application<[Circle | Triangle | Rectangle]>(); + const schema: OpenApi.IJsonSchema = app.schemas[0]; + const casted: IOpenAiSchema | null = OpenAiComposer.schema( + app.components, + schema, + ); + TestValidator.equals("oneOf")(casted)({ + oneOf: [ + { + type: "object", + properties: { + type: { + type: "string", + enum: ["circle"], + }, + radius: { + type: "number", + }, + }, + required: ["type", "radius"], + }, + { + type: "object", + properties: { + type: { + type: "string", + enum: ["triangle"], + }, + base: { + type: "number", + }, + height: { + type: "number", + }, + }, + required: ["type", "base", "height"], + }, + { + type: "object", + properties: { + type: { + type: "string", + enum: ["square"], + }, + width: { + type: "number", + }, + height: { + type: "number", + }, + }, + required: ["type", "width", "height"], + }, + ], + ...{ discriminator: undefined }, + }); +}; + +interface Circle { + type: "circle"; + radius: number; +} +interface Triangle { + type: "triangle"; + base: number; + height: number; +} +interface Rectangle { + type: "square"; + width: number; + height: number; +}