@effect/openapi-generator 4.0.0-beta.4 → 4.0.0-beta.41
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/HttpApiTransformer.d.ts +6 -0
- package/dist/HttpApiTransformer.d.ts.map +1 -0
- package/dist/HttpApiTransformer.js +354 -0
- package/dist/HttpApiTransformer.js.map +1 -0
- package/dist/JsonSchemaGenerator.d.ts +13 -3
- package/dist/JsonSchemaGenerator.d.ts.map +1 -1
- package/dist/JsonSchemaGenerator.js +146 -47
- package/dist/JsonSchemaGenerator.js.map +1 -1
- package/dist/OpenApiGenerator.d.ts +17 -5
- package/dist/OpenApiGenerator.d.ts.map +1 -1
- package/dist/OpenApiGenerator.js +650 -117
- package/dist/OpenApiGenerator.js.map +1 -1
- package/dist/OpenApiPatch.d.ts +2 -2
- package/dist/OpenApiTransformer.d.ts +10 -10
- package/dist/OpenApiTransformer.d.ts.map +1 -1
- package/dist/OpenApiTransformer.js +14 -22
- package/dist/OpenApiTransformer.js.map +1 -1
- package/dist/ParsedOperation.d.ts +80 -1
- package/dist/ParsedOperation.d.ts.map +1 -1
- package/dist/ParsedOperation.js +25 -0
- package/dist/ParsedOperation.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +19 -8
- package/dist/main.js.map +1 -1
- package/package.json +5 -5
- package/src/HttpApiTransformer.ts +497 -0
- package/src/JsonSchemaGenerator.ts +240 -47
- package/src/OpenApiGenerator.ts +907 -171
- package/src/OpenApiTransformer.ts +18 -28
- package/src/ParsedOperation.ts +127 -1
- package/src/main.ts +36 -10
package/dist/OpenApiGenerator.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as Predicate from "effect/Predicate";
|
|
|
4
4
|
import * as ServiceMap from "effect/ServiceMap";
|
|
5
5
|
import * as String from "effect/String";
|
|
6
6
|
import SwaggerToOpenApi from "swagger2openapi";
|
|
7
|
+
import * as HttpApiTransformer from "./HttpApiTransformer.js";
|
|
7
8
|
import * as JsonSchemaGenerator from "./JsonSchemaGenerator.js";
|
|
8
9
|
import * as OpenApiTransformer from "./OpenApiTransformer.js";
|
|
9
10
|
import * as ParsedOperation from "./ParsedOperation.js";
|
|
@@ -14,6 +15,7 @@ export const make = /*#__PURE__*/Effect.gen(function* () {
|
|
|
14
15
|
const generate = Effect.fn(function* (spec, options) {
|
|
15
16
|
const generator = JsonSchemaGenerator.make();
|
|
16
17
|
const openApiTransformer = yield* OpenApiTransformer.OpenApiTransformer;
|
|
18
|
+
const emitWarning = makeWarningEmitter(options);
|
|
17
19
|
// If we receive a Swagger 2.0 spec, convert it to an OpenApi 3.0 spec
|
|
18
20
|
if (isSwaggerSpec(spec)) {
|
|
19
21
|
spec = yield* convertSwaggerSpec(spec);
|
|
@@ -26,150 +28,681 @@ export const make = /*#__PURE__*/Effect.gen(function* () {
|
|
|
26
28
|
}
|
|
27
29
|
return current;
|
|
28
30
|
}
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
31
|
+
const multipartSchemaRefs = options.format === "httpapi" ? makeHttpApiMultipartSchemaRefs(spec.components?.schemas ?? {}) : undefined;
|
|
32
|
+
const parsed = parseOpenApi(spec, generator, resolveRef, options.format, emitWarning, multipartSchemaRefs);
|
|
33
|
+
// TODO: make a CLI option ?
|
|
34
|
+
const importName = "Schema";
|
|
35
|
+
const source = getDialect(spec);
|
|
36
|
+
const generation = options.format === "httpapi" ? generator.generateHttpApi(source, withHttpApiMultipartSchemas(spec.components?.schemas ?? {}, multipartSchemaRefs), {
|
|
37
|
+
onEnter: options.onEnter,
|
|
38
|
+
multipartSchemaRefs
|
|
39
|
+
}) : generator.generate(source, spec.components?.schemas ?? {}, options.format === "httpclient-type-only", {
|
|
40
|
+
onEnter: options.onEnter
|
|
41
|
+
});
|
|
42
|
+
if (options.format === "httpapi") {
|
|
43
|
+
const needsMultipartImport = generation.includes("Multipart.");
|
|
44
|
+
return String.stripMargin(`|${HttpApiTransformer.imports(importName, {
|
|
45
|
+
multipart: needsMultipartImport
|
|
46
|
+
})}
|
|
47
|
+
|${generation}
|
|
48
|
+
|${HttpApiTransformer.toImplementation(importName, options.name, parsed)}`);
|
|
49
|
+
}
|
|
50
|
+
return String.stripMargin(`|${openApiTransformer.imports(importName, parsed)}
|
|
51
|
+
|${generation}
|
|
52
|
+
|${openApiTransformer.toImplementation(importName, options.name, parsed)}
|
|
53
|
+
|
|
|
54
|
+
|${openApiTransformer.toTypes(importName, options.name, parsed)}`);
|
|
55
|
+
}, (effect, _, options) => Effect.provideServiceEffect(effect, OpenApiTransformer.OpenApiTransformer, options.format === "httpclient-type-only" ? Effect.sync(OpenApiTransformer.makeTransformerTs) : Effect.sync(OpenApiTransformer.makeTransformerSchema)));
|
|
56
|
+
return {
|
|
57
|
+
generate
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
const makeWarningEmitter = options => warning => {
|
|
61
|
+
options.onWarning?.(warning);
|
|
62
|
+
};
|
|
63
|
+
const parseOpenApi = (spec, generator, resolveRef, format, emitWarning, multipartSchemaRefs) => {
|
|
64
|
+
const operations = [];
|
|
65
|
+
const reservedSchemaNames = new Set(Object.keys(spec.components?.schemas ?? {}));
|
|
66
|
+
const isHttpApi = format === "httpapi";
|
|
67
|
+
const securitySchemes = isHttpApi ? parseSecuritySchemes(spec, resolveRef) : [];
|
|
68
|
+
const addSchema = (baseName, schema, operation) => {
|
|
69
|
+
let candidate = baseName;
|
|
70
|
+
let index = 2;
|
|
71
|
+
while (reservedSchemaNames.has(candidate)) {
|
|
72
|
+
candidate = `${baseName}${index}`;
|
|
73
|
+
index += 1;
|
|
74
|
+
}
|
|
75
|
+
if (candidate !== baseName) {
|
|
76
|
+
warnForOperation(emitWarning, operation, {
|
|
77
|
+
code: "naming-collision",
|
|
78
|
+
message: `Schema name "${baseName}" collided with an existing name and was renamed to "${candidate}".`
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
reservedSchemaNames.add(candidate);
|
|
82
|
+
return generator.addSchema(candidate, schema);
|
|
83
|
+
};
|
|
84
|
+
for (const [path, methods] of Object.entries(spec.paths)) {
|
|
85
|
+
for (const method of methodNames) {
|
|
86
|
+
const operation = methods[method];
|
|
87
|
+
if (Predicate.isUndefined(operation)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const id = operation.operationId ? Utils.camelize(operation.operationId) : `${method.toUpperCase()}${path}`;
|
|
91
|
+
const description = Utils.nonEmptyString(operation.description) ?? Utils.nonEmptyString(operation.summary);
|
|
92
|
+
const {
|
|
93
|
+
pathIds,
|
|
94
|
+
pathTemplate
|
|
95
|
+
} = processPath(path);
|
|
96
|
+
const op = ParsedOperation.makeDeepMutable({
|
|
97
|
+
id,
|
|
98
|
+
method,
|
|
99
|
+
description,
|
|
100
|
+
pathIds,
|
|
101
|
+
pathTemplate
|
|
102
|
+
});
|
|
103
|
+
op.path = path;
|
|
104
|
+
op.operationId = Utils.nonEmptyString(operation.operationId);
|
|
105
|
+
op.tags = [...(operation.tags ?? [])];
|
|
106
|
+
if (isHttpApi && op.tags.length > 1) {
|
|
107
|
+
warnForOperation(emitWarning, op, {
|
|
108
|
+
code: "additional-tags-dropped",
|
|
109
|
+
message: `Additional tags (${op.tags.slice(1).join(", ")}) were dropped. Only the first tag ("${op.tags[0]}") is used for grouping.`
|
|
48
110
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
111
|
+
}
|
|
112
|
+
op.metadata = {
|
|
113
|
+
summary: Utils.nonEmptyString(operation.summary),
|
|
114
|
+
description: Utils.nonEmptyString(operation.description),
|
|
115
|
+
deprecated: operation.deprecated === true,
|
|
116
|
+
externalDocs: operation.externalDocs
|
|
117
|
+
};
|
|
118
|
+
op.effectiveSecurity = cloneSecurityRequirements(operation.security ?? spec.security ?? []);
|
|
119
|
+
if (isHttpApi) {
|
|
120
|
+
warnForAndSecurityRequirements(emitWarning, op);
|
|
121
|
+
}
|
|
122
|
+
const schemaId = Utils.identifier(operation.operationId ?? path);
|
|
123
|
+
const pathParameters = Predicate.isObject(methods) && Array.isArray(methods.parameters) ? methods.parameters : undefined;
|
|
124
|
+
const parameters = resolveOperationParameters(pathParameters, Array.isArray(operation.parameters) ? operation.parameters : undefined, resolveRef);
|
|
125
|
+
for (const parameter of parameters) {
|
|
126
|
+
const parsedParameter = {
|
|
127
|
+
name: parameter.name,
|
|
128
|
+
in: parameter.in,
|
|
129
|
+
required: parameter.required === true,
|
|
130
|
+
description: Utils.nonEmptyString(parameter.description),
|
|
131
|
+
schema: parameter.schema
|
|
132
|
+
};
|
|
133
|
+
switch (parameter.in) {
|
|
134
|
+
case "path":
|
|
135
|
+
{
|
|
136
|
+
op.parameters.path.push(parsedParameter);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "query":
|
|
140
|
+
{
|
|
141
|
+
op.parameters.query.push(parsedParameter);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case "header":
|
|
145
|
+
{
|
|
146
|
+
op.parameters.header.push(parsedParameter);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case "cookie":
|
|
150
|
+
{
|
|
151
|
+
op.parameters.cookie.push(parsedParameter);
|
|
152
|
+
warnForOperation(emitWarning, op, {
|
|
153
|
+
code: "cookie-parameter-dropped",
|
|
154
|
+
message: `Cookie parameter "${parameter.name}" was dropped because non-security cookie parameters are not supported.`
|
|
155
|
+
});
|
|
156
|
+
break;
|
|
63
157
|
}
|
|
64
|
-
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const requestBody = resolveReference(operation.requestBody, resolveRef);
|
|
161
|
+
if (isHttpApi && !methodSupportsRequestBody(op.method) && Predicate.isObject(requestBody)) {
|
|
162
|
+
warnForOperation(emitWarning, op, {
|
|
163
|
+
code: "no-body-method-request-body-skipped",
|
|
164
|
+
message: `Operation was skipped because ${op.method.toUpperCase()} does not support request bodies.`
|
|
165
|
+
});
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const resolvedResponses = Object.entries(operation.responses ?? {}).map(([status, response]) => [status, resolveReference(response, resolveRef)]);
|
|
169
|
+
const hasExplicitSuccessResponse = resolvedResponses.some(([status]) => {
|
|
170
|
+
if (!/^\d{3}$/.test(status)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
return Number(status) < 400;
|
|
174
|
+
});
|
|
175
|
+
if (isHttpApi && hasSuccessfulSseResponse(resolvedResponses, hasExplicitSuccessResponse)) {
|
|
176
|
+
warnForOperation(emitWarning, op, {
|
|
177
|
+
code: "sse-operation-skipped",
|
|
178
|
+
message: "Operation was skipped because successful text/event-stream responses are not supported in HttpApi generation."
|
|
179
|
+
});
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const validParameters = parameters.filter(parameter => parameter.in !== "path" && parameter.in !== "cookie");
|
|
183
|
+
const combinedParameterSchema = buildParameterSchema(validParameters, (parameter, added) => {
|
|
184
|
+
if (parameter.in === "query") {
|
|
185
|
+
Utils.spreadElementsInto(added, op.urlParams);
|
|
186
|
+
} else if (parameter.in === "header") {
|
|
187
|
+
Utils.spreadElementsInto(added, op.headers);
|
|
188
|
+
} else if (parameter.in === "cookie") {
|
|
189
|
+
Utils.spreadElementsInto(added, op.cookies);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
if (combinedParameterSchema !== undefined) {
|
|
193
|
+
op.params = addSchema(`${schemaId}Params`, combinedParameterSchema.schema, op);
|
|
194
|
+
op.paramsOptional = combinedParameterSchema.optional;
|
|
195
|
+
}
|
|
196
|
+
if (isHttpApi) {
|
|
197
|
+
const pathParameterSchema = buildParameterSchema(op.parameters.path);
|
|
198
|
+
if (pathParameterSchema !== undefined) {
|
|
199
|
+
op.pathSchema = addSchema(`${schemaId}PathParams`, pathParameterSchema.schema, op);
|
|
200
|
+
}
|
|
201
|
+
const queryParameterSchema = buildParameterSchema(op.parameters.query);
|
|
202
|
+
if (queryParameterSchema !== undefined) {
|
|
203
|
+
op.querySchema = addSchema(`${schemaId}Query`, queryParameterSchema.schema, op);
|
|
204
|
+
op.querySchemaOptional = queryParameterSchema.optional;
|
|
205
|
+
}
|
|
206
|
+
const headerParameterSchema = buildParameterSchema(op.parameters.header);
|
|
207
|
+
if (headerParameterSchema !== undefined) {
|
|
208
|
+
op.headersSchema = addSchema(`${schemaId}Headers`, headerParameterSchema.schema, op);
|
|
209
|
+
op.headersSchemaOptional = headerParameterSchema.optional;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (Predicate.isNotUndefined(requestBody) && Predicate.isObject(requestBody)) {
|
|
213
|
+
const content = Predicate.isObject(requestBody.content) ? requestBody.content : {};
|
|
214
|
+
const requestSchemaNames = new Map();
|
|
215
|
+
op.requestBody = {
|
|
216
|
+
required: requestBody.required === true,
|
|
217
|
+
contentTypes: Object.keys(content)
|
|
218
|
+
};
|
|
219
|
+
if (isHttpApi && requestBody.required === false) {
|
|
220
|
+
warnForOperation(emitWarning, op, {
|
|
221
|
+
code: "optional-request-body-approximated",
|
|
222
|
+
message: "Optional request body was approximated by adding a no-content payload alternative."
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (Predicate.isNotUndefined(content["application/json"]?.schema)) {
|
|
226
|
+
op.payload = addSchema(`${schemaId}RequestJson`, content["application/json"].schema, op);
|
|
227
|
+
requestSchemaNames.set("application/json", op.payload);
|
|
228
|
+
}
|
|
229
|
+
if (Predicate.isNotUndefined(content["multipart/form-data"]?.schema)) {
|
|
230
|
+
op.payload = addSchema(`${schemaId}RequestFormData`, transformMultipartSchema(content["multipart/form-data"].schema, multipartSchemaRefs, resolveRef), op);
|
|
231
|
+
op.payloadFormData = true;
|
|
232
|
+
requestSchemaNames.set("multipart/form-data", op.payload);
|
|
233
|
+
}
|
|
234
|
+
if (isHttpApi) {
|
|
235
|
+
const representableRequestBody = [];
|
|
236
|
+
for (const [contentType, mediaType] of Object.entries(content)) {
|
|
237
|
+
if (!Predicate.isObject(mediaType) || Predicate.isUndefined(mediaType.schema)) {
|
|
65
238
|
continue;
|
|
66
239
|
}
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const required = "required" in paramSchema ? paramSchema.required : [];
|
|
71
|
-
for (const [name, propSchema] of Object.entries(paramSchema.properties)) {
|
|
72
|
-
const adjustedName = `${parameter.name}[${name}]`;
|
|
73
|
-
schema.properties[adjustedName] = propSchema;
|
|
74
|
-
if (required.includes(name)) {
|
|
75
|
-
schema.required.push(adjustedName);
|
|
76
|
-
}
|
|
77
|
-
added.push(adjustedName);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
schema.properties[parameter.name] = parameter.schema;
|
|
81
|
-
if (parameter.required) {
|
|
82
|
-
schema.required.push(parameter.name);
|
|
83
|
-
}
|
|
84
|
-
added.push(parameter.name);
|
|
240
|
+
const encoding = getRequestMediaTypeEncoding(contentType);
|
|
241
|
+
if (encoding === undefined) {
|
|
242
|
+
continue;
|
|
85
243
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
Utils.spreadElementsInto(added, op.cookies);
|
|
244
|
+
let schemaName = requestSchemaNames.get(contentType);
|
|
245
|
+
if (schemaName === undefined) {
|
|
246
|
+
const schema = encoding === "multipart" ? transformMultipartSchema(mediaType.schema, multipartSchemaRefs, resolveRef) : mediaType.schema;
|
|
247
|
+
schemaName = addSchema(`${schemaId}Request${mediaTypeToSuffix(contentType)}`, schema, op);
|
|
248
|
+
requestSchemaNames.set(contentType, schemaName);
|
|
92
249
|
}
|
|
250
|
+
representableRequestBody.push({
|
|
251
|
+
contentType,
|
|
252
|
+
encoding,
|
|
253
|
+
schema: schemaName
|
|
254
|
+
});
|
|
93
255
|
}
|
|
94
|
-
op.
|
|
95
|
-
|
|
256
|
+
op.requestBodyRepresentable = representableRequestBody;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
let defaultSchema;
|
|
260
|
+
for (const [status, response] of resolvedResponses) {
|
|
261
|
+
if (!Predicate.isObject(response)) {
|
|
262
|
+
continue;
|
|
96
263
|
}
|
|
97
|
-
|
|
98
|
-
|
|
264
|
+
const parsedStatus = isHttpApi ? remapDefaultResponseStatusForHttpApi(status, hasExplicitSuccessResponse) : status;
|
|
265
|
+
if (isHttpApi && status === "default") {
|
|
266
|
+
warnForOperation(emitWarning, op, {
|
|
267
|
+
code: "default-response-remapped",
|
|
268
|
+
message: `Default response was remapped to status ${parsedStatus} for HttpApi generation.`
|
|
269
|
+
});
|
|
99
270
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
op
|
|
271
|
+
const content = Predicate.isObject(response.content) ? response.content : undefined;
|
|
272
|
+
if (isHttpApi && Predicate.isNotUndefined(response.headers)) {
|
|
273
|
+
warnForOperation(emitWarning, op, {
|
|
274
|
+
code: "response-headers-ignored",
|
|
275
|
+
message: `Response headers on status ${status} were ignored in HttpApi generation.`
|
|
276
|
+
});
|
|
103
277
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
278
|
+
const representable = [];
|
|
279
|
+
let jsonSchemaName;
|
|
280
|
+
const jsonResponseSchema = content?.["application/json"]?.schema;
|
|
281
|
+
if (Predicate.isNotUndefined(jsonResponseSchema)) {
|
|
282
|
+
jsonSchemaName = addSchema(`${schemaId}${status}`, jsonResponseSchema, op);
|
|
283
|
+
if (isHttpApi) {
|
|
284
|
+
representable.push({
|
|
285
|
+
contentType: "application/json",
|
|
286
|
+
encoding: "json",
|
|
287
|
+
schema: jsonSchemaName
|
|
288
|
+
});
|
|
110
289
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
290
|
+
}
|
|
291
|
+
if (isHttpApi) {
|
|
292
|
+
for (const [contentType, mediaType] of Object.entries(content ?? {})) {
|
|
293
|
+
if (contentType === "application/json") {
|
|
115
294
|
continue;
|
|
116
295
|
}
|
|
117
|
-
|
|
118
|
-
const statusMajorNumber = Number(status[0]);
|
|
119
|
-
if (Number.isNaN(statusMajorNumber)) {
|
|
296
|
+
if (!Predicate.isObject(mediaType) || Predicate.isUndefined(mediaType.schema)) {
|
|
120
297
|
continue;
|
|
121
298
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
op.errorSchemas.set(statusLower, schemaName);
|
|
299
|
+
const encoding = getResponseMediaTypeEncoding(contentType);
|
|
300
|
+
if (encoding === undefined) {
|
|
301
|
+
continue;
|
|
126
302
|
}
|
|
303
|
+
const schemaName = addSchema(`${schemaId}${status}${mediaTypeToSuffix(contentType)}`, mediaType.schema, op);
|
|
304
|
+
representable.push({
|
|
305
|
+
contentType,
|
|
306
|
+
encoding,
|
|
307
|
+
schema: schemaName
|
|
308
|
+
});
|
|
127
309
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
310
|
+
}
|
|
311
|
+
const isEmptyResponse = Predicate.isUndefined(content) || Object.keys(content).length === 0;
|
|
312
|
+
const parsedResponse = {
|
|
313
|
+
status: parsedStatus,
|
|
314
|
+
description: Utils.nonEmptyString(response.description),
|
|
315
|
+
contentTypes: Predicate.isNotUndefined(content) ? Object.keys(content) : [],
|
|
316
|
+
hasHeaders: Predicate.isNotUndefined(response.headers),
|
|
317
|
+
isEmpty: isEmptyResponse,
|
|
318
|
+
representable
|
|
319
|
+
};
|
|
320
|
+
op.responses.push(parsedResponse);
|
|
321
|
+
if (status === "default") {
|
|
322
|
+
op.defaultResponse = parsedResponse;
|
|
323
|
+
}
|
|
324
|
+
if (Predicate.isNotUndefined(jsonSchemaName)) {
|
|
325
|
+
const schemaName = jsonSchemaName;
|
|
326
|
+
if (status === "default" && !isHttpApi) {
|
|
327
|
+
defaultSchema = schemaName;
|
|
328
|
+
continue;
|
|
134
329
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
op.binaryResponse = true;
|
|
140
|
-
}
|
|
330
|
+
const statusLower = parsedStatus.toLowerCase();
|
|
331
|
+
const statusMajorNumber = Number(parsedStatus[0]);
|
|
332
|
+
if (Number.isNaN(statusMajorNumber)) {
|
|
333
|
+
continue;
|
|
141
334
|
}
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
335
|
+
if (statusMajorNumber < 4) {
|
|
336
|
+
op.successSchemas.set(statusLower, schemaName);
|
|
337
|
+
} else {
|
|
338
|
+
op.errorSchemas.set(statusLower, schemaName);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const sseResponseSchema = content?.["text/event-stream"]?.schema;
|
|
342
|
+
if (Predicate.isUndefined(op.sseSchema) && Predicate.isNotUndefined(sseResponseSchema)) {
|
|
343
|
+
const statusMajorNumber = Number(parsedStatus[0]);
|
|
344
|
+
if (!Number.isNaN(statusMajorNumber) && statusMajorNumber < 4) {
|
|
345
|
+
op.sseSchema = addSchema(`${schemaId}${status}Sse`, sseResponseSchema, op);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (Predicate.isNotUndefined(content?.["application/octet-stream"])) {
|
|
349
|
+
const statusMajorNumber = Number(parsedStatus[0]);
|
|
350
|
+
if (!Number.isNaN(statusMajorNumber) && statusMajorNumber < 4) {
|
|
351
|
+
op.binaryResponse = true;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (isEmptyResponse) {
|
|
355
|
+
if (parsedStatus !== "default") {
|
|
356
|
+
op.voidSchemas.add(parsedStatus.toLowerCase());
|
|
146
357
|
}
|
|
147
358
|
}
|
|
148
|
-
|
|
149
|
-
|
|
359
|
+
}
|
|
360
|
+
if (!isHttpApi && op.successSchemas.size === 0 && Predicate.isNotUndefined(defaultSchema)) {
|
|
361
|
+
op.successSchemas.set("2xx", defaultSchema);
|
|
362
|
+
warnForOperation(emitWarning, op, {
|
|
363
|
+
code: "default-response-remapped",
|
|
364
|
+
message: "Default response was remapped to 2xx for the current HttpClient outputs."
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
operations.push(op);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
metadata: {
|
|
372
|
+
title: spec.info.title,
|
|
373
|
+
version: spec.info.version,
|
|
374
|
+
summary: Utils.nonEmptyString(spec.info.summary),
|
|
375
|
+
description: Utils.nonEmptyString(spec.info.description),
|
|
376
|
+
license: spec.info.license,
|
|
377
|
+
servers: spec.servers
|
|
378
|
+
},
|
|
379
|
+
tags: (spec.tags ?? []).map(tag => ({
|
|
380
|
+
name: tag.name,
|
|
381
|
+
description: Utils.nonEmptyString(tag.description),
|
|
382
|
+
externalDocs: tag.externalDocs
|
|
383
|
+
})),
|
|
384
|
+
securitySchemes,
|
|
385
|
+
operations
|
|
386
|
+
};
|
|
387
|
+
};
|
|
388
|
+
const isOpenApiParameter = parameter => {
|
|
389
|
+
if (!Predicate.isObject(parameter)) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
return typeof parameter.name === "string" && (parameter.in === "path" || parameter.in === "query" || parameter.in === "header" || parameter.in === "cookie");
|
|
393
|
+
};
|
|
394
|
+
const resolveOperationParameters = (pathParameters, operationParameters, resolveRef) => {
|
|
395
|
+
const resolved = new Map();
|
|
396
|
+
const add = parameter => {
|
|
397
|
+
const current = resolveReference(parameter, resolveRef);
|
|
398
|
+
if (!isOpenApiParameter(current)) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const key = `${current.in}:${current.name}`;
|
|
402
|
+
if (resolved.has(key)) {
|
|
403
|
+
resolved.delete(key);
|
|
404
|
+
}
|
|
405
|
+
resolved.set(key, current);
|
|
406
|
+
};
|
|
407
|
+
for (const parameter of pathParameters ?? []) {
|
|
408
|
+
add(parameter);
|
|
409
|
+
}
|
|
410
|
+
for (const parameter of operationParameters ?? []) {
|
|
411
|
+
add(parameter);
|
|
412
|
+
}
|
|
413
|
+
return [...resolved.values()];
|
|
414
|
+
};
|
|
415
|
+
const buildParameterSchema = (parameters, onAdded) => {
|
|
416
|
+
if (parameters.length === 0) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const schema = {
|
|
420
|
+
type: "object",
|
|
421
|
+
properties: {},
|
|
422
|
+
required: [],
|
|
423
|
+
additionalProperties: false
|
|
424
|
+
};
|
|
425
|
+
for (const parameter of parameters) {
|
|
426
|
+
const paramSchema = parameter.schema;
|
|
427
|
+
const added = [];
|
|
428
|
+
if (Predicate.isObject(paramSchema) && "properties" in paramSchema && Predicate.isObject(paramSchema.properties)) {
|
|
429
|
+
const required = "required" in paramSchema ? paramSchema.required : [];
|
|
430
|
+
for (const [name, propertySchema] of Object.entries(paramSchema.properties)) {
|
|
431
|
+
const adjustedName = `${parameter.name}[${name}]`;
|
|
432
|
+
schema.properties[adjustedName] = propertySchema;
|
|
433
|
+
if (required.includes(name)) {
|
|
434
|
+
schema.required.push(adjustedName);
|
|
150
435
|
}
|
|
151
|
-
|
|
436
|
+
added.push(adjustedName);
|
|
152
437
|
}
|
|
438
|
+
} else {
|
|
439
|
+
schema.properties[parameter.name] = parameter.schema;
|
|
440
|
+
if (parameter.required) {
|
|
441
|
+
schema.required.push(parameter.name);
|
|
442
|
+
}
|
|
443
|
+
added.push(parameter.name);
|
|
153
444
|
}
|
|
154
|
-
|
|
155
|
-
|
|
445
|
+
onAdded?.(parameter, added);
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
schema,
|
|
449
|
+
optional: schema.required.length === 0
|
|
450
|
+
};
|
|
451
|
+
};
|
|
452
|
+
const mediaTypeToSuffix = contentType => {
|
|
453
|
+
const normalized = contentType.toLowerCase();
|
|
454
|
+
switch (normalized) {
|
|
455
|
+
case "application/json":
|
|
456
|
+
return "Json";
|
|
457
|
+
case "multipart/form-data":
|
|
458
|
+
return "FormData";
|
|
459
|
+
case "application/x-www-form-urlencoded":
|
|
460
|
+
return "FormUrlEncoded";
|
|
461
|
+
case "text/plain":
|
|
462
|
+
return "Text";
|
|
463
|
+
case "application/octet-stream":
|
|
464
|
+
return "Binary";
|
|
465
|
+
}
|
|
466
|
+
const suffix = Utils.identifier(contentType);
|
|
467
|
+
return suffix.length > 0 ? suffix : "Body";
|
|
468
|
+
};
|
|
469
|
+
const makeHttpApiMultipartSchemaRefs = definitions => {
|
|
470
|
+
const names = new Set(Object.keys(definitions));
|
|
471
|
+
const allocate = base => {
|
|
472
|
+
let candidate = base;
|
|
473
|
+
let index = 2;
|
|
474
|
+
while (names.has(candidate)) {
|
|
475
|
+
candidate = `${base}${index}`;
|
|
476
|
+
index += 1;
|
|
156
477
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const generation = generator.generate(source, spec.components?.schemas ?? {}, options.typeOnly, {
|
|
161
|
-
onEnter: options.onEnter
|
|
162
|
-
});
|
|
163
|
-
return String.stripMargin(`|${openApiTransformer.imports(importName, operations)}
|
|
164
|
-
|${generation}
|
|
165
|
-
|${openApiTransformer.toImplementation(importName, options.name, operations)}
|
|
166
|
-
|
|
|
167
|
-
|${openApiTransformer.toTypes(importName, options.name, operations)}`);
|
|
168
|
-
}, (effect, _, options) => Effect.provideServiceEffect(effect, OpenApiTransformer.OpenApiTransformer, options.typeOnly ? Effect.sync(OpenApiTransformer.makeTransformerTs) : Effect.sync(OpenApiTransformer.makeTransformerSchema)));
|
|
478
|
+
names.add(candidate);
|
|
479
|
+
return candidate;
|
|
480
|
+
};
|
|
169
481
|
return {
|
|
170
|
-
|
|
482
|
+
singleFile: allocate("__HttpApiMultipartSingleFile"),
|
|
483
|
+
files: allocate("__HttpApiMultipartFiles")
|
|
171
484
|
};
|
|
172
|
-
}
|
|
485
|
+
};
|
|
486
|
+
const toDefinitionRef = name => `#/$defs/${name.replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
487
|
+
const withHttpApiMultipartSchemas = (definitions, multipartSchemaRefs) => {
|
|
488
|
+
if (multipartSchemaRefs === undefined) {
|
|
489
|
+
return definitions;
|
|
490
|
+
}
|
|
491
|
+
return {
|
|
492
|
+
...definitions,
|
|
493
|
+
[multipartSchemaRefs.singleFile]: {
|
|
494
|
+
type: "string",
|
|
495
|
+
format: "binary"
|
|
496
|
+
},
|
|
497
|
+
[multipartSchemaRefs.files]: {
|
|
498
|
+
type: "array",
|
|
499
|
+
items: {
|
|
500
|
+
$ref: toDefinitionRef(multipartSchemaRefs.singleFile)
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
};
|
|
505
|
+
const transformMultipartSchema = (schema, multipartSchemaRefs, resolveRef) => {
|
|
506
|
+
if (multipartSchemaRefs === undefined) {
|
|
507
|
+
return schema;
|
|
508
|
+
}
|
|
509
|
+
const singleFileRef = toDefinitionRef(multipartSchemaRefs.singleFile);
|
|
510
|
+
const filesRef = toDefinitionRef(multipartSchemaRefs.files);
|
|
511
|
+
const cache = new Map();
|
|
512
|
+
const stack = new Set();
|
|
513
|
+
const visit = value => {
|
|
514
|
+
if (Array.isArray(value)) {
|
|
515
|
+
return value.map(visit);
|
|
516
|
+
}
|
|
517
|
+
if (!Predicate.isObject(value)) {
|
|
518
|
+
return value;
|
|
519
|
+
}
|
|
520
|
+
if (typeof value.$ref === "string" && value.$ref.startsWith("#/components/schemas/")) {
|
|
521
|
+
const cached = cache.get(value.$ref);
|
|
522
|
+
if (cached !== undefined) {
|
|
523
|
+
return cached;
|
|
524
|
+
}
|
|
525
|
+
if (stack.has(value.$ref)) {
|
|
526
|
+
return value;
|
|
527
|
+
}
|
|
528
|
+
stack.add(value.$ref);
|
|
529
|
+
const transformed = visit(resolveSchemaReference(value.$ref, resolveRef));
|
|
530
|
+
stack.delete(value.$ref);
|
|
531
|
+
cache.set(value.$ref, transformed);
|
|
532
|
+
return transformed;
|
|
533
|
+
}
|
|
534
|
+
if (isMultipartBinaryFile(value)) {
|
|
535
|
+
return {
|
|
536
|
+
$ref: singleFileRef
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
const out = {};
|
|
540
|
+
for (const [key, current] of Object.entries(value)) {
|
|
541
|
+
out[key] = visit(current);
|
|
542
|
+
}
|
|
543
|
+
if (isMultipartBinaryFiles(out, singleFileRef)) {
|
|
544
|
+
return {
|
|
545
|
+
$ref: filesRef
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
return out;
|
|
549
|
+
};
|
|
550
|
+
return visit(schema);
|
|
551
|
+
};
|
|
552
|
+
const resolveSchemaReference = (ref, resolveRef) => {
|
|
553
|
+
let current = {
|
|
554
|
+
$ref: ref
|
|
555
|
+
};
|
|
556
|
+
const seen = new Set();
|
|
557
|
+
while (Predicate.isObject(current) && typeof current.$ref === "string") {
|
|
558
|
+
if (seen.has(current.$ref)) {
|
|
559
|
+
return current;
|
|
560
|
+
}
|
|
561
|
+
seen.add(current.$ref);
|
|
562
|
+
current = resolveRef(current.$ref);
|
|
563
|
+
}
|
|
564
|
+
return current;
|
|
565
|
+
};
|
|
566
|
+
const isMultipartBinaryFile = value => Predicate.isObject(value) && value.type === "string" && (typeof value.format === "string" && value.format.toLowerCase() === "binary" || typeof value.contentEncoding === "string" && value.contentEncoding.toLowerCase() === "binary");
|
|
567
|
+
const isMultipartBinaryFiles = (value, singleFileRef) => {
|
|
568
|
+
if (value.type !== "array") {
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
const items = value.items;
|
|
572
|
+
return isMultipartBinaryFile(items) || Predicate.isObject(items) && items.$ref === singleFileRef;
|
|
573
|
+
};
|
|
574
|
+
const isJsonMediaType = contentType => contentType === "application/json" || contentType.startsWith("application/") && contentType.endsWith("+json");
|
|
575
|
+
const isTextMediaType = contentType => contentType.startsWith("text/");
|
|
576
|
+
const isBinaryMediaType = contentType => contentType === "application/octet-stream" || contentType.startsWith("application/") && (contentType.includes("binary") || contentType.endsWith("+octet-stream"));
|
|
577
|
+
const getRequestMediaTypeEncoding = contentType => {
|
|
578
|
+
const normalized = contentType.toLowerCase();
|
|
579
|
+
if (isJsonMediaType(normalized)) {
|
|
580
|
+
return "json";
|
|
581
|
+
}
|
|
582
|
+
if (normalized === "multipart/form-data") {
|
|
583
|
+
return "multipart";
|
|
584
|
+
}
|
|
585
|
+
if (normalized === "application/x-www-form-urlencoded") {
|
|
586
|
+
return "form-url-encoded";
|
|
587
|
+
}
|
|
588
|
+
if (isTextMediaType(normalized)) {
|
|
589
|
+
return "text";
|
|
590
|
+
}
|
|
591
|
+
if (isBinaryMediaType(normalized)) {
|
|
592
|
+
return "binary";
|
|
593
|
+
}
|
|
594
|
+
return;
|
|
595
|
+
};
|
|
596
|
+
const getResponseMediaTypeEncoding = contentType => {
|
|
597
|
+
const normalized = contentType.toLowerCase();
|
|
598
|
+
if (isJsonMediaType(normalized)) {
|
|
599
|
+
return "json";
|
|
600
|
+
}
|
|
601
|
+
if (normalized === "application/x-www-form-urlencoded") {
|
|
602
|
+
return "form-url-encoded";
|
|
603
|
+
}
|
|
604
|
+
if (isTextMediaType(normalized)) {
|
|
605
|
+
return "text";
|
|
606
|
+
}
|
|
607
|
+
if (isBinaryMediaType(normalized)) {
|
|
608
|
+
return "binary";
|
|
609
|
+
}
|
|
610
|
+
return;
|
|
611
|
+
};
|
|
612
|
+
const resolveReference = (input, resolveRef) => {
|
|
613
|
+
let current = input;
|
|
614
|
+
while (Predicate.isObject(current) && typeof current.$ref === "string") {
|
|
615
|
+
current = resolveRef(current.$ref);
|
|
616
|
+
}
|
|
617
|
+
return current;
|
|
618
|
+
};
|
|
619
|
+
const cloneSecurityRequirements = security => security.map(requirement => Object.fromEntries(Object.entries(requirement).map(([name, scopes]) => [name, [...scopes]])));
|
|
620
|
+
const parseSecuritySchemes = (spec, resolveRef) => {
|
|
621
|
+
const securitySchemes = spec.components?.securitySchemes ?? {};
|
|
622
|
+
const parsed = [];
|
|
623
|
+
for (const [name, value] of Object.entries(securitySchemes)) {
|
|
624
|
+
const scheme = resolveReference(value, resolveRef);
|
|
625
|
+
if (!Predicate.isObject(scheme)) {
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
if (scheme.type === "http") {
|
|
629
|
+
const normalizedScheme = scheme.scheme.toLowerCase();
|
|
630
|
+
if (normalizedScheme === "basic") {
|
|
631
|
+
parsed.push({
|
|
632
|
+
name,
|
|
633
|
+
type: "basic",
|
|
634
|
+
description: Utils.nonEmptyString(scheme.description),
|
|
635
|
+
bearerFormat: undefined,
|
|
636
|
+
key: undefined,
|
|
637
|
+
in: undefined
|
|
638
|
+
});
|
|
639
|
+
} else if (normalizedScheme === "bearer") {
|
|
640
|
+
parsed.push({
|
|
641
|
+
name,
|
|
642
|
+
type: "bearer",
|
|
643
|
+
description: Utils.nonEmptyString(scheme.description),
|
|
644
|
+
bearerFormat: Utils.nonEmptyString(scheme.bearerFormat),
|
|
645
|
+
key: undefined,
|
|
646
|
+
in: undefined
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (scheme.type === "apiKey" && (scheme.in === "header" || scheme.in === "query" || scheme.in === "cookie") && typeof scheme.name === "string") {
|
|
652
|
+
parsed.push({
|
|
653
|
+
name,
|
|
654
|
+
type: "apiKey",
|
|
655
|
+
description: Utils.nonEmptyString(scheme.description),
|
|
656
|
+
bearerFormat: undefined,
|
|
657
|
+
key: scheme.name,
|
|
658
|
+
in: scheme.in
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return parsed;
|
|
663
|
+
};
|
|
664
|
+
const warnForAndSecurityRequirements = (emitWarning, operation) => {
|
|
665
|
+
if (operation.effectiveSecurity.some(requirement => Object.keys(requirement).length === 0)) {
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
for (const requirement of operation.effectiveSecurity) {
|
|
669
|
+
const schemes = Object.keys(requirement);
|
|
670
|
+
if (schemes.length <= 1) {
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
warnForOperation(emitWarning, operation, {
|
|
674
|
+
code: "security-and-downgraded",
|
|
675
|
+
message: `Security requirement requiring all of [${schemes.join(", ")}] was downgraded to a placeholder middleware.`
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
const hasSuccessfulSseResponse = (responses, hasExplicitSuccessResponse) => {
|
|
680
|
+
for (const [status, response] of responses) {
|
|
681
|
+
if (!Predicate.isObject(response)) {
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
const content = Predicate.isObject(response.content) ? response.content : undefined;
|
|
685
|
+
if (Predicate.isUndefined(content?.["text/event-stream"]?.schema)) {
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
const remappedStatus = remapDefaultResponseStatusForHttpApi(status, hasExplicitSuccessResponse);
|
|
689
|
+
const statusCode = Number(remappedStatus);
|
|
690
|
+
if (!Number.isNaN(statusCode) && statusCode < 400) {
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return false;
|
|
695
|
+
};
|
|
696
|
+
const remapDefaultResponseStatusForHttpApi = (status, hasExplicitSuccessResponse) => status === "default" ? hasExplicitSuccessResponse ? "500" : "200" : status;
|
|
697
|
+
const methodSupportsRequestBody = method => method !== "get" && method !== "head" && method !== "options" && method !== "trace";
|
|
698
|
+
const warnForOperation = (emitWarning, operation, warning) => {
|
|
699
|
+
emitWarning({
|
|
700
|
+
...warning,
|
|
701
|
+
path: operation.path,
|
|
702
|
+
method: operation.method,
|
|
703
|
+
operationId: operation.operationId
|
|
704
|
+
});
|
|
705
|
+
};
|
|
173
706
|
function getDialect(spec) {
|
|
174
707
|
return spec.openapi.trim().startsWith("3.0") ? "openapi-3.0" : "openapi-3.1";
|
|
175
708
|
}
|