From d27075c5f536a86a6c81cb3c3cec92b302663e2e Mon Sep 17 00:00:00 2001 From: zyoshoka <107108195+zyoshoka@users.noreply.github.com> Date: Tue, 27 May 2025 08:57:09 +0900 Subject: fix(backend): correct invalid schema format specifying only `required` for `anyOf` (#16089) * fix(backend): correct invalid schema format specifying only `required` for `anyOf` * refactor(backend): make types derived from `allOf` or `anyOf` more strong --- packages/backend/src/misc/json-schema.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'packages/backend/src/misc/json-schema.ts') diff --git a/packages/backend/src/misc/json-schema.ts b/packages/backend/src/misc/json-schema.ts index 23f6b692a7..5e5d7041b9 100644 --- a/packages/backend/src/misc/json-schema.ts +++ b/packages/backend/src/misc/json-schema.ts @@ -218,7 +218,17 @@ type NullOrUndefined

= // https://stackoverflow.com/questions/54938141/typescript-convert-union-to-intersection // Get intersection from union type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; -type PartialIntersection = Partial>; + +type ArrayToIntersection> = + T extends readonly [infer Head, ...infer Tail] + ? Head extends Schema + ? Tail extends ReadonlyArray + ? Tail extends [] + ? SchemaType + : SchemaType & ArrayToIntersection + : never + : never + : never; // https://github.com/misskey-dev/misskey/pull/8144#discussion_r785287552 // To get union, we use `Foo extends any ? Hoge : never` @@ -236,8 +246,8 @@ type ObjectSchemaTypeDef

= : never : ObjType> : - p['anyOf'] extends ReadonlyArray ? never : // see CONTRIBUTING.md - p['allOf'] extends ReadonlyArray ? UnionToIntersection> : + p['anyOf'] extends ReadonlyArray ? UnionSchemaType : + p['allOf'] extends ReadonlyArray ? ArrayToIntersection : p['additionalProperties'] extends true ? Record : p['additionalProperties'] extends Schema ? p['additionalProperties'] extends infer AdditionalProperties ? @@ -277,7 +287,8 @@ export type SchemaTypeDef

= p['items'] extends NonNullable ? SchemaType[] : any[] ) : - p['anyOf'] extends ReadonlyArray ? UnionSchemaType & PartialIntersection> : + p['anyOf'] extends ReadonlyArray ? UnionSchemaType : + p['allOf'] extends ReadonlyArray ? ArrayToIntersection : p['oneOf'] extends ReadonlyArray ? UnionSchemaType : any; -- cgit v1.2.3-freya