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 circular ref array of recursive objects #1913

Closed
Closed
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
16 changes: 8 additions & 8 deletions packages/fets/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ type RangedJSONSchema<T extends { minimum: number; maximum: number }> = T extend
} ? MIN extends number ? MAX extends number ? IntRange<MIN, MAX> : never : never : never;
*/

export type Circular<TJSONSchema extends JSONSchema> = TJSONSchema extends {
properties: { [key: string]: JSONSchema };
}
? TJSONSchema extends PropertyValue<TJSONSchema, Property<TJSONSchema>>
? true
: Circular<PropertyValue<TJSONSchema, Property<TJSONSchema>>>
: false;
type Circular<T> = T extends { $ref: infer R }
? { $ref: R }
: T extends { properties: Record<string, JSONSchema> }
? {
[K in keyof T]: Circular<T[K]>;
}
: T;

export type Property<TJSONSchema extends JSONSchema> = keyof TJSONSchema['properties'];
export type PropertyValue<
Expand All @@ -182,7 +182,7 @@ export type FromSchema<T> =
? FromSchemaOriginal<
T,
{
deserialize: Circular<T> extends false
deserialize: Circular<T> extends { $ref: any }
? [
{
pattern: {
Expand Down
65 changes: 58 additions & 7 deletions packages/fets/tests/client/circular-ref-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import type treeOAS from './fixtures/example-circular-ref-oas';
type NormalizedOAS = NormalizeOAS<typeof treeOAS>;

// So it does handle circular reference actually
type SchemaInOAS =
type TreeSchemaInOAS =
NormalizedOAS['paths']['/tree']['get']['responses']['200']['content']['application/json']['schema'];

type Test = FromSchema<SchemaInOAS>;
type TreeTest = FromSchema<TreeSchemaInOAS>;

const a: Test = {
const a: TreeTest = {
number: 1,
child: {
number: 2,
Expand All @@ -33,9 +33,9 @@ if (a.child?.child?.child) {
a.child.child.child.number = 1;
}

type Test2 = FromSchema<OASJSONResponseSchema<NormalizedOAS, '/tree', 'get', '200'>>;
type TreeTest2 = FromSchema<OASJSONResponseSchema<NormalizedOAS, '/tree', 'get', '200'>>;

const b: Test2 = {
const b: TreeTest2 = {
number: 1,
child: {
number: 2,
Expand All @@ -45,9 +45,9 @@ const b: Test2 = {
},
};

type Test3 = OASOutput<NormalizedOAS, '/tree', 'get', '200'>;
type TreeTest3 = OASOutput<NormalizedOAS, '/tree', 'get', '200'>;

const c: Test3 = {
const c: TreeTest3 = {
number: 1,
child: {
number: 2,
Expand All @@ -57,6 +57,57 @@ const c: Test3 = {
},
};

type TreeListSchemaInOAS =
NormalizedOAS['paths']['/tree-list']['get']['responses']['200']['content']['application/json']['schema'];

type TreeListTest = FromSchema<TreeListSchemaInOAS>;

const d: TreeListTest = {
number: 1,
children: [
{
number: 2,
get children() {
return [d];
},
},
],
};

if (d.children?.[0].children?.[0]?.children?.[0].number) {
// @ts-expect-error number is a number
d.children[0].children[0].children[0].number = 'a';
d.children[0].children[0].children[0].number = 1;
}

type TreeListTest2 = FromSchema<OASJSONResponseSchema<NormalizedOAS, '/tree-list', 'get', '200'>>;

const e: TreeListTest2 = {
number: 1,
children: [
{
number: 2,
get children() {
return [e];
},
},
],
};

type TreeListTest3 = OASOutput<NormalizedOAS, '/tree-list', 'get', '200'>;

const f: TreeListTest3 = {
number: 1,
children: [
{
number: 2,
get children() {
return [f];
},
},
],
};

const client = createClient<NormalizedOAS>({});

// Somehow here is a problem
Expand Down
39 changes: 39 additions & 0 deletions packages/fets/tests/client/fixtures/example-circular-ref-oas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,48 @@ export default {
},
},
},
"/tree-list": {
get: {
tags: ["tree-list"],
summary: "Get tree list",
description: "",
operationId: "getTreeList",
responses: {
"200": {
description: "successful operation",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/TreeNode",
},
},
},
},
"404": {
description: "Tree not found",
},
},
},
},
},
components: {
schemas: {
TreeNode: {
type: "object",
properties: {
number: {
type: 'integer',
format: 'int64',
example: 10,
},
children: {
type: "array",
items: {
$ref: "#/components/schemas/TreeNode",
},
},
},
},
Node: {
type: 'object',
properties: {
Expand Down
Loading