Skip to content

Commit

Permalink
Merge pull request #107 from samchon/feat/human
Browse files Browse the repository at this point in the history
Fix `additionalProperties`' separation problem
  • Loading branch information
samchon authored Dec 11, 2024
2 parents 2f0a6a5 + 11b508e commit 590e7e7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/openapi",
"version": "2.0.2",
"version": "2.0.3",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
15 changes: 8 additions & 7 deletions src/composers/llm/LlmSchemaV3Composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,15 @@ export namespace LlmSchemaV3Composer {
predicate: (schema: ILlmSchemaV3) => boolean;
schema: ILlmSchemaV3.IObject;
}): [ILlmSchemaV3.IObject | null, ILlmSchemaV3.IObject | null] => {
if (
!!props.schema.additionalProperties ||
Object.keys(props.schema.properties ?? {}).length === 0
)
return [props.schema, null];
const llm = {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3>,
additionalProperties: props.schema.additionalProperties,
} satisfies ILlmSchemaV3.IObject;
const human = {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3>,
additionalProperties: props.schema.additionalProperties,
} satisfies ILlmSchemaV3.IObject;
for (const [key, value] of Object.entries(props.schema.properties ?? {})) {
const [x, y] = separateStation({
Expand All @@ -252,8 +249,12 @@ export namespace LlmSchemaV3Composer {
human.additionalProperties = dy ?? false;
}
return [
Object.keys(llm.properties).length === 0 ? null : shrinkRequired(llm),
Object.keys(human.properties).length === 0 ? null : shrinkRequired(human),
!!Object.keys(llm.properties).length || !!llm.additionalProperties
? shrinkRequired(llm)
: null,
!!Object.keys(human.properties).length || !!human.additionalProperties
? shrinkRequired(human)
: null,
];
};

Expand Down
9 changes: 7 additions & 2 deletions src/composers/llm/LlmSchemaV3_1Composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ export namespace LlmSchemaV3_1Composer {
const llm = {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3_1>,
additionalProperties: props.schema.additionalProperties,
} satisfies ILlmSchemaV3_1.IObject;
const human = {
...props.schema,
Expand Down Expand Up @@ -454,8 +455,12 @@ export namespace LlmSchemaV3_1Composer {
human.additionalProperties = dy ?? false;
}
return [
Object.keys(llm.properties).length === 0 ? null : shrinkRequired(llm),
Object.keys(human.properties).length === 0 ? null : shrinkRequired(human),
!!Object.keys(llm.properties).length || !!llm.additionalProperties
? shrinkRequired(llm)
: null,
!!Object.keys(human.properties).length || human.additionalProperties
? shrinkRequired(human)
: null,
];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,68 @@ const validate_llm_parameters_separate_object_additionalProperties = <
: s.description?.includes("@contentMediaType") === true),
parameters: schema as any,
});
const member: ILlmSchema.IParameters<Model> = schema(
const params: ILlmSchema.IParameters<Model> = schema(
model,
constraint,
)(typia.json.schemas<[IWrapper<IMember>]>());
const upload: ILlmSchema.IParameters<Model> = schema(
model,
constraint,
)(typia.json.schemas<[IWrapper<IFileUpload>]>());
const combined: ILlmSchema.IParameters<Model> = schema(
model,
constraint,
)(typia.json.schemas<[IWrapper<ICombined>]>());

TestValidator.equals("member")(separator(member))({
llm: member,
human: null,
});
TestValidator.equals("upload")(separator(upload))({
llm: null,
human: upload,
});
TestValidator.equals("combined")(separator(combined))({
llm: member,
human: upload,
)(typia.json.schemas<[IParameters]>());
TestValidator.equals(model)(separator(params))({
llm: schema(
model,
constraint,
)(
typia.json.schemas<
[
{
input: {
email: string;
hobbies: Record<
string,
{
id: string;
name: string;
}
>;
};
},
]
>(),
),
human: schema(
model,
constraint,
)(
typia.json.schemas<
[
{
input: {
hobbies: Record<
string,
{
thumbnail: string &
tags.Format<"uri"> &
tags.ContentMediaType<"image/*">;
}
>;
};
},
]
>(),
),
});
};

interface IWrapper<T> {
data: T;
interface IParameters {
input: IMember;
}
interface IMember {
id: number;
name: string;
email: string;
hobbies: Record<string, IHobby>;
}
interface IFileUpload {
file: string & tags.Format<"uri"> & tags.ContentMediaType<"image/png">;
interface IHobby {
id: string;
name: string;
thumbnail: string & tags.Format<"uri"> & tags.ContentMediaType<"image/*">;
}
interface ICombined extends IMember, IFileUpload {}

const schema =
<Model extends ILlmSchema.Model>(model: Model, constraint: boolean) =>
Expand Down

0 comments on commit 590e7e7

Please sign in to comment.