-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.ts
More file actions
130 lines (115 loc) · 3.23 KB
/
Copy pathconverter.ts
File metadata and controls
130 lines (115 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { JSONSchema7Definition } from "@ai-sdk/provider";
/**
* Converts JSON Schema 7 to OpenAPI Schema 3.0
*/
export function convertJSONSchemaToOpenAPISchema(
jsonSchema: JSONSchema7Definition | undefined
): unknown {
// parameters need to be undefined if they are empty objects:
if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
return undefined;
}
if (typeof jsonSchema === "boolean") {
return { type: "boolean", properties: {} };
}
const {
type,
description,
required,
properties,
items,
allOf,
anyOf,
oneOf,
format,
const: constValue,
minLength,
enum: enumValues,
} = jsonSchema;
const result: Record<string, unknown> = {};
if (description) result.description = description;
if (required) result.required = required;
if (format) result.format = format;
if (constValue !== undefined) {
result.enum = [constValue];
}
// Handle type
if (type) {
if (Array.isArray(type)) {
if (type.includes("null")) {
result.type = type.filter((t) => t !== "null")[0];
result.nullable = true;
} else {
result.type = type;
}
} else if (type === "null") {
result.type = "null";
} else {
result.type = type;
}
}
// Handle enum
if (enumValues !== undefined) {
result.enum = enumValues;
}
if (properties != null) {
result.properties = Object.entries(properties).reduce(
(acc, [key, value]) => {
acc[key] = convertJSONSchemaToOpenAPISchema(value);
return acc;
},
{} as Record<string, unknown>
);
}
if (items) {
result.items = Array.isArray(items)
? items.map(convertJSONSchemaToOpenAPISchema)
: convertJSONSchemaToOpenAPISchema(items);
}
if (allOf) {
result.allOf = allOf.map(convertJSONSchemaToOpenAPISchema);
}
if (anyOf) {
// Handle cases where anyOf includes a null type
if (
anyOf.some(
(schema) => typeof schema === "object" && schema?.type === "null"
)
) {
const nonNullSchemas = anyOf.filter(
(schema) => !(typeof schema === "object" && schema?.type === "null")
);
if (nonNullSchemas.length === 1) {
// If there's only one non-null schema, convert it and make it nullable
const converted = convertJSONSchemaToOpenAPISchema(nonNullSchemas[0]);
if (typeof converted === "object") {
result.nullable = true;
Object.assign(result, converted);
}
} else {
// If there are multiple non-null schemas, keep them in anyOf
result.anyOf = nonNullSchemas.map(convertJSONSchemaToOpenAPISchema);
result.nullable = true;
}
} else {
result.anyOf = anyOf.map(convertJSONSchemaToOpenAPISchema);
}
}
if (oneOf) {
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
}
if (minLength !== undefined) {
result.minLength = minLength;
}
return result;
}
function isEmptyObjectSchema(jsonSchema: JSONSchema7Definition): boolean {
return (
jsonSchema != null &&
typeof jsonSchema === "object" &&
jsonSchema.type === "object" &&
(jsonSchema.properties == null ||
Object.keys(jsonSchema.properties).length === 0) &&
!jsonSchema.additionalProperties
);
}