File tree 4 files changed +28
-1
lines changed
src/_vendor/zod-to-json-schema
4 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ export type Options<Target extends Targets = 'jsonSchema7'> = {
27
27
applyRegexFlags : boolean ;
28
28
emailStrategy : 'format:email' | 'format:idn-email' | 'pattern:zod' ;
29
29
base64Strategy : 'format:binary' | 'contentEncoding:base64' | 'pattern:zod' ;
30
- nameStrategy : 'ref' | 'title' ;
30
+ nameStrategy : 'ref' | 'duplicate-ref' | ' title';
31
31
override ?: (
32
32
def : ZodTypeDef ,
33
33
refs : Refs ,
Original file line number Diff line number Diff line change @@ -4,6 +4,12 @@ import { JsonSchema7Type } from './parseDef';
4
4
5
5
export type Refs = {
6
6
seen : Map < ZodTypeDef , Seen > ;
7
+ /**
8
+ * Set of all the `$ref`s we created, e.g. `Set(['#/$defs/ui'])`
9
+ * this notable does not include any `definitions` that were
10
+ * explicitly given as an option.
11
+ */
12
+ seenRefs : Set < string > ;
7
13
currentPath : string [ ] ;
8
14
propertyPath : string [ ] | undefined ;
9
15
} & Options < Targets > ;
@@ -24,6 +30,7 @@ export const getRefs = (options?: string | Partial<Options<Targets>>): Refs => {
24
30
..._options ,
25
31
currentPath : currentPath ,
26
32
propertyPath : undefined ,
33
+ seenRefs : new Set ( ) ,
27
34
seen : new Map (
28
35
Object . entries ( _options . definitions ) . map ( ( [ name , def ] ) => [
29
36
def . _def ,
Original file line number Diff line number Diff line change @@ -87,6 +87,10 @@ export function parseDef(
87
87
const seenSchema = get$ref ( seenItem , refs ) ;
88
88
89
89
if ( seenSchema !== undefined ) {
90
+ if ( '$ref' in seenSchema ) {
91
+ refs . seenRefs . add ( seenSchema . $ref ) ;
92
+ }
93
+
90
94
return seenSchema ;
91
95
}
92
96
}
Original file line number Diff line number Diff line change @@ -61,6 +61,8 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
61
61
main . title = title ;
62
62
}
63
63
64
+ const rootRefPath = name ? [ ...refs . basePath , refs . definitionPath , name ] . join ( '/' ) : null ;
65
+
64
66
const combined : ReturnType < typeof zodToJsonSchema < Target > > =
65
67
name === undefined ?
66
68
definitions ?
@@ -69,6 +71,20 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
69
71
[ refs . definitionPath ] : definitions ,
70
72
}
71
73
: main
74
+ : refs . nameStrategy === 'duplicate-ref' ?
75
+ {
76
+ ...main ,
77
+ ...( definitions || refs . seenRefs . has ( rootRefPath ! ) ?
78
+ {
79
+ [ refs . definitionPath ] : {
80
+ ...definitions ,
81
+ // only actually duplicate the schema definition if it was ever referenced
82
+ // otherwise the duplication is completely pointless
83
+ ...( refs . seenRefs . has ( rootRefPath ! ) ? { [ name ] : main } : undefined ) ,
84
+ } ,
85
+ }
86
+ : undefined ) ,
87
+ }
72
88
: {
73
89
$ref : [ ...( refs . $refStrategy === 'relative' ? [ ] : refs . basePath ) , refs . definitionPath , name ] . join (
74
90
'/' ,
You can’t perform that action at this time.
0 commit comments