Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Merge pull request #13 from wrtnio/features/schema
Browse files Browse the repository at this point in the history
Remove `oneOf.discriminator` property.
  • Loading branch information
samchon authored Jul 8, 2024
2 parents 88d427d + 63a4180 commit 85252d3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 10 additions & 3 deletions src/OpenAiComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 =
Expand Down
79 changes: 79 additions & 0 deletions test/features/composer/test_composer_schema_oneof.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 85252d3

Please sign in to comment.