Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/fets/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export type Circular<TJSONSchema extends JSONSchema> = TJSONSchema extends {
? TJSONSchema extends PropertyValue<TJSONSchema, Property<TJSONSchema>>
? true
: Circular<PropertyValue<TJSONSchema, Property<TJSONSchema>>>
: false;
: TJSONSchema extends { items: JSONSchema }
? true
: false;

export type Property<TJSONSchema extends JSONSchema> = keyof TJSONSchema['properties'];
export type PropertyValue<
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