zod-openapi 5.0.0-beta.2 → 5.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components.chunk.cjs +95 -157
- package/dist/components.chunk.mjs +184 -246
- package/dist/create/components.d.ts +11 -31
- package/dist/create/schema/schema.d.ts +2 -2
- package/package.json +1 -1
|
@@ -622,9 +622,7 @@ const createSchema = (schema, ctx = {
|
|
|
622
622
|
ctx.registry ?? (ctx.registry = createRegistry());
|
|
623
623
|
ctx.opts ?? (ctx.opts = {});
|
|
624
624
|
ctx.io ?? (ctx.io = "output");
|
|
625
|
-
const registrySchemas = Object.fromEntries(
|
|
626
|
-
ctx.registry.schemas[ctx.io].schemas
|
|
627
|
-
);
|
|
625
|
+
const registrySchemas = Object.fromEntries(ctx.registry.schemas[ctx.io]);
|
|
628
626
|
const schemas = {
|
|
629
627
|
zodOpenApiCreateSchema: { zodType: schema }
|
|
630
628
|
};
|
|
@@ -639,181 +637,121 @@ const createSchema = (schema, ctx = {
|
|
|
639
637
|
components: jsonSchemas.components
|
|
640
638
|
};
|
|
641
639
|
};
|
|
642
|
-
const createSchemas = (schemas, {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
opts
|
|
646
|
-
}) => {
|
|
647
|
-
var _a, _b, _c, _d, _e, _f;
|
|
648
|
-
const schemaRegistry = v4.registry();
|
|
649
|
-
const globalsInSchemas = /* @__PURE__ */ new Map();
|
|
640
|
+
const createSchemas = (schemas, ctx) => {
|
|
641
|
+
var _a, _b;
|
|
642
|
+
const entries = {};
|
|
650
643
|
for (const [name, { zodType }] of Object.entries(schemas)) {
|
|
651
|
-
|
|
652
|
-
if (id) {
|
|
653
|
-
globalsInSchemas.set(name, id);
|
|
654
|
-
}
|
|
655
|
-
schemaRegistry.add(zodType, { id: name });
|
|
644
|
+
entries[name] = zodType;
|
|
656
645
|
}
|
|
646
|
+
const schemaRegistry = v4.object(entries);
|
|
657
647
|
const outputIds = /* @__PURE__ */ new Map();
|
|
658
648
|
const jsonSchema = v4.toJSONSchema(schemaRegistry, {
|
|
659
|
-
override(
|
|
660
|
-
const meta =
|
|
649
|
+
override(context) {
|
|
650
|
+
const meta = context.zodSchema.meta();
|
|
661
651
|
if ((meta == null ? void 0 : meta.outputId) && (meta == null ? void 0 : meta.id)) {
|
|
662
652
|
outputIds.set(meta.id, meta.outputId);
|
|
663
653
|
}
|
|
664
|
-
if (
|
|
654
|
+
if (context.jsonSchema.$ref) {
|
|
665
655
|
return;
|
|
666
656
|
}
|
|
667
|
-
const enrichedContext = { ...
|
|
657
|
+
const enrichedContext = { ...context, io: ctx.io };
|
|
668
658
|
override(enrichedContext);
|
|
669
|
-
if (typeof opts.override === "function") {
|
|
670
|
-
opts.override(enrichedContext);
|
|
659
|
+
if (typeof ctx.opts.override === "function") {
|
|
660
|
+
ctx.opts.override(enrichedContext);
|
|
671
661
|
}
|
|
672
662
|
if (typeof (meta == null ? void 0 : meta.override) === "function") {
|
|
673
663
|
meta.override(enrichedContext);
|
|
674
|
-
delete
|
|
664
|
+
delete context.jsonSchema.override;
|
|
675
665
|
}
|
|
676
666
|
if (typeof (meta == null ? void 0 : meta.override) === "object" && meta.override !== null) {
|
|
677
|
-
Object.assign(
|
|
678
|
-
delete
|
|
667
|
+
Object.assign(context.jsonSchema, meta.override);
|
|
668
|
+
delete context.jsonSchema.override;
|
|
679
669
|
}
|
|
680
|
-
delete
|
|
681
|
-
delete
|
|
682
|
-
deleteZodOpenApiMeta(
|
|
683
|
-
validate(enrichedContext, opts);
|
|
670
|
+
delete context.jsonSchema.$schema;
|
|
671
|
+
delete context.jsonSchema.id;
|
|
672
|
+
deleteZodOpenApiMeta(context.jsonSchema);
|
|
673
|
+
validate(enrichedContext, ctx.opts);
|
|
684
674
|
},
|
|
685
|
-
io,
|
|
686
|
-
unrepresentable: "any"
|
|
687
|
-
uri: (id) => `#/components/schemas/${id}`
|
|
675
|
+
io: ctx.io,
|
|
676
|
+
unrepresentable: "any"
|
|
688
677
|
});
|
|
689
|
-
const
|
|
690
|
-
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
continue;
|
|
699
|
-
}
|
|
700
|
-
componentsToReplace.set(`__shared#/$defs/${key}`, key);
|
|
701
|
-
}
|
|
702
|
-
for (const value of Object.values(jsonSchema.schemas)) {
|
|
703
|
-
delete value.$schema;
|
|
704
|
-
delete value.id;
|
|
705
|
-
}
|
|
706
|
-
const dynamicComponent = /* @__PURE__ */ new Map();
|
|
707
|
-
const patched = JSON.stringify(jsonSchema).replace(
|
|
708
|
-
/"#\/components\/schemas\/([^"]+)"/g,
|
|
709
|
-
(_, match) => {
|
|
710
|
-
const replacement = componentsToReplace.get(match);
|
|
711
|
-
if (replacement) {
|
|
712
|
-
return `"#/components/schemas/${replacement}"`;
|
|
713
|
-
}
|
|
714
|
-
const component = registry.schemas.ids.get(match);
|
|
715
|
-
if (component) {
|
|
716
|
-
return `"#/components/schemas/${match}`;
|
|
717
|
-
}
|
|
718
|
-
const globalInSchema = globalsInSchemas.get(match);
|
|
719
|
-
if (globalInSchema) {
|
|
720
|
-
componentsToReplace.set(match, globalInSchema);
|
|
721
|
-
dynamicComponent.set(match, globalInSchema);
|
|
722
|
-
return `"#/components/schemas/${globalInSchema}"`;
|
|
723
|
-
}
|
|
724
|
-
const manualSchema = registry.schemas.manual.get(match);
|
|
725
|
-
if (manualSchema) {
|
|
726
|
-
componentsToReplace.set(match, manualSchema.key);
|
|
727
|
-
dynamicComponent.set(match, manualSchema.key);
|
|
728
|
-
manualSchema.io[io].used = true;
|
|
729
|
-
return `"#/components/schemas/${manualSchema.key}"`;
|
|
678
|
+
const components = jsonSchema.$defs ?? {};
|
|
679
|
+
const dynamicComponents = /* @__PURE__ */ new Map();
|
|
680
|
+
for (const [key, value] of Object.entries(components)) {
|
|
681
|
+
if (/^__schema\d+$/.test(key)) {
|
|
682
|
+
const newName = `__schema${ctx.registry.schemas.dynamicSchemaCount++}`;
|
|
683
|
+
dynamicComponents.set(key, `"#/components/schemas/${newName}"`);
|
|
684
|
+
if (newName !== key) {
|
|
685
|
+
components[newName] = value;
|
|
686
|
+
delete components[key];
|
|
730
687
|
}
|
|
731
|
-
const componentName = `__schema${registry.schemas.dynamicSchemaCount++}`;
|
|
732
|
-
componentsToReplace.set(match, componentName);
|
|
733
|
-
dynamicComponent.set(match, componentName);
|
|
734
|
-
return `"#/components/schemas/${componentName}"`;
|
|
735
688
|
}
|
|
689
|
+
}
|
|
690
|
+
const parsedJsonSchema = JSON.parse(
|
|
691
|
+
JSON.stringify(jsonSchema).replace(
|
|
692
|
+
/"#\/\$defs\/([^"]+)"/g,
|
|
693
|
+
(_, match) => {
|
|
694
|
+
const dynamic = dynamicComponents.get(match);
|
|
695
|
+
if (dynamic) {
|
|
696
|
+
return dynamic;
|
|
697
|
+
}
|
|
698
|
+
const manualComponent = ctx.registry.schemas.manual.get(match);
|
|
699
|
+
if (manualComponent) {
|
|
700
|
+
manualComponent.io[ctx.io].used++;
|
|
701
|
+
}
|
|
702
|
+
return `"#/components/schemas/${match}"`;
|
|
703
|
+
}
|
|
704
|
+
)
|
|
736
705
|
);
|
|
737
|
-
const
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
dynamicComponent.set(key, value.key);
|
|
706
|
+
for (const [key, value] of ctx.registry.schemas.manual) {
|
|
707
|
+
if (value.io[ctx.io].used === 1 && ((_a = parsedJsonSchema.properties) == null ? void 0 : _a[value.identifier]) && ((_b = parsedJsonSchema.$defs) == null ? void 0 : _b[key])) {
|
|
708
|
+
parsedJsonSchema.properties[value.identifier] = parsedJsonSchema.$defs[key];
|
|
709
|
+
delete parsedJsonSchema.$defs[key];
|
|
710
|
+
continue;
|
|
743
711
|
}
|
|
744
712
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
$ref: `#/components/schemas/${value}`
|
|
752
|
-
};
|
|
753
|
-
components[value] = component;
|
|
754
|
-
}
|
|
755
|
-
const renamedComponents = renameComponents(components, outputIds, {
|
|
756
|
-
registry
|
|
757
|
-
});
|
|
758
|
-
if (renamedComponents.size === 0) {
|
|
759
|
-
delete patchedJsonSchema.schemas.__shared;
|
|
713
|
+
const renamedComponents = renameComponents(
|
|
714
|
+
parsedJsonSchema.$defs ?? {},
|
|
715
|
+
outputIds,
|
|
716
|
+
ctx
|
|
717
|
+
);
|
|
718
|
+
if (!renamedComponents.size) {
|
|
760
719
|
return {
|
|
761
|
-
schemas:
|
|
762
|
-
components
|
|
720
|
+
schemas: parsedJsonSchema.properties,
|
|
721
|
+
components: parsedJsonSchema.$defs ?? {}
|
|
763
722
|
};
|
|
764
723
|
}
|
|
765
|
-
const
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
724
|
+
const renamedJsonSchema = JSON.parse(
|
|
725
|
+
JSON.stringify(parsedJsonSchema).replace(
|
|
726
|
+
/"#\/components\/schemas\/([^"]+)"/g,
|
|
727
|
+
(_, match) => {
|
|
728
|
+
const replacement = renamedComponents.get(match);
|
|
729
|
+
if (replacement) {
|
|
730
|
+
return `"#/components/schemas/${replacement}"`;
|
|
731
|
+
}
|
|
732
|
+
return `"#/components/schemas/${match}"`;
|
|
771
733
|
}
|
|
772
|
-
|
|
773
|
-
}
|
|
734
|
+
)
|
|
774
735
|
);
|
|
775
|
-
const renamedJsonSchema = JSON.parse(renamedStringified);
|
|
776
|
-
const renamedJsonSchemaComponents = ((_f = renamedJsonSchema.schemas.__shared) == null ? void 0 : _f.$defs) ?? {};
|
|
777
|
-
delete renamedJsonSchema.schemas.__shared;
|
|
778
736
|
return {
|
|
779
|
-
schemas: renamedJsonSchema.
|
|
780
|
-
components:
|
|
737
|
+
schemas: renamedJsonSchema.properties,
|
|
738
|
+
components: renamedJsonSchema.$defs ?? {}
|
|
781
739
|
};
|
|
782
740
|
};
|
|
783
741
|
const createRegistry = (components) => {
|
|
784
742
|
const registry = {
|
|
785
743
|
schemas: {
|
|
786
744
|
dynamicSchemaCount: 0,
|
|
787
|
-
input:
|
|
788
|
-
|
|
789
|
-
schemas: /* @__PURE__ */ new Map()
|
|
790
|
-
},
|
|
791
|
-
output: {
|
|
792
|
-
seen: /* @__PURE__ */ new WeakMap(),
|
|
793
|
-
schemas: /* @__PURE__ */ new Map()
|
|
794
|
-
},
|
|
745
|
+
input: /* @__PURE__ */ new Map(),
|
|
746
|
+
output: /* @__PURE__ */ new Map(),
|
|
795
747
|
ids: /* @__PURE__ */ new Map(),
|
|
796
748
|
manual: /* @__PURE__ */ new Map(),
|
|
797
749
|
setSchema: (key, schema, io) => {
|
|
798
|
-
const seenSchema = registry.schemas[io].seen.get(schema);
|
|
799
|
-
if (seenSchema) {
|
|
800
|
-
if (seenSchema.type === "manual") {
|
|
801
|
-
const manualSchema = registry.schemas.manual.get(seenSchema.id);
|
|
802
|
-
if (!manualSchema) {
|
|
803
|
-
throw new Error(
|
|
804
|
-
`Manual schema "${key}" not found in registry for ${io} IO.`
|
|
805
|
-
);
|
|
806
|
-
}
|
|
807
|
-
manualSchema.io[io].used = true;
|
|
808
|
-
}
|
|
809
|
-
return seenSchema.schemaObject;
|
|
810
|
-
}
|
|
811
750
|
const schemaObject = {};
|
|
812
|
-
registry.schemas[io].
|
|
751
|
+
registry.schemas[io].set(key, {
|
|
813
752
|
schemaObject,
|
|
814
753
|
zodType: schema
|
|
815
754
|
});
|
|
816
|
-
registry.schemas[io].seen.set(schema, { type: "schema", schemaObject });
|
|
817
755
|
return schemaObject;
|
|
818
756
|
}
|
|
819
757
|
},
|
|
@@ -863,36 +801,36 @@ const registerSchemas = (schemas, registry) => {
|
|
|
863
801
|
const inputSchemaObject = {};
|
|
864
802
|
const outputSchemaObject = {};
|
|
865
803
|
const identifier = `components > schemas > ${key}`;
|
|
866
|
-
registry.schemas.input.
|
|
804
|
+
registry.schemas.input.set(identifier, {
|
|
867
805
|
zodType: schema,
|
|
868
806
|
schemaObject: inputSchemaObject
|
|
869
807
|
});
|
|
870
|
-
registry.schemas.
|
|
871
|
-
type: "manual",
|
|
872
|
-
schemaObject: inputSchemaObject,
|
|
873
|
-
id: identifier
|
|
874
|
-
});
|
|
875
|
-
registry.schemas.output.schemas.set(identifier, {
|
|
808
|
+
registry.schemas.output.set(identifier, {
|
|
876
809
|
zodType: schema,
|
|
877
810
|
schemaObject: outputSchemaObject
|
|
878
811
|
});
|
|
879
|
-
registry.schemas.
|
|
880
|
-
|
|
881
|
-
schemaObject: outputSchemaObject,
|
|
882
|
-
id: identifier
|
|
883
|
-
});
|
|
884
|
-
registry.schemas.manual.set(identifier, {
|
|
885
|
-
key,
|
|
812
|
+
registry.schemas.manual.set(key, {
|
|
813
|
+
identifier,
|
|
886
814
|
io: {
|
|
887
815
|
input: {
|
|
888
|
-
schemaObject: inputSchemaObject
|
|
816
|
+
schemaObject: inputSchemaObject,
|
|
817
|
+
used: 0
|
|
889
818
|
},
|
|
890
819
|
output: {
|
|
891
|
-
schemaObject: outputSchemaObject
|
|
820
|
+
schemaObject: outputSchemaObject,
|
|
821
|
+
used: 0
|
|
892
822
|
}
|
|
893
823
|
},
|
|
894
824
|
zodType: schema
|
|
895
825
|
});
|
|
826
|
+
const meta = core.globalRegistry.get(schema);
|
|
827
|
+
if (meta == null ? void 0 : meta.id) {
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
core.globalRegistry.add(schema, {
|
|
831
|
+
...meta,
|
|
832
|
+
id: key
|
|
833
|
+
});
|
|
896
834
|
continue;
|
|
897
835
|
}
|
|
898
836
|
registry.schemas.ids.set(key, schema);
|
|
@@ -1025,14 +963,14 @@ const registerPathItems = (pathItems, registry) => {
|
|
|
1025
963
|
};
|
|
1026
964
|
const createIOSchemas = (ctx) => {
|
|
1027
965
|
const { schemas, components } = createSchemas(
|
|
1028
|
-
Object.fromEntries(ctx.registry.schemas[ctx.io]
|
|
966
|
+
Object.fromEntries(ctx.registry.schemas[ctx.io]),
|
|
1029
967
|
ctx
|
|
1030
968
|
);
|
|
1031
969
|
for (const [key, schema] of Object.entries(components)) {
|
|
1032
970
|
ctx.registry.schemas.ids.set(key, schema);
|
|
1033
971
|
}
|
|
1034
972
|
for (const [key, schema] of Object.entries(schemas)) {
|
|
1035
|
-
const ioSchema = ctx.registry.schemas[ctx.io].
|
|
973
|
+
const ioSchema = ctx.registry.schemas[ctx.io].get(key);
|
|
1036
974
|
if (ioSchema) {
|
|
1037
975
|
Object.assign(ioSchema.schemaObject, schema);
|
|
1038
976
|
}
|
|
@@ -1040,11 +978,11 @@ const createIOSchemas = (ctx) => {
|
|
|
1040
978
|
};
|
|
1041
979
|
const createManualSchemas = (registry) => {
|
|
1042
980
|
var _a;
|
|
1043
|
-
for (const [, value] of registry.schemas.manual) {
|
|
1044
|
-
if (
|
|
981
|
+
for (const [key, value] of registry.schemas.manual) {
|
|
982
|
+
if (value.io.input.used === 1) {
|
|
1045
983
|
const io = ((_a = core.globalRegistry.get(value.zodType)) == null ? void 0 : _a.unusedIO) ?? "output";
|
|
1046
984
|
const schema = value.io[io].schemaObject;
|
|
1047
|
-
registry.schemas.ids.set(
|
|
985
|
+
registry.schemas.ids.set(key, schema);
|
|
1048
986
|
}
|
|
1049
987
|
}
|
|
1050
988
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { globalRegistry as globalRegistry$1 } from "zod/v4/core";
|
|
2
|
-
import { globalRegistry,
|
|
2
|
+
import { globalRegistry, object, toJSONSchema } from "zod/v4";
|
|
3
3
|
const isAnyZodType = (schema) => typeof schema === "object" && schema !== null && "_zod" in schema;
|
|
4
4
|
const unwrapZodObject = (zodType, io, path) => {
|
|
5
5
|
const def = zodType._zod.def;
|
|
@@ -273,7 +273,7 @@ const createResponses = (responses, ctx, path) => {
|
|
|
273
273
|
}
|
|
274
274
|
return responsesObject;
|
|
275
275
|
};
|
|
276
|
-
const createOperation = (operationObject,
|
|
276
|
+
const createOperation = (operationObject, registry, path) => {
|
|
277
277
|
const {
|
|
278
278
|
parameters,
|
|
279
279
|
requestParams,
|
|
@@ -286,7 +286,7 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
286
286
|
const maybeManualParameters = createManualParameters(
|
|
287
287
|
parameters,
|
|
288
288
|
{
|
|
289
|
-
registry
|
|
289
|
+
registry,
|
|
290
290
|
io: "input"
|
|
291
291
|
},
|
|
292
292
|
[...path, "parameters"]
|
|
@@ -294,7 +294,7 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
294
294
|
const maybeRequestParams = createParameters(
|
|
295
295
|
requestParams,
|
|
296
296
|
{
|
|
297
|
-
registry
|
|
297
|
+
registry,
|
|
298
298
|
io: "input"
|
|
299
299
|
},
|
|
300
300
|
[...path, "requestParams"]
|
|
@@ -308,7 +308,7 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
308
308
|
const maybeRequestBody = createRequestBody(
|
|
309
309
|
requestBody,
|
|
310
310
|
{
|
|
311
|
-
registry
|
|
311
|
+
registry,
|
|
312
312
|
io: "input"
|
|
313
313
|
},
|
|
314
314
|
[...path, "requestBody"]
|
|
@@ -319,7 +319,7 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
319
319
|
const maybeResponses = createResponses(
|
|
320
320
|
responses,
|
|
321
321
|
{
|
|
322
|
-
registry
|
|
322
|
+
registry,
|
|
323
323
|
io: "output"
|
|
324
324
|
},
|
|
325
325
|
[...path, "responses"]
|
|
@@ -327,7 +327,7 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
327
327
|
if (maybeResponses) {
|
|
328
328
|
operation.responses = maybeResponses;
|
|
329
329
|
}
|
|
330
|
-
const maybeCallbacks = createCallbacks(callbacks,
|
|
330
|
+
const maybeCallbacks = createCallbacks(callbacks, registry, [
|
|
331
331
|
...path,
|
|
332
332
|
"callbacks"
|
|
333
333
|
]);
|
|
@@ -336,14 +336,14 @@ const createOperation = (operationObject, registry2, path) => {
|
|
|
336
336
|
}
|
|
337
337
|
return operation;
|
|
338
338
|
};
|
|
339
|
-
const createPathItem = (pathItem,
|
|
339
|
+
const createPathItem = (pathItem, registry, path) => {
|
|
340
340
|
const pathItemObject = {};
|
|
341
341
|
const { id, ...rest } = pathItem;
|
|
342
342
|
for (const [key, value] of Object.entries(rest)) {
|
|
343
343
|
if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
|
|
344
344
|
pathItemObject[key] = createOperation(
|
|
345
345
|
value,
|
|
346
|
-
|
|
346
|
+
registry,
|
|
347
347
|
[...path, key]
|
|
348
348
|
);
|
|
349
349
|
continue;
|
|
@@ -352,7 +352,7 @@ const createPathItem = (pathItem, registry2, path) => {
|
|
|
352
352
|
pathItemObject[key] = createManualParameters(
|
|
353
353
|
value,
|
|
354
354
|
{
|
|
355
|
-
registry
|
|
355
|
+
registry,
|
|
356
356
|
io: "input"
|
|
357
357
|
},
|
|
358
358
|
[...path, key]
|
|
@@ -365,14 +365,14 @@ const createPathItem = (pathItem, registry2, path) => {
|
|
|
365
365
|
const ref = {
|
|
366
366
|
$ref: `#/components/pathItems/${id}`
|
|
367
367
|
};
|
|
368
|
-
|
|
369
|
-
|
|
368
|
+
registry.pathItems.ids.set(id, pathItemObject);
|
|
369
|
+
registry.pathItems.seen.set(pathItem, ref);
|
|
370
370
|
return ref;
|
|
371
371
|
}
|
|
372
|
-
|
|
372
|
+
registry.pathItems.seen.set(pathItem, pathItemObject);
|
|
373
373
|
return pathItemObject;
|
|
374
374
|
};
|
|
375
|
-
const createPaths = (paths,
|
|
375
|
+
const createPaths = (paths, registry, path) => {
|
|
376
376
|
if (!paths) {
|
|
377
377
|
return void 0;
|
|
378
378
|
}
|
|
@@ -382,15 +382,15 @@ const createPaths = (paths, registry2, path) => {
|
|
|
382
382
|
pathsObject[singlePath] = pathItemObject;
|
|
383
383
|
continue;
|
|
384
384
|
}
|
|
385
|
-
pathsObject[singlePath] = createPathItem(pathItemObject,
|
|
385
|
+
pathsObject[singlePath] = createPathItem(pathItemObject, registry, [
|
|
386
386
|
...path,
|
|
387
387
|
singlePath
|
|
388
388
|
]);
|
|
389
389
|
}
|
|
390
390
|
return pathsObject;
|
|
391
391
|
};
|
|
392
|
-
const createCallback = (callbackObject,
|
|
393
|
-
const seenCallback =
|
|
392
|
+
const createCallback = (callbackObject, registry, path) => {
|
|
393
|
+
const seenCallback = registry.callbacks.seen.get(callbackObject);
|
|
394
394
|
if (seenCallback) {
|
|
395
395
|
return seenCallback;
|
|
396
396
|
}
|
|
@@ -403,7 +403,7 @@ const createCallback = (callbackObject, registry2, path) => {
|
|
|
403
403
|
}
|
|
404
404
|
callback[name] = createPathItem(
|
|
405
405
|
pathItem,
|
|
406
|
-
|
|
406
|
+
registry,
|
|
407
407
|
[...path, name]
|
|
408
408
|
);
|
|
409
409
|
}
|
|
@@ -411,14 +411,14 @@ const createCallback = (callbackObject, registry2, path) => {
|
|
|
411
411
|
const ref = {
|
|
412
412
|
$ref: `#/components/callbacks/${id}`
|
|
413
413
|
};
|
|
414
|
-
|
|
415
|
-
|
|
414
|
+
registry.callbacks.ids.set(id, callback);
|
|
415
|
+
registry.callbacks.seen.set(callbackObject, ref);
|
|
416
416
|
return ref;
|
|
417
417
|
}
|
|
418
|
-
|
|
418
|
+
registry.callbacks.seen.set(callbackObject, callback);
|
|
419
419
|
return callback;
|
|
420
420
|
};
|
|
421
|
-
const createCallbacks = (callbacks,
|
|
421
|
+
const createCallbacks = (callbacks, registry, path) => {
|
|
422
422
|
if (!callbacks) {
|
|
423
423
|
return void 0;
|
|
424
424
|
}
|
|
@@ -430,7 +430,7 @@ const createCallbacks = (callbacks, registry2, path) => {
|
|
|
430
430
|
}
|
|
431
431
|
callbacksObject[name] = createCallback(
|
|
432
432
|
value,
|
|
433
|
-
|
|
433
|
+
registry,
|
|
434
434
|
[...path, name]
|
|
435
435
|
);
|
|
436
436
|
}
|
|
@@ -579,7 +579,7 @@ const renameComponents = (components, outputIds, ctx) => {
|
|
|
579
579
|
}
|
|
580
580
|
return componentsToRename;
|
|
581
581
|
};
|
|
582
|
-
const isDependencyPure = (componentDependencies, stringifiedComponents,
|
|
582
|
+
const isDependencyPure = (componentDependencies, stringifiedComponents, registry, key, visited = /* @__PURE__ */ new Set()) => {
|
|
583
583
|
if (visited.has(key)) {
|
|
584
584
|
return true;
|
|
585
585
|
}
|
|
@@ -588,7 +588,7 @@ const isDependencyPure = (componentDependencies, stringifiedComponents, registry
|
|
|
588
588
|
return dependencies.pure;
|
|
589
589
|
}
|
|
590
590
|
const stringified = stringifiedComponents.get(key);
|
|
591
|
-
const component =
|
|
591
|
+
const component = registry.schemas.ids.get(key);
|
|
592
592
|
if (component && stringified !== JSON.stringify(component)) {
|
|
593
593
|
dependencies.pure = false;
|
|
594
594
|
return false;
|
|
@@ -598,7 +598,7 @@ const isDependencyPure = (componentDependencies, stringifiedComponents, registry
|
|
|
598
598
|
(dep) => isDependencyPure(
|
|
599
599
|
componentDependencies,
|
|
600
600
|
stringifiedComponents,
|
|
601
|
-
|
|
601
|
+
registry,
|
|
602
602
|
dep,
|
|
603
603
|
new Set(visited)
|
|
604
604
|
)
|
|
@@ -621,9 +621,7 @@ const createSchema = (schema, ctx = {
|
|
|
621
621
|
ctx.registry ?? (ctx.registry = createRegistry());
|
|
622
622
|
ctx.opts ?? (ctx.opts = {});
|
|
623
623
|
ctx.io ?? (ctx.io = "output");
|
|
624
|
-
const registrySchemas = Object.fromEntries(
|
|
625
|
-
ctx.registry.schemas[ctx.io].schemas
|
|
626
|
-
);
|
|
624
|
+
const registrySchemas = Object.fromEntries(ctx.registry.schemas[ctx.io]);
|
|
627
625
|
const schemas = {
|
|
628
626
|
zodOpenApiCreateSchema: { zodType: schema }
|
|
629
627
|
};
|
|
@@ -638,181 +636,121 @@ const createSchema = (schema, ctx = {
|
|
|
638
636
|
components: jsonSchemas.components
|
|
639
637
|
};
|
|
640
638
|
};
|
|
641
|
-
const createSchemas = (schemas, {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
opts
|
|
645
|
-
}) => {
|
|
646
|
-
var _a, _b, _c, _d, _e, _f;
|
|
647
|
-
const schemaRegistry = registry();
|
|
648
|
-
const globalsInSchemas = /* @__PURE__ */ new Map();
|
|
639
|
+
const createSchemas = (schemas, ctx) => {
|
|
640
|
+
var _a, _b;
|
|
641
|
+
const entries = {};
|
|
649
642
|
for (const [name, { zodType }] of Object.entries(schemas)) {
|
|
650
|
-
|
|
651
|
-
if (id) {
|
|
652
|
-
globalsInSchemas.set(name, id);
|
|
653
|
-
}
|
|
654
|
-
schemaRegistry.add(zodType, { id: name });
|
|
643
|
+
entries[name] = zodType;
|
|
655
644
|
}
|
|
645
|
+
const schemaRegistry = object(entries);
|
|
656
646
|
const outputIds = /* @__PURE__ */ new Map();
|
|
657
647
|
const jsonSchema = toJSONSchema(schemaRegistry, {
|
|
658
|
-
override(
|
|
659
|
-
const meta =
|
|
648
|
+
override(context) {
|
|
649
|
+
const meta = context.zodSchema.meta();
|
|
660
650
|
if ((meta == null ? void 0 : meta.outputId) && (meta == null ? void 0 : meta.id)) {
|
|
661
651
|
outputIds.set(meta.id, meta.outputId);
|
|
662
652
|
}
|
|
663
|
-
if (
|
|
653
|
+
if (context.jsonSchema.$ref) {
|
|
664
654
|
return;
|
|
665
655
|
}
|
|
666
|
-
const enrichedContext = { ...
|
|
656
|
+
const enrichedContext = { ...context, io: ctx.io };
|
|
667
657
|
override(enrichedContext);
|
|
668
|
-
if (typeof opts.override === "function") {
|
|
669
|
-
opts.override(enrichedContext);
|
|
658
|
+
if (typeof ctx.opts.override === "function") {
|
|
659
|
+
ctx.opts.override(enrichedContext);
|
|
670
660
|
}
|
|
671
661
|
if (typeof (meta == null ? void 0 : meta.override) === "function") {
|
|
672
662
|
meta.override(enrichedContext);
|
|
673
|
-
delete
|
|
663
|
+
delete context.jsonSchema.override;
|
|
674
664
|
}
|
|
675
665
|
if (typeof (meta == null ? void 0 : meta.override) === "object" && meta.override !== null) {
|
|
676
|
-
Object.assign(
|
|
677
|
-
delete
|
|
666
|
+
Object.assign(context.jsonSchema, meta.override);
|
|
667
|
+
delete context.jsonSchema.override;
|
|
678
668
|
}
|
|
679
|
-
delete
|
|
680
|
-
delete
|
|
681
|
-
deleteZodOpenApiMeta(
|
|
682
|
-
validate(enrichedContext, opts);
|
|
669
|
+
delete context.jsonSchema.$schema;
|
|
670
|
+
delete context.jsonSchema.id;
|
|
671
|
+
deleteZodOpenApiMeta(context.jsonSchema);
|
|
672
|
+
validate(enrichedContext, ctx.opts);
|
|
683
673
|
},
|
|
684
|
-
io,
|
|
685
|
-
unrepresentable: "any"
|
|
686
|
-
uri: (id) => `#/components/schemas/${id}`
|
|
674
|
+
io: ctx.io,
|
|
675
|
+
unrepresentable: "any"
|
|
687
676
|
});
|
|
688
|
-
const
|
|
689
|
-
|
|
690
|
-
const
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
continue;
|
|
698
|
-
}
|
|
699
|
-
componentsToReplace.set(`__shared#/$defs/${key}`, key);
|
|
700
|
-
}
|
|
701
|
-
for (const value of Object.values(jsonSchema.schemas)) {
|
|
702
|
-
delete value.$schema;
|
|
703
|
-
delete value.id;
|
|
704
|
-
}
|
|
705
|
-
const dynamicComponent = /* @__PURE__ */ new Map();
|
|
706
|
-
const patched = JSON.stringify(jsonSchema).replace(
|
|
707
|
-
/"#\/components\/schemas\/([^"]+)"/g,
|
|
708
|
-
(_, match) => {
|
|
709
|
-
const replacement = componentsToReplace.get(match);
|
|
710
|
-
if (replacement) {
|
|
711
|
-
return `"#/components/schemas/${replacement}"`;
|
|
712
|
-
}
|
|
713
|
-
const component = registry$1.schemas.ids.get(match);
|
|
714
|
-
if (component) {
|
|
715
|
-
return `"#/components/schemas/${match}`;
|
|
716
|
-
}
|
|
717
|
-
const globalInSchema = globalsInSchemas.get(match);
|
|
718
|
-
if (globalInSchema) {
|
|
719
|
-
componentsToReplace.set(match, globalInSchema);
|
|
720
|
-
dynamicComponent.set(match, globalInSchema);
|
|
721
|
-
return `"#/components/schemas/${globalInSchema}"`;
|
|
722
|
-
}
|
|
723
|
-
const manualSchema = registry$1.schemas.manual.get(match);
|
|
724
|
-
if (manualSchema) {
|
|
725
|
-
componentsToReplace.set(match, manualSchema.key);
|
|
726
|
-
dynamicComponent.set(match, manualSchema.key);
|
|
727
|
-
manualSchema.io[io].used = true;
|
|
728
|
-
return `"#/components/schemas/${manualSchema.key}"`;
|
|
677
|
+
const components = jsonSchema.$defs ?? {};
|
|
678
|
+
const dynamicComponents = /* @__PURE__ */ new Map();
|
|
679
|
+
for (const [key, value] of Object.entries(components)) {
|
|
680
|
+
if (/^__schema\d+$/.test(key)) {
|
|
681
|
+
const newName = `__schema${ctx.registry.schemas.dynamicSchemaCount++}`;
|
|
682
|
+
dynamicComponents.set(key, `"#/components/schemas/${newName}"`);
|
|
683
|
+
if (newName !== key) {
|
|
684
|
+
components[newName] = value;
|
|
685
|
+
delete components[key];
|
|
729
686
|
}
|
|
730
|
-
const componentName = `__schema${registry$1.schemas.dynamicSchemaCount++}`;
|
|
731
|
-
componentsToReplace.set(match, componentName);
|
|
732
|
-
dynamicComponent.set(match, componentName);
|
|
733
|
-
return `"#/components/schemas/${componentName}"`;
|
|
734
687
|
}
|
|
688
|
+
}
|
|
689
|
+
const parsedJsonSchema = JSON.parse(
|
|
690
|
+
JSON.stringify(jsonSchema).replace(
|
|
691
|
+
/"#\/\$defs\/([^"]+)"/g,
|
|
692
|
+
(_, match) => {
|
|
693
|
+
const dynamic = dynamicComponents.get(match);
|
|
694
|
+
if (dynamic) {
|
|
695
|
+
return dynamic;
|
|
696
|
+
}
|
|
697
|
+
const manualComponent = ctx.registry.schemas.manual.get(match);
|
|
698
|
+
if (manualComponent) {
|
|
699
|
+
manualComponent.io[ctx.io].used++;
|
|
700
|
+
}
|
|
701
|
+
return `"#/components/schemas/${match}"`;
|
|
702
|
+
}
|
|
703
|
+
)
|
|
735
704
|
);
|
|
736
|
-
const
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
dynamicComponent.set(key, value.key);
|
|
705
|
+
for (const [key, value] of ctx.registry.schemas.manual) {
|
|
706
|
+
if (value.io[ctx.io].used === 1 && ((_a = parsedJsonSchema.properties) == null ? void 0 : _a[value.identifier]) && ((_b = parsedJsonSchema.$defs) == null ? void 0 : _b[key])) {
|
|
707
|
+
parsedJsonSchema.properties[value.identifier] = parsedJsonSchema.$defs[key];
|
|
708
|
+
delete parsedJsonSchema.$defs[key];
|
|
709
|
+
continue;
|
|
742
710
|
}
|
|
743
711
|
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
$ref: `#/components/schemas/${value}`
|
|
751
|
-
};
|
|
752
|
-
components[value] = component;
|
|
753
|
-
}
|
|
754
|
-
const renamedComponents = renameComponents(components, outputIds, {
|
|
755
|
-
registry: registry$1
|
|
756
|
-
});
|
|
757
|
-
if (renamedComponents.size === 0) {
|
|
758
|
-
delete patchedJsonSchema.schemas.__shared;
|
|
712
|
+
const renamedComponents = renameComponents(
|
|
713
|
+
parsedJsonSchema.$defs ?? {},
|
|
714
|
+
outputIds,
|
|
715
|
+
ctx
|
|
716
|
+
);
|
|
717
|
+
if (!renamedComponents.size) {
|
|
759
718
|
return {
|
|
760
|
-
schemas:
|
|
761
|
-
components
|
|
719
|
+
schemas: parsedJsonSchema.properties,
|
|
720
|
+
components: parsedJsonSchema.$defs ?? {}
|
|
762
721
|
};
|
|
763
722
|
}
|
|
764
|
-
const
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
723
|
+
const renamedJsonSchema = JSON.parse(
|
|
724
|
+
JSON.stringify(parsedJsonSchema).replace(
|
|
725
|
+
/"#\/components\/schemas\/([^"]+)"/g,
|
|
726
|
+
(_, match) => {
|
|
727
|
+
const replacement = renamedComponents.get(match);
|
|
728
|
+
if (replacement) {
|
|
729
|
+
return `"#/components/schemas/${replacement}"`;
|
|
730
|
+
}
|
|
731
|
+
return `"#/components/schemas/${match}"`;
|
|
770
732
|
}
|
|
771
|
-
|
|
772
|
-
}
|
|
733
|
+
)
|
|
773
734
|
);
|
|
774
|
-
const renamedJsonSchema = JSON.parse(renamedStringified);
|
|
775
|
-
const renamedJsonSchemaComponents = ((_f = renamedJsonSchema.schemas.__shared) == null ? void 0 : _f.$defs) ?? {};
|
|
776
|
-
delete renamedJsonSchema.schemas.__shared;
|
|
777
735
|
return {
|
|
778
|
-
schemas: renamedJsonSchema.
|
|
779
|
-
components:
|
|
736
|
+
schemas: renamedJsonSchema.properties,
|
|
737
|
+
components: renamedJsonSchema.$defs ?? {}
|
|
780
738
|
};
|
|
781
739
|
};
|
|
782
740
|
const createRegistry = (components) => {
|
|
783
|
-
const
|
|
741
|
+
const registry = {
|
|
784
742
|
schemas: {
|
|
785
743
|
dynamicSchemaCount: 0,
|
|
786
|
-
input:
|
|
787
|
-
|
|
788
|
-
schemas: /* @__PURE__ */ new Map()
|
|
789
|
-
},
|
|
790
|
-
output: {
|
|
791
|
-
seen: /* @__PURE__ */ new WeakMap(),
|
|
792
|
-
schemas: /* @__PURE__ */ new Map()
|
|
793
|
-
},
|
|
744
|
+
input: /* @__PURE__ */ new Map(),
|
|
745
|
+
output: /* @__PURE__ */ new Map(),
|
|
794
746
|
ids: /* @__PURE__ */ new Map(),
|
|
795
747
|
manual: /* @__PURE__ */ new Map(),
|
|
796
748
|
setSchema: (key, schema, io) => {
|
|
797
|
-
const seenSchema = registry2.schemas[io].seen.get(schema);
|
|
798
|
-
if (seenSchema) {
|
|
799
|
-
if (seenSchema.type === "manual") {
|
|
800
|
-
const manualSchema = registry2.schemas.manual.get(seenSchema.id);
|
|
801
|
-
if (!manualSchema) {
|
|
802
|
-
throw new Error(
|
|
803
|
-
`Manual schema "${key}" not found in registry for ${io} IO.`
|
|
804
|
-
);
|
|
805
|
-
}
|
|
806
|
-
manualSchema.io[io].used = true;
|
|
807
|
-
}
|
|
808
|
-
return seenSchema.schemaObject;
|
|
809
|
-
}
|
|
810
749
|
const schemaObject = {};
|
|
811
|
-
|
|
750
|
+
registry.schemas[io].set(key, {
|
|
812
751
|
schemaObject,
|
|
813
752
|
zodType: schema
|
|
814
753
|
});
|
|
815
|
-
registry2.schemas[io].seen.set(schema, { type: "schema", schemaObject });
|
|
816
754
|
return schemaObject;
|
|
817
755
|
}
|
|
818
756
|
},
|
|
@@ -841,68 +779,68 @@ const createRegistry = (components) => {
|
|
|
841
779
|
seen: /* @__PURE__ */ new WeakMap()
|
|
842
780
|
}
|
|
843
781
|
};
|
|
844
|
-
registerSchemas(components == null ? void 0 : components.schemas,
|
|
845
|
-
registerParameters(components == null ? void 0 : components.parameters,
|
|
846
|
-
registerHeaders(components == null ? void 0 : components.headers,
|
|
847
|
-
registerResponses(components == null ? void 0 : components.responses,
|
|
848
|
-
registerPathItems(components == null ? void 0 : components.pathItems,
|
|
849
|
-
registerRequestBodies(components == null ? void 0 : components.requestBodies,
|
|
850
|
-
registerCallbacks(components == null ? void 0 : components.callbacks,
|
|
851
|
-
return
|
|
782
|
+
registerSchemas(components == null ? void 0 : components.schemas, registry);
|
|
783
|
+
registerParameters(components == null ? void 0 : components.parameters, registry);
|
|
784
|
+
registerHeaders(components == null ? void 0 : components.headers, registry);
|
|
785
|
+
registerResponses(components == null ? void 0 : components.responses, registry);
|
|
786
|
+
registerPathItems(components == null ? void 0 : components.pathItems, registry);
|
|
787
|
+
registerRequestBodies(components == null ? void 0 : components.requestBodies, registry);
|
|
788
|
+
registerCallbacks(components == null ? void 0 : components.callbacks, registry);
|
|
789
|
+
return registry;
|
|
852
790
|
};
|
|
853
|
-
const registerSchemas = (schemas,
|
|
791
|
+
const registerSchemas = (schemas, registry) => {
|
|
854
792
|
if (!schemas) {
|
|
855
793
|
return;
|
|
856
794
|
}
|
|
857
795
|
for (const [key, schema] of Object.entries(schemas)) {
|
|
858
|
-
if (
|
|
796
|
+
if (registry.schemas.ids.has(key)) {
|
|
859
797
|
throw new Error(`Schema "${key}" is already registered`);
|
|
860
798
|
}
|
|
861
799
|
if (isAnyZodType(schema)) {
|
|
862
800
|
const inputSchemaObject = {};
|
|
863
801
|
const outputSchemaObject = {};
|
|
864
802
|
const identifier = `components > schemas > ${key}`;
|
|
865
|
-
|
|
803
|
+
registry.schemas.input.set(identifier, {
|
|
866
804
|
zodType: schema,
|
|
867
805
|
schemaObject: inputSchemaObject
|
|
868
806
|
});
|
|
869
|
-
|
|
870
|
-
type: "manual",
|
|
871
|
-
schemaObject: inputSchemaObject,
|
|
872
|
-
id: identifier
|
|
873
|
-
});
|
|
874
|
-
registry2.schemas.output.schemas.set(identifier, {
|
|
807
|
+
registry.schemas.output.set(identifier, {
|
|
875
808
|
zodType: schema,
|
|
876
809
|
schemaObject: outputSchemaObject
|
|
877
810
|
});
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
schemaObject: outputSchemaObject,
|
|
881
|
-
id: identifier
|
|
882
|
-
});
|
|
883
|
-
registry2.schemas.manual.set(identifier, {
|
|
884
|
-
key,
|
|
811
|
+
registry.schemas.manual.set(key, {
|
|
812
|
+
identifier,
|
|
885
813
|
io: {
|
|
886
814
|
input: {
|
|
887
|
-
schemaObject: inputSchemaObject
|
|
815
|
+
schemaObject: inputSchemaObject,
|
|
816
|
+
used: 0
|
|
888
817
|
},
|
|
889
818
|
output: {
|
|
890
|
-
schemaObject: outputSchemaObject
|
|
819
|
+
schemaObject: outputSchemaObject,
|
|
820
|
+
used: 0
|
|
891
821
|
}
|
|
892
822
|
},
|
|
893
823
|
zodType: schema
|
|
894
824
|
});
|
|
825
|
+
const meta = globalRegistry$1.get(schema);
|
|
826
|
+
if (meta == null ? void 0 : meta.id) {
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
globalRegistry$1.add(schema, {
|
|
830
|
+
...meta,
|
|
831
|
+
id: key
|
|
832
|
+
});
|
|
895
833
|
continue;
|
|
896
834
|
}
|
|
897
|
-
|
|
835
|
+
registry.schemas.ids.set(key, schema);
|
|
898
836
|
}
|
|
899
837
|
};
|
|
900
|
-
const registerParameters = (parameters,
|
|
838
|
+
const registerParameters = (parameters, registry) => {
|
|
901
839
|
if (!parameters) {
|
|
902
840
|
return;
|
|
903
841
|
}
|
|
904
842
|
for (const [key, schema] of Object.entries(parameters)) {
|
|
905
|
-
if (
|
|
843
|
+
if (registry.parameters.ids.has(key)) {
|
|
906
844
|
throw new Error(`Parameter "${key}" is already registered`);
|
|
907
845
|
}
|
|
908
846
|
if (isAnyZodType(schema)) {
|
|
@@ -911,24 +849,24 @@ const registerParameters = (parameters, registry2) => {
|
|
|
911
849
|
schema,
|
|
912
850
|
void 0,
|
|
913
851
|
{
|
|
914
|
-
registry
|
|
852
|
+
registry,
|
|
915
853
|
io: "input"
|
|
916
854
|
},
|
|
917
855
|
path
|
|
918
856
|
);
|
|
919
|
-
|
|
920
|
-
|
|
857
|
+
registry.parameters.ids.set(key, paramObject);
|
|
858
|
+
registry.parameters.seen.set(schema, paramObject);
|
|
921
859
|
continue;
|
|
922
860
|
}
|
|
923
|
-
|
|
861
|
+
registry.parameters.ids.set(key, schema);
|
|
924
862
|
}
|
|
925
863
|
};
|
|
926
|
-
const registerHeaders = (headers,
|
|
864
|
+
const registerHeaders = (headers, registry) => {
|
|
927
865
|
if (!headers) {
|
|
928
866
|
return;
|
|
929
867
|
}
|
|
930
868
|
for (const [key, schema] of Object.entries(headers)) {
|
|
931
|
-
if (
|
|
869
|
+
if (registry.headers.ids.has(key)) {
|
|
932
870
|
throw new Error(`Header "${key}" is already registered`);
|
|
933
871
|
}
|
|
934
872
|
if (isAnyZodType(schema)) {
|
|
@@ -936,45 +874,45 @@ const registerHeaders = (headers, registry2) => {
|
|
|
936
874
|
const headerObject = createHeader(
|
|
937
875
|
schema,
|
|
938
876
|
{
|
|
939
|
-
registry
|
|
877
|
+
registry,
|
|
940
878
|
io: "output"
|
|
941
879
|
},
|
|
942
880
|
path
|
|
943
881
|
);
|
|
944
|
-
|
|
945
|
-
|
|
882
|
+
registry.headers.ids.set(key, headerObject);
|
|
883
|
+
registry.headers.seen.set(schema, headerObject);
|
|
946
884
|
continue;
|
|
947
885
|
}
|
|
948
|
-
|
|
886
|
+
registry.headers.ids.set(key, schema);
|
|
949
887
|
}
|
|
950
888
|
};
|
|
951
|
-
const registerResponses = (responses,
|
|
889
|
+
const registerResponses = (responses, registry) => {
|
|
952
890
|
if (!responses) {
|
|
953
891
|
return;
|
|
954
892
|
}
|
|
955
893
|
for (const [key, schema] of Object.entries(responses)) {
|
|
956
894
|
const path = ["components", "responses", key];
|
|
957
|
-
if (
|
|
895
|
+
if (registry.responses.ids.has(key)) {
|
|
958
896
|
throw new Error(`Response "${key}" is already registered`);
|
|
959
897
|
}
|
|
960
898
|
const responseObject = createResponse(
|
|
961
899
|
schema,
|
|
962
900
|
{
|
|
963
|
-
registry
|
|
901
|
+
registry,
|
|
964
902
|
io: "output"
|
|
965
903
|
},
|
|
966
904
|
path
|
|
967
905
|
);
|
|
968
|
-
|
|
969
|
-
|
|
906
|
+
registry.responses.ids.set(key, responseObject);
|
|
907
|
+
registry.responses.seen.set(schema, responseObject);
|
|
970
908
|
}
|
|
971
909
|
};
|
|
972
|
-
const registerRequestBodies = (requestBodies,
|
|
910
|
+
const registerRequestBodies = (requestBodies, registry) => {
|
|
973
911
|
if (!requestBodies) {
|
|
974
912
|
return;
|
|
975
913
|
}
|
|
976
914
|
for (const [key, schema] of Object.entries(requestBodies)) {
|
|
977
|
-
if (
|
|
915
|
+
if (registry.requestBodies.ids.has(key)) {
|
|
978
916
|
throw new Error(`RequestBody "${key}" is already registered`);
|
|
979
917
|
}
|
|
980
918
|
if (isAnyZodType(schema)) {
|
|
@@ -982,96 +920,96 @@ const registerRequestBodies = (requestBodies, registry2) => {
|
|
|
982
920
|
const requestBodyObject = createRequestBody(
|
|
983
921
|
schema,
|
|
984
922
|
{
|
|
985
|
-
registry
|
|
923
|
+
registry,
|
|
986
924
|
io: "input"
|
|
987
925
|
},
|
|
988
926
|
path
|
|
989
927
|
);
|
|
990
|
-
|
|
928
|
+
registry.requestBodies.ids.set(key, requestBodyObject);
|
|
991
929
|
continue;
|
|
992
930
|
}
|
|
993
|
-
|
|
931
|
+
registry.requestBodies.ids.set(key, schema);
|
|
994
932
|
}
|
|
995
933
|
};
|
|
996
|
-
const registerCallbacks = (callbacks,
|
|
934
|
+
const registerCallbacks = (callbacks, registry) => {
|
|
997
935
|
if (!callbacks) {
|
|
998
936
|
return;
|
|
999
937
|
}
|
|
1000
938
|
for (const [key, schema] of Object.entries(callbacks)) {
|
|
1001
|
-
if (
|
|
939
|
+
if (registry.callbacks.ids.has(key)) {
|
|
1002
940
|
throw new Error(`Callback "${key}" is already registered`);
|
|
1003
941
|
}
|
|
1004
942
|
const path = ["components", "callbacks", key];
|
|
1005
|
-
const callbackObject = createCallback(schema,
|
|
1006
|
-
|
|
1007
|
-
|
|
943
|
+
const callbackObject = createCallback(schema, registry, path);
|
|
944
|
+
registry.callbacks.ids.set(key, callbackObject);
|
|
945
|
+
registry.callbacks.seen.set(schema, callbackObject);
|
|
1008
946
|
}
|
|
1009
947
|
};
|
|
1010
|
-
const registerPathItems = (pathItems,
|
|
948
|
+
const registerPathItems = (pathItems, registry) => {
|
|
1011
949
|
if (!pathItems) {
|
|
1012
950
|
return;
|
|
1013
951
|
}
|
|
1014
952
|
for (const [key, schema] of Object.entries(pathItems)) {
|
|
1015
|
-
if (
|
|
953
|
+
if (registry.pathItems.ids.has(key)) {
|
|
1016
954
|
throw new Error(`PathItem "${key}" is already registered`);
|
|
1017
955
|
}
|
|
1018
956
|
const path = ["components", "pathItems", key];
|
|
1019
|
-
const pathItemObject = createPathItem(schema,
|
|
1020
|
-
|
|
1021
|
-
|
|
957
|
+
const pathItemObject = createPathItem(schema, registry, path);
|
|
958
|
+
registry.pathItems.ids.set(key, pathItemObject);
|
|
959
|
+
registry.pathItems.seen.set(schema, pathItemObject);
|
|
1022
960
|
continue;
|
|
1023
961
|
}
|
|
1024
962
|
};
|
|
1025
963
|
const createIOSchemas = (ctx) => {
|
|
1026
964
|
const { schemas, components } = createSchemas(
|
|
1027
|
-
Object.fromEntries(ctx.registry.schemas[ctx.io]
|
|
965
|
+
Object.fromEntries(ctx.registry.schemas[ctx.io]),
|
|
1028
966
|
ctx
|
|
1029
967
|
);
|
|
1030
968
|
for (const [key, schema] of Object.entries(components)) {
|
|
1031
969
|
ctx.registry.schemas.ids.set(key, schema);
|
|
1032
970
|
}
|
|
1033
971
|
for (const [key, schema] of Object.entries(schemas)) {
|
|
1034
|
-
const ioSchema = ctx.registry.schemas[ctx.io].
|
|
972
|
+
const ioSchema = ctx.registry.schemas[ctx.io].get(key);
|
|
1035
973
|
if (ioSchema) {
|
|
1036
974
|
Object.assign(ioSchema.schemaObject, schema);
|
|
1037
975
|
}
|
|
1038
976
|
}
|
|
1039
977
|
};
|
|
1040
|
-
const createManualSchemas = (
|
|
978
|
+
const createManualSchemas = (registry) => {
|
|
1041
979
|
var _a;
|
|
1042
|
-
for (const [, value] of
|
|
1043
|
-
if (
|
|
980
|
+
for (const [key, value] of registry.schemas.manual) {
|
|
981
|
+
if (value.io.input.used === 1) {
|
|
1044
982
|
const io = ((_a = globalRegistry$1.get(value.zodType)) == null ? void 0 : _a.unusedIO) ?? "output";
|
|
1045
983
|
const schema = value.io[io].schemaObject;
|
|
1046
|
-
|
|
984
|
+
registry.schemas.ids.set(key, schema);
|
|
1047
985
|
}
|
|
1048
986
|
}
|
|
1049
987
|
};
|
|
1050
|
-
const createComponents = (
|
|
1051
|
-
createIOSchemas({ registry
|
|
1052
|
-
createIOSchemas({ registry
|
|
1053
|
-
createManualSchemas(
|
|
988
|
+
const createComponents = (registry, opts) => {
|
|
989
|
+
createIOSchemas({ registry, io: "input", opts });
|
|
990
|
+
createIOSchemas({ registry, io: "output", opts });
|
|
991
|
+
createManualSchemas(registry);
|
|
1054
992
|
const components = {};
|
|
1055
|
-
if (
|
|
1056
|
-
components.schemas = Object.fromEntries(
|
|
993
|
+
if (registry.schemas.ids.size > 0) {
|
|
994
|
+
components.schemas = Object.fromEntries(registry.schemas.ids);
|
|
1057
995
|
}
|
|
1058
|
-
if (
|
|
1059
|
-
components.headers = Object.fromEntries(
|
|
996
|
+
if (registry.headers.ids.size > 0) {
|
|
997
|
+
components.headers = Object.fromEntries(registry.headers.ids);
|
|
1060
998
|
}
|
|
1061
|
-
if (
|
|
1062
|
-
components.requestBodies = Object.fromEntries(
|
|
999
|
+
if (registry.requestBodies.ids.size > 0) {
|
|
1000
|
+
components.requestBodies = Object.fromEntries(registry.requestBodies.ids);
|
|
1063
1001
|
}
|
|
1064
|
-
if (
|
|
1065
|
-
components.responses = Object.fromEntries(
|
|
1002
|
+
if (registry.responses.ids.size > 0) {
|
|
1003
|
+
components.responses = Object.fromEntries(registry.responses.ids);
|
|
1066
1004
|
}
|
|
1067
|
-
if (
|
|
1068
|
-
components.parameters = Object.fromEntries(
|
|
1005
|
+
if (registry.parameters.ids.size > 0) {
|
|
1006
|
+
components.parameters = Object.fromEntries(registry.parameters.ids);
|
|
1069
1007
|
}
|
|
1070
|
-
if (
|
|
1071
|
-
components.callbacks = Object.fromEntries(
|
|
1008
|
+
if (registry.callbacks.ids.size > 0) {
|
|
1009
|
+
components.callbacks = Object.fromEntries(registry.callbacks.ids);
|
|
1072
1010
|
}
|
|
1073
|
-
if (
|
|
1074
|
-
components.pathItems = Object.fromEntries(
|
|
1011
|
+
if (registry.pathItems.ids.size > 0) {
|
|
1012
|
+
components.pathItems = Object.fromEntries(registry.pathItems.ids);
|
|
1075
1013
|
}
|
|
1076
1014
|
return components;
|
|
1077
1015
|
};
|
|
@@ -8,44 +8,24 @@ interface ComponentRegistry {
|
|
|
8
8
|
*/
|
|
9
9
|
schemas: {
|
|
10
10
|
dynamicSchemaCount: number;
|
|
11
|
-
input: {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}>;
|
|
20
|
-
schemas: Map<string, {
|
|
21
|
-
zodType: $ZodType;
|
|
22
|
-
schemaObject: SchemaObject | ReferenceObject;
|
|
23
|
-
}>;
|
|
24
|
-
};
|
|
25
|
-
output: {
|
|
26
|
-
seen: WeakMap<$ZodType, {
|
|
27
|
-
type: 'manual';
|
|
28
|
-
id: string;
|
|
29
|
-
schemaObject: SchemaObject | ReferenceObject;
|
|
30
|
-
} | {
|
|
31
|
-
type: 'schema';
|
|
32
|
-
schemaObject: SchemaObject | ReferenceObject;
|
|
33
|
-
}>;
|
|
34
|
-
schemas: Map<string, {
|
|
35
|
-
zodType: $ZodType;
|
|
36
|
-
schemaObject: SchemaObject | ReferenceObject;
|
|
37
|
-
}>;
|
|
38
|
-
};
|
|
11
|
+
input: Map<string, {
|
|
12
|
+
zodType: $ZodType;
|
|
13
|
+
schemaObject: SchemaObject | ReferenceObject;
|
|
14
|
+
}>;
|
|
15
|
+
output: Map<string, {
|
|
16
|
+
zodType: $ZodType;
|
|
17
|
+
schemaObject: SchemaObject | ReferenceObject;
|
|
18
|
+
}>;
|
|
39
19
|
ids: Map<string, SchemaObject | ReferenceObject>;
|
|
40
20
|
manual: Map<string, {
|
|
41
|
-
|
|
21
|
+
identifier: string;
|
|
42
22
|
io: {
|
|
43
23
|
input: {
|
|
44
|
-
used
|
|
24
|
+
used: number;
|
|
45
25
|
schemaObject: SchemaObject;
|
|
46
26
|
};
|
|
47
27
|
output: {
|
|
48
|
-
used
|
|
28
|
+
used: number;
|
|
49
29
|
schemaObject: SchemaObject;
|
|
50
30
|
};
|
|
51
31
|
};
|
|
@@ -2,7 +2,7 @@ import { core } from 'zod/v4';
|
|
|
2
2
|
import { CreateDocumentOptions } from '../document.js';
|
|
3
3
|
import { ComponentRegistry } from '../components.js';
|
|
4
4
|
import '../../zod.js';
|
|
5
|
-
import {
|
|
5
|
+
import { SchemaObject, ReferenceObject } from '../../openapi3-ts/dist/model/openapi31.js';
|
|
6
6
|
|
|
7
7
|
interface SchemaResult {
|
|
8
8
|
schema: SchemaObject | ReferenceObject;
|
|
@@ -13,7 +13,7 @@ declare const createSchema: (schema: core.$ZodType, ctx?: {
|
|
|
13
13
|
io?: "input" | "output";
|
|
14
14
|
opts?: CreateDocumentOptions;
|
|
15
15
|
}) => {
|
|
16
|
-
schema:
|
|
16
|
+
schema: SchemaObject | ReferenceObject;
|
|
17
17
|
components: Record<string, SchemaObject>;
|
|
18
18
|
};
|
|
19
19
|
|