Skip to content

Commit

Permalink
Fix nested circular references (#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Aug 16, 2024
1 parent c23d785 commit 047ecec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-pigs-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fets': patch
---

Fix nested circular references
22 changes: 18 additions & 4 deletions packages/fets/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ 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;

export type Property<TJSONSchema extends JSONSchema> = keyof TJSONSchema['properties'];
export type PropertyValue<
TJSONSchema extends JSONSchema,
TProperty extends keyof TJSONSchema['properties'],
> = TJSONSchema['properties'][TProperty];

export type FromSchema<T> =
/* T extends { type: 'integer'; minimum: number; maximum: number } ? RangedJSONSchema<T> : */ T extends {
static: infer U;
Expand All @@ -168,9 +182,8 @@ export type FromSchema<T> =
? FromSchemaOriginal<
T,
{
deserialize: T extends T['properties'][keyof T['properties']]
? false
: [
deserialize: Circular<T> extends false
? [
{
pattern: {
type: 'string';
Expand All @@ -192,7 +205,8 @@ export type FromSchema<T> =
};
output: bigint | number;
},
];
]
: false;
}
>
: never;
Expand Down
12 changes: 12 additions & 0 deletions packages/fets/tests/client/circular-ref-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createClient,
OASJSONResponseSchema,
OASModel,
OASOutput,
type FromSchema,
type NormalizeOAS,
Expand Down Expand Up @@ -72,3 +73,14 @@ if (response.ok) {
} else {
console.log(response.status);
}

type NodeA = OASModel<NormalizedOAS, 'Node'>;
const nodeA = {} as NodeA;
const numberA = nodeA.child?.child?.child?.child?.number;
type NumberA = typeof numberA;
let numberAVar: NumberA;
numberAVar = 2;
// @ts-expect-error - numberAVar is a number
numberAVar = 'a';

console.log(numberAVar);

0 comments on commit 047ecec

Please sign in to comment.