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

Commit 85252d3

Browse files
authored
Merge pull request #13 from wrtnio/features/schema
Remove `oneOf.discriminator` property.
2 parents 88d427d + 63a4180 commit 85252d3

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wrtnio/openai-function-schema",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "OpenAI LLM function schema from OpenAPI (Swagger) document",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
@@ -37,16 +37,16 @@
3737
"author": "",
3838
"license": "ISC",
3939
"dependencies": {
40-
"@nestia/fetcher": "^3.4.1",
41-
"@samchon/openapi": "^0.3.0",
40+
"@nestia/fetcher": "^3.5.0",
41+
"@samchon/openapi": "^0.3.1",
4242
"commander": "^10.0.0",
4343
"inquirer": "^8.2.5",
44-
"typia": "^6.4.0"
44+
"typia": "^6.4.3"
4545
},
4646
"devDependencies": {
47-
"@nestia/core": "^3.4.1",
47+
"@nestia/core": "^3.5.0",
4848
"@nestia/e2e": "^0.6.0",
49-
"@nestia/sdk": "^3.4.1",
49+
"@nestia/sdk": "^3.5.0",
5050
"@nestjs/common": "^10.3.10",
5151
"@nestjs/core": "^10.3.10",
5252
"@nestjs/platform-express": "^10.3.10",

src/OpenAiComposer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { OpenApiV3Downgrader } from "@samchon/openapi/lib/internal/OpenApiV3Down
1111
import typia from "typia";
1212

1313
import { OpenAiSchemaSeparator } from "./internal/OpenAiSchemaSeparator";
14-
import { IOpenAiSchema, ISwaggerOperation } from "./module";
14+
import { IOpenAiSchema, ISwaggerOperation, OpenAiTypeChecker } from "./module";
1515
import { IOpenAiDocument } from "./structures/IOpenAiDocument";
1616
import { IOpenAiFunction } from "./structures/IOpenAiFunction";
1717
import { ISwagger } from "./structures/ISwagger";
@@ -162,11 +162,18 @@ export namespace OpenAiComposer {
162162
new Set(),
163163
)(schema);
164164
if (escaped === null) return null;
165-
const downgraded = OpenApiV3Downgrader.downgradeSchema({
165+
const downgraded: IOpenAiSchema = OpenApiV3Downgrader.downgradeSchema({
166166
original: {},
167167
downgraded: {},
168168
})(escaped);
169-
return downgraded as IOpenAiSchema;
169+
OpenAiTypeChecker.visit(downgraded, (schema) => {
170+
if (
171+
OpenAiTypeChecker.isOneOf(schema) &&
172+
(schema as any).discriminator !== undefined
173+
)
174+
delete (schema as any).discriminator;
175+
});
176+
return downgraded;
170177
};
171178

172179
const composeFunction =
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { TestValidator } from "@nestia/e2e";
2+
import { OpenApi } from "@samchon/openapi";
3+
import { IOpenAiSchema, OpenAiComposer } from "@wrtnio/openai-function-schema";
4+
import typia, { IJsonApplication } from "typia";
5+
6+
export const test_composer_schema_oneof = (): void => {
7+
const app: IJsonApplication =
8+
typia.json.application<[Circle | Triangle | Rectangle]>();
9+
const schema: OpenApi.IJsonSchema = app.schemas[0];
10+
const casted: IOpenAiSchema | null = OpenAiComposer.schema(
11+
app.components,
12+
schema,
13+
);
14+
TestValidator.equals("oneOf")(casted)({
15+
oneOf: [
16+
{
17+
type: "object",
18+
properties: {
19+
type: {
20+
type: "string",
21+
enum: ["circle"],
22+
},
23+
radius: {
24+
type: "number",
25+
},
26+
},
27+
required: ["type", "radius"],
28+
},
29+
{
30+
type: "object",
31+
properties: {
32+
type: {
33+
type: "string",
34+
enum: ["triangle"],
35+
},
36+
base: {
37+
type: "number",
38+
},
39+
height: {
40+
type: "number",
41+
},
42+
},
43+
required: ["type", "base", "height"],
44+
},
45+
{
46+
type: "object",
47+
properties: {
48+
type: {
49+
type: "string",
50+
enum: ["square"],
51+
},
52+
width: {
53+
type: "number",
54+
},
55+
height: {
56+
type: "number",
57+
},
58+
},
59+
required: ["type", "width", "height"],
60+
},
61+
],
62+
...{ discriminator: undefined },
63+
});
64+
};
65+
66+
interface Circle {
67+
type: "circle";
68+
radius: number;
69+
}
70+
interface Triangle {
71+
type: "triangle";
72+
base: number;
73+
height: number;
74+
}
75+
interface Rectangle {
76+
type: "square";
77+
width: number;
78+
height: number;
79+
}

0 commit comments

Comments
 (0)