Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix additionalProperties' separation problem #107

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading