@samchon/openapi
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -373,3 +373,6 @@ "use strict"; | ||
else if (TypeChecker.isAllOf(schema)) | ||
union.push(convertAllOfSchema(components)(schema)); | ||
if (schema.allOf.length === 1) | ||
visit(schema.allOf[0]); | ||
else | ||
union.push(convertAllOfSchema(components)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
@@ -376,0 +379,0 @@ else if (TypeChecker.isBoolean(schema)) |
@@ -284,3 +284,6 @@ "use strict"; | ||
else if (TypeChecker.isAllOf(schema)) | ||
union.push(convertAllOfSchema(components)(schema)); | ||
if (schema.allOf.length === 1) | ||
visit(schema.allOf[0]); | ||
else | ||
union.push(convertAllOfSchema(components)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
@@ -287,0 +290,0 @@ else if (TypeChecker.isBoolean(schema) || |
@@ -5,3 +5,3 @@ import { OpenApi } from "../OpenApi"; | ||
const convert: (input: SwaggerV2.IDocument) => OpenApi.IDocument; | ||
const convertSchema: (input: SwaggerV2.IJsonSchema) => OpenApi.IJsonSchema; | ||
const convertSchema: (definitions: Record<string, SwaggerV2.IJsonSchema>) => (input: SwaggerV2.IJsonSchema) => OpenApi.IJsonSchema; | ||
} |
@@ -105,3 +105,3 @@ "use strict"; | ||
return function (input) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
return (__assign(__assign({}, input), { parameters: pathItem.parameters !== undefined || input.parameters !== undefined | ||
@@ -119,5 +119,5 @@ ? __spreadArray(__spreadArray([], __read(((_a = pathItem.parameters) !== null && _a !== void 0 ? _a : [])), false), __read(((_b = input.parameters) !== null && _b !== void 0 ? _b : [])), false).map(function (p) { | ||
undefined; | ||
}).map(convertParameter) | ||
}).map(convertParameter((_c = doc.definitions) !== null && _c !== void 0 ? _c : {})) | ||
: undefined, requestBody: (function () { | ||
var _a; | ||
var _a, _b; | ||
var found = (_a = input.parameters) === null || _a === void 0 ? void 0 : _a.find(function (p) { | ||
@@ -129,3 +129,5 @@ var _a, _b; | ||
}); | ||
return found ? convertRequestBody(found) : undefined; | ||
return found | ||
? convertRequestBody((_b = doc.definitions) !== null && _b !== void 0 ? _b : {})(found) | ||
: undefined; | ||
})(), responses: input.responses | ||
@@ -149,20 +151,24 @@ ? Object.fromEntries(Object.entries(input.responses) | ||
}; | ||
var convertParameter = function (input) { return ({ | ||
name: input.name, | ||
in: input.in, | ||
description: input.description, | ||
schema: SwaggerV2Upgrader.convertSchema(input), | ||
required: true, | ||
}); }; | ||
var convertRequestBody = function (input) { return ({ | ||
description: input.description, | ||
content: { | ||
"application/json": { | ||
schema: SwaggerV2Upgrader.convertSchema(input.schema), | ||
var convertParameter = function (definitions) { | ||
return function (input) { return ({ | ||
name: input.name, | ||
in: input.in, | ||
description: input.description, | ||
schema: SwaggerV2Upgrader.convertSchema(definitions)(input), | ||
required: true, | ||
}); }; | ||
}; | ||
var convertRequestBody = function (definitions) { | ||
return function (input) { return ({ | ||
description: input.description, | ||
content: { | ||
"application/json": { | ||
schema: SwaggerV2Upgrader.convertSchema(definitions)(input.schema), | ||
}, | ||
}, | ||
}, | ||
}); }; | ||
}); }; | ||
}; | ||
var convertResponse = function (doc) { | ||
return function (input) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
if (TypeChecker.isReference(input)) { | ||
@@ -179,3 +185,3 @@ var found = (_a = doc.responses) === null || _a === void 0 ? void 0 : _a[(_b = input.$ref.split("/").pop()) !== null && _b !== void 0 ? _b : ""]; | ||
"application/json": { | ||
schema: SwaggerV2Upgrader.convertSchema(input.schema), | ||
schema: SwaggerV2Upgrader.convertSchema((_c = doc.definitions) !== null && _c !== void 0 ? _c : {})(input.schema), | ||
example: input.example, | ||
@@ -192,7 +198,8 @@ }, | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
var _b; | ||
var _c = __read(_a, 2), key = _c[0], value = _c[1]; | ||
return [ | ||
key, | ||
{ | ||
schema: SwaggerV2Upgrader.convertSchema(value), | ||
schema: SwaggerV2Upgrader.convertSchema((_b = doc.definitions) !== null && _b !== void 0 ? _b : {})(value), | ||
in: "header", | ||
@@ -218,4 +225,8 @@ }, | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return [key, SwaggerV2Upgrader.convertSchema(value)]; | ||
var _b; | ||
var _c = __read(_a, 2), key = _c[0], value = _c[1]; | ||
return [ | ||
key, | ||
SwaggerV2Upgrader.convertSchema((_b = input.definitions) !== null && _b !== void 0 ? _b : {})(value), | ||
]; | ||
})), | ||
@@ -298,118 +309,164 @@ securitySchemes: input.securityDefinitions | ||
}; | ||
SwaggerV2Upgrader.convertSchema = function (input) { | ||
var e_1, _a; | ||
var nullable = { | ||
value: false, | ||
default: undefined, | ||
}; | ||
var union = []; | ||
var attribute = __assign(__assign({ title: input.title, description: input.description }, Object.fromEntries(Object.entries(input).filter(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return key.startsWith("x-") && value !== undefined; | ||
}))), { example: input.example, examples: input.examples | ||
? Object.fromEntries(input.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined }); | ||
var visit = function (schema) { | ||
var _a, _b, _c, _d, _e; | ||
// NULLABLE PROPERTY | ||
if (schema["x-nullable"] === | ||
true) { | ||
nullable.value || (nullable.value = true); | ||
if (schema.default === null) | ||
nullable.default = null; | ||
} | ||
if (Array.isArray(schema.enum) && | ||
((_a = schema.enum) === null || _a === void 0 ? void 0 : _a.length) && | ||
((_b = schema.enum) === null || _b === void 0 ? void 0 : _b.some(function (e) { return e === null; }))) | ||
nullable.value || (nullable.value = true); | ||
// UNION TYPE CASE | ||
if (TypeChecker.isAnyOf(schema)) | ||
schema["x-anyOf"].forEach(visit); | ||
else if (TypeChecker.isOneOf(schema)) | ||
schema["x-oneOf"].forEach(visit); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
else if (TypeChecker.isBoolean(schema) || | ||
TypeChecker.isInteger(schema) || | ||
TypeChecker.isNumber(schema) || | ||
TypeChecker.isString(schema)) | ||
if (((_c = schema.enum) === null || _c === void 0 ? void 0 : _c.length) && schema.enum.filter(function (e) { return e !== null; }).length) | ||
union.push.apply(union, __spreadArray([], __read(schema.enum | ||
.filter(function (v) { return v !== null; }) | ||
.map(function (value) { return ({ const: value }); })), false)); | ||
SwaggerV2Upgrader.convertSchema = function (definitions) { | ||
return function (input) { | ||
var e_1, _a; | ||
var nullable = { | ||
value: false, | ||
default: undefined, | ||
}; | ||
var union = []; | ||
var attribute = __assign(__assign({ title: input.title, description: input.description }, Object.fromEntries(Object.entries(input).filter(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return key.startsWith("x-") && value !== undefined; | ||
}))), { example: input.example, examples: input.examples | ||
? Object.fromEntries(input.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined }); | ||
var visit = function (schema) { | ||
var _a, _b, _c, _d, _e; | ||
// NULLABLE PROPERTY | ||
if (schema["x-nullable"] === true) { | ||
nullable.value || (nullable.value = true); | ||
if (schema.default === null) | ||
nullable.default = null; | ||
} | ||
if (Array.isArray(schema.enum) && | ||
((_a = schema.enum) === null || _a === void 0 ? void 0 : _a.length) && | ||
((_b = schema.enum) === null || _b === void 0 ? void 0 : _b.some(function (e) { return e === null; }))) | ||
nullable.value || (nullable.value = true); | ||
// UNION TYPE CASE | ||
if (TypeChecker.isAnyOf(schema)) | ||
schema["x-anyOf"].forEach(visit); | ||
else if (TypeChecker.isOneOf(schema)) | ||
schema["x-oneOf"].forEach(visit); | ||
else if (TypeChecker.isAllOf(schema)) | ||
if (schema.allOf.length === 1) | ||
visit(schema.allOf[0]); | ||
else | ||
union.push(convertAllOfSchema(definitions)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
else if (TypeChecker.isBoolean(schema) || | ||
TypeChecker.isInteger(schema) || | ||
TypeChecker.isNumber(schema) || | ||
TypeChecker.isString(schema)) | ||
if (((_c = schema.enum) === null || _c === void 0 ? void 0 : _c.length) && | ||
schema.enum.filter(function (e) { return e !== null; }).length) | ||
union.push.apply(union, __spreadArray([], __read(schema.enum | ||
.filter(function (v) { return v !== null; }) | ||
.map(function (value) { return ({ const: value }); })), false)); | ||
else | ||
union.push(__assign(__assign(__assign({}, schema), { default: ((_d = schema.default) !== null && _d !== void 0 ? _d : undefined), examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined }), { enum: undefined })); | ||
// INSTANCE TYPE CASE | ||
else if (TypeChecker.isArray(schema)) | ||
union.push(__assign(__assign({}, schema), { items: SwaggerV2Upgrader.convertSchema(definitions)(schema.items), examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined })); | ||
else if (TypeChecker.isObject(schema)) | ||
union.push(__assign(__assign(__assign({}, schema), { | ||
properties: schema.properties | ||
? Object.fromEntries(Object.entries(schema.properties) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], v = _b[1]; | ||
return v !== undefined; | ||
}) | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return [ | ||
key, | ||
SwaggerV2Upgrader.convertSchema(definitions)(value), | ||
]; | ||
})) | ||
: {}, | ||
additionalProperties: schema.additionalProperties | ||
? typeof schema.additionalProperties === "object" && | ||
schema.additionalProperties !== null | ||
? SwaggerV2Upgrader.convertSchema(definitions)(schema.additionalProperties) | ||
: schema.additionalProperties | ||
: undefined, | ||
}), { examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined, required: (_e = schema.required) !== null && _e !== void 0 ? _e : [] })); | ||
else if (TypeChecker.isReference(schema)) | ||
union.push(__assign(__assign({}, schema), { $ref: schema.$ref.replace("#/definitions/", "#/components/schemas/"), examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined })); | ||
else | ||
union.push(__assign(__assign(__assign({}, schema), { default: ((_d = schema.default) !== null && _d !== void 0 ? _d : undefined), examples: schema.examples | ||
union.push(__assign(__assign({}, schema), { examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined }), { enum: undefined })); | ||
// INSTANCE TYPE CASE | ||
else if (TypeChecker.isArray(schema)) | ||
union.push(__assign(__assign({}, schema), { items: SwaggerV2Upgrader.convertSchema(schema.items), examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined })); | ||
else if (TypeChecker.isObject(schema)) | ||
union.push(__assign(__assign(__assign({}, schema), { | ||
properties: schema.properties | ||
? Object.fromEntries(Object.entries(schema.properties) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], v = _b[1]; | ||
return v !== undefined; | ||
}) | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return [key, SwaggerV2Upgrader.convertSchema(value)]; | ||
})) | ||
: {}, | ||
additionalProperties: schema.additionalProperties | ||
? typeof schema.additionalProperties === "object" && | ||
schema.additionalProperties !== null | ||
? SwaggerV2Upgrader.convertSchema(schema.additionalProperties) | ||
: schema.additionalProperties | ||
: undefined, | ||
}), { examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined, required: (_e = schema.required) !== null && _e !== void 0 ? _e : [] })); | ||
else if (TypeChecker.isReference(schema)) | ||
union.push(__assign(__assign({}, schema), { $ref: schema.$ref.replace("#/definitions/", "#/components/schemas/"), examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined })); | ||
else | ||
union.push(__assign(__assign({}, schema), { examples: schema.examples | ||
? Object.fromEntries(schema.examples.map(function (v, i) { return [i.toString(), v]; })) | ||
: undefined })); | ||
}; | ||
visit(input); | ||
if (nullable.value === true && | ||
!union.some(function (e) { return e.type === "null"; })) | ||
union.push({ | ||
type: "null", | ||
default: nullable.default, | ||
}); | ||
if (union.length === 2 && | ||
union.filter(function (x) { return OpenApiTypeChecker_1.OpenApiTypeChecker.isNull(x); }).length === 1) { | ||
var type = union.filter(function (x) { return OpenApiTypeChecker_1.OpenApiTypeChecker.isNull(x) === false; })[0]; | ||
try { | ||
for (var _b = __values([ | ||
"title", | ||
"description", | ||
"deprecated", | ||
"example", | ||
"examples", | ||
]), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var key = _c.value; | ||
if (type[key] !== undefined) | ||
delete type[key]; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
: undefined })); | ||
}; | ||
visit(input); | ||
if (nullable.value === true && | ||
!union.some(function (e) { return e.type === "null"; })) | ||
union.push({ | ||
type: "null", | ||
default: nullable.default, | ||
}); | ||
if (union.length === 2 && | ||
union.filter(function (x) { return OpenApiTypeChecker_1.OpenApiTypeChecker.isNull(x); }).length === 1) { | ||
var type = union.filter(function (x) { return OpenApiTypeChecker_1.OpenApiTypeChecker.isNull(x) === false; })[0]; | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
for (var _b = __values([ | ||
"title", | ||
"description", | ||
"deprecated", | ||
"example", | ||
"examples", | ||
]), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var key = _c.value; | ||
if (type[key] !== undefined) | ||
delete type[key]; | ||
} | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
} | ||
return __assign(__assign(__assign({}, (union.length === 0 | ||
? { type: undefined } | ||
: union.length === 1 | ||
? __assign({}, union[0]) : { oneOf: union.map(function (u) { return (__assign(__assign({}, u), { "x-nullable": undefined })); }) })), attribute), { "x-nullable": undefined }); | ||
return __assign(__assign(__assign({}, (union.length === 0 | ||
? { type: undefined } | ||
: union.length === 1 | ||
? __assign({}, union[0]) : { oneOf: union.map(function (u) { return (__assign(__assign({}, u), { "x-nullable": undefined })); }) })), attribute), { "x-nullable": undefined }); | ||
}; | ||
}; | ||
var convertAllOfSchema = function (definitions) { | ||
return function (input) { | ||
var objects = input.allOf.map(function (schema) { return retrieveObject(definitions)(schema); }); | ||
if (objects.some(function (obj) { return obj === null; })) | ||
return __assign({ type: undefined }, { | ||
allOf: undefined, | ||
}); | ||
return __assign(__assign(__assign({}, input), { type: "object", properties: Object.fromEntries(objects | ||
.map(function (o) { var _a; return Object.entries((_a = o === null || o === void 0 ? void 0 : o.properties) !== null && _a !== void 0 ? _a : {}); }) | ||
.flat() | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return [key, SwaggerV2Upgrader.convertSchema(definitions)(value)]; | ||
})) }), { | ||
allOf: undefined, | ||
required: __spreadArray([], __read(new Set(objects.map(function (o) { var _a; return (_a = o === null || o === void 0 ? void 0 : o.required) !== null && _a !== void 0 ? _a : []; }).flat())), false), | ||
}); | ||
}; | ||
}; | ||
var retrieveObject = function (definitions) { | ||
return function (input, visited) { | ||
var _a, _b; | ||
if (visited === void 0) { visited = new Set(); } | ||
if (TypeChecker.isObject(input)) | ||
return input.properties !== undefined && !input.additionalProperties | ||
? input | ||
: null; | ||
else if (visited.has(input)) | ||
return null; | ||
else | ||
visited.add(input); | ||
if (TypeChecker.isReference(input)) | ||
return retrieveObject(definitions)((_b = definitions === null || definitions === void 0 ? void 0 : definitions[(_a = input.$ref.split("/").pop()) !== null && _a !== void 0 ? _a : ""]) !== null && _b !== void 0 ? _b : {}, visited); | ||
return null; | ||
}; | ||
}; | ||
var TypeChecker; | ||
@@ -438,2 +495,5 @@ (function (TypeChecker) { | ||
}; | ||
TypeChecker.isAllOf = function (schema) { | ||
return schema.allOf !== undefined; | ||
}; | ||
TypeChecker.isOneOf = function (schema) { | ||
@@ -440,0 +500,0 @@ return schema["x-oneOf"] !== undefined; |
{ | ||
"name": "@samchon/openapi", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.", | ||
@@ -19,6 +19,16 @@ "main": "./lib/index.js", | ||
"openapi", | ||
"openapi-generator", | ||
"openapi-validator", | ||
"converter", | ||
"migrate", | ||
"typia", | ||
"nestia" | ||
"nestia", | ||
"llm", | ||
"llm-function-calling", | ||
"structured-output", | ||
"openai", | ||
"chatgpt", | ||
"claude", | ||
"gemini", | ||
"llama" | ||
], | ||
@@ -25,0 +35,0 @@ "repository": { |
@@ -20,5 +20,6 @@ # `@samchon/openapi` | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/openapi/blob/master/LICENSE) | ||
[![npm version](https://img.shields.io/npm/v/@samchon/openapi.svg)](https://www.npmjs.com/package/@samchon/openapi.svg) | ||
[![npm version](https://img.shields.io/npm/v/@samchon/openapi.svg)](https://www.npmjs.com/package/@samchon/openapi) | ||
[![Downloads](https://img.shields.io/npm/dm/@samchon/openapi.svg)](https://www.npmjs.com/package/@samchon/openapi) | ||
[![Build Status](https://github.com/samchon/openapi/workflows/build/badge.svg)](https://github.com/samchon/openapi/actions?query=workflow%3Abuild) | ||
[![Discord Badge](https://img.shields.io/badge/discord-samchon-d91965?style=flat&labelColor=5866f2&logo=discord&logoColor=white&link=https://discord.gg/E94XhzrUCZ)](https://discord.gg/E94XhzrUCZ) | ||
@@ -25,0 +26,0 @@ OpenAPI definitions, converters and LLM function calling application composer. |
@@ -384,3 +384,4 @@ import { OpenApi } from "../OpenApi"; | ||
else if (TypeChecker.isAllOf(schema)) | ||
union.push(convertAllOfSchema(components)(schema)); | ||
if (schema.allOf.length === 1) visit(schema.allOf[0]); | ||
else union.push(convertAllOfSchema(components)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
@@ -387,0 +388,0 @@ else if (TypeChecker.isBoolean(schema)) |
@@ -284,3 +284,4 @@ import { OpenApi } from "../OpenApi"; | ||
else if (TypeChecker.isAllOf(schema)) | ||
union.push(convertAllOfSchema(components)(schema)); | ||
if (schema.allOf.length === 1) visit(schema.allOf[0]); | ||
else union.push(convertAllOfSchema(components)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
@@ -287,0 +288,0 @@ else if ( |
@@ -85,3 +85,3 @@ import { OpenApi } from "../OpenApi"; | ||
) as SwaggerV2.IOperation.IGeneralParameter[] | ||
).map(convertParameter) | ||
).map(convertParameter(doc.definitions ?? {})) | ||
: undefined, | ||
@@ -97,3 +97,5 @@ requestBody: (() => { | ||
}) as SwaggerV2.IOperation.IBodyParameter | undefined; | ||
return found ? convertRequestBody(found) : undefined; | ||
return found | ||
? convertRequestBody(doc.definitions ?? {})(found) | ||
: undefined; | ||
})(), | ||
@@ -112,21 +114,25 @@ responses: input.responses | ||
const convertParameter = ( | ||
input: SwaggerV2.IOperation.IGeneralParameter, | ||
): OpenApi.IOperation.IParameter => ({ | ||
name: input.name, | ||
in: input.in as any, | ||
description: input.description, | ||
schema: convertSchema(input), | ||
required: true, | ||
}); | ||
const convertRequestBody = ( | ||
input: SwaggerV2.IOperation.IBodyParameter, | ||
): OpenApi.IOperation.IRequestBody => ({ | ||
description: input.description, | ||
content: { | ||
"application/json": { | ||
schema: convertSchema(input.schema), | ||
const convertParameter = | ||
(definitions: Record<string, SwaggerV2.IJsonSchema>) => | ||
( | ||
input: SwaggerV2.IOperation.IGeneralParameter, | ||
): OpenApi.IOperation.IParameter => ({ | ||
name: input.name, | ||
in: input.in as any, | ||
description: input.description, | ||
schema: convertSchema(definitions)(input), | ||
required: true, | ||
}); | ||
const convertRequestBody = | ||
(definitions: Record<string, SwaggerV2.IJsonSchema>) => | ||
( | ||
input: SwaggerV2.IOperation.IBodyParameter, | ||
): OpenApi.IOperation.IRequestBody => ({ | ||
description: input.description, | ||
content: { | ||
"application/json": { | ||
schema: convertSchema(definitions)(input.schema), | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
@@ -151,3 +157,3 @@ const convertResponse = | ||
"application/json": { | ||
schema: convertSchema(input.schema), | ||
schema: convertSchema(doc.definitions ?? {})(input.schema), | ||
example: input.example, | ||
@@ -166,3 +172,3 @@ }, | ||
{ | ||
schema: convertSchema(value), | ||
schema: convertSchema(doc.definitions ?? {})(value), | ||
in: "header", | ||
@@ -186,3 +192,6 @@ }, | ||
.filter(([_, v]) => v !== undefined) | ||
.map(([key, value]) => [key, convertSchema(value)]), | ||
.map(([key, value]) => [ | ||
key, | ||
convertSchema(input.definitions ?? {})(value), | ||
]), | ||
), | ||
@@ -259,63 +268,84 @@ securitySchemes: input.securityDefinitions | ||
export const convertSchema = ( | ||
input: SwaggerV2.IJsonSchema, | ||
): OpenApi.IJsonSchema => { | ||
const nullable: { value: boolean; default?: null } = { | ||
value: false, | ||
default: undefined, | ||
}; | ||
const union: OpenApi.IJsonSchema[] = []; | ||
const attribute: OpenApi.IJsonSchema.__IAttribute = { | ||
title: input.title, | ||
description: input.description, | ||
...Object.fromEntries( | ||
Object.entries(input).filter( | ||
([key, value]) => key.startsWith("x-") && value !== undefined, | ||
export const convertSchema = | ||
(definitions: Record<string, SwaggerV2.IJsonSchema>) => | ||
(input: SwaggerV2.IJsonSchema): OpenApi.IJsonSchema => { | ||
const nullable: { value: boolean; default?: null } = { | ||
value: false, | ||
default: undefined, | ||
}; | ||
const union: OpenApi.IJsonSchema[] = []; | ||
const attribute: OpenApi.IJsonSchema.__IAttribute = { | ||
title: input.title, | ||
description: input.description, | ||
...Object.fromEntries( | ||
Object.entries(input).filter( | ||
([key, value]) => key.startsWith("x-") && value !== undefined, | ||
), | ||
), | ||
), | ||
example: input.example, | ||
examples: input.examples | ||
? Object.fromEntries(input.examples.map((v, i) => [i.toString(), v])) | ||
: undefined, | ||
}; | ||
const visit = (schema: SwaggerV2.IJsonSchema): void => { | ||
// NULLABLE PROPERTY | ||
if ( | ||
(schema as SwaggerV2.IJsonSchema.__ISignificant<any>)["x-nullable"] === | ||
true | ||
) { | ||
nullable.value ||= true; | ||
if ((schema as SwaggerV2.IJsonSchema.INumber).default === null) | ||
nullable.default = null; | ||
} | ||
if ( | ||
Array.isArray((schema as SwaggerV2.IJsonSchema.INumber).enum) && | ||
(schema as SwaggerV2.IJsonSchema.INumber).enum?.length && | ||
(schema as SwaggerV2.IJsonSchema.INumber).enum?.some((e) => e === null) | ||
) | ||
nullable.value ||= true; | ||
// UNION TYPE CASE | ||
if (TypeChecker.isAnyOf(schema)) schema["x-anyOf"].forEach(visit); | ||
else if (TypeChecker.isOneOf(schema)) schema["x-oneOf"].forEach(visit); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
else if ( | ||
TypeChecker.isBoolean(schema) || | ||
TypeChecker.isInteger(schema) || | ||
TypeChecker.isNumber(schema) || | ||
TypeChecker.isString(schema) | ||
) | ||
if (schema.enum?.length && schema.enum.filter((e) => e !== null).length) | ||
union.push( | ||
...schema.enum | ||
.filter((v) => v !== null) | ||
.map((value) => ({ const: value })), | ||
); | ||
else | ||
example: input.example, | ||
examples: input.examples | ||
? Object.fromEntries(input.examples.map((v, i) => [i.toString(), v])) | ||
: undefined, | ||
}; | ||
const visit = (schema: SwaggerV2.IJsonSchema): void => { | ||
// NULLABLE PROPERTY | ||
if ( | ||
(schema as SwaggerV2.IJsonSchema.__ISignificant<any>)[ | ||
"x-nullable" | ||
] === true | ||
) { | ||
nullable.value ||= true; | ||
if ((schema as SwaggerV2.IJsonSchema.INumber).default === null) | ||
nullable.default = null; | ||
} | ||
if ( | ||
Array.isArray((schema as SwaggerV2.IJsonSchema.INumber).enum) && | ||
(schema as SwaggerV2.IJsonSchema.INumber).enum?.length && | ||
(schema as SwaggerV2.IJsonSchema.INumber).enum?.some( | ||
(e) => e === null, | ||
) | ||
) | ||
nullable.value ||= true; | ||
// UNION TYPE CASE | ||
if (TypeChecker.isAnyOf(schema)) schema["x-anyOf"].forEach(visit); | ||
else if (TypeChecker.isOneOf(schema)) schema["x-oneOf"].forEach(visit); | ||
else if (TypeChecker.isAllOf(schema)) | ||
if (schema.allOf.length === 1) visit(schema.allOf[0]); | ||
else union.push(convertAllOfSchema(definitions)(schema)); | ||
// ATOMIC TYPE CASE (CONSIDER ENUM VALUES) | ||
else if ( | ||
TypeChecker.isBoolean(schema) || | ||
TypeChecker.isInteger(schema) || | ||
TypeChecker.isNumber(schema) || | ||
TypeChecker.isString(schema) | ||
) | ||
if ( | ||
schema.enum?.length && | ||
schema.enum.filter((e) => e !== null).length | ||
) | ||
union.push( | ||
...schema.enum | ||
.filter((v) => v !== null) | ||
.map((value) => ({ const: value })), | ||
); | ||
else | ||
union.push({ | ||
...schema, | ||
default: (schema.default ?? undefined) satisfies | ||
| boolean | ||
| number | ||
| string | ||
| undefined as any, | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
...{ enum: undefined }, | ||
}); | ||
// INSTANCE TYPE CASE | ||
else if (TypeChecker.isArray(schema)) | ||
union.push({ | ||
...schema, | ||
default: (schema.default ?? undefined) satisfies | ||
| boolean | ||
| number | ||
| string | ||
| undefined as any, | ||
items: convertSchema(definitions)(schema.items), | ||
examples: schema.examples | ||
@@ -326,97 +356,143 @@ ? Object.fromEntries( | ||
: undefined, | ||
...{ enum: undefined }, | ||
}); | ||
// INSTANCE TYPE CASE | ||
else if (TypeChecker.isArray(schema)) | ||
union.push({ | ||
...schema, | ||
items: convertSchema(schema.items), | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
}); | ||
else if (TypeChecker.isObject(schema)) | ||
union.push({ | ||
...schema, | ||
...{ | ||
properties: schema.properties | ||
else if (TypeChecker.isObject(schema)) | ||
union.push({ | ||
...schema, | ||
...{ | ||
properties: schema.properties | ||
? Object.fromEntries( | ||
Object.entries(schema.properties) | ||
.filter(([_, v]) => v !== undefined) | ||
.map(([key, value]) => [ | ||
key, | ||
convertSchema(definitions)(value), | ||
]), | ||
) | ||
: {}, | ||
additionalProperties: schema.additionalProperties | ||
? typeof schema.additionalProperties === "object" && | ||
schema.additionalProperties !== null | ||
? convertSchema(definitions)(schema.additionalProperties) | ||
: schema.additionalProperties | ||
: undefined, | ||
}, | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
Object.entries(schema.properties) | ||
.filter(([_, v]) => v !== undefined) | ||
.map(([key, value]) => [key, convertSchema(value)]), | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: {}, | ||
additionalProperties: schema.additionalProperties | ||
? typeof schema.additionalProperties === "object" && | ||
schema.additionalProperties !== null | ||
? convertSchema(schema.additionalProperties) | ||
: schema.additionalProperties | ||
: undefined, | ||
}, | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
required: schema.required ?? [], | ||
}); | ||
else if (TypeChecker.isReference(schema)) | ||
required: schema.required ?? [], | ||
}); | ||
else if (TypeChecker.isReference(schema)) | ||
union.push({ | ||
...schema, | ||
$ref: schema.$ref.replace( | ||
"#/definitions/", | ||
"#/components/schemas/", | ||
), | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
}); | ||
else | ||
union.push({ | ||
...schema, | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
}); | ||
}; | ||
visit(input); | ||
if ( | ||
nullable.value === true && | ||
!union.some((e) => (e as OpenApi.IJsonSchema.INull).type === "null") | ||
) | ||
union.push({ | ||
...schema, | ||
$ref: schema.$ref.replace("#/definitions/", "#/components/schemas/"), | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
type: "null", | ||
default: nullable.default, | ||
}); | ||
else | ||
union.push({ | ||
...schema, | ||
examples: schema.examples | ||
? Object.fromEntries( | ||
schema.examples.map((v, i) => [i.toString(), v]), | ||
) | ||
: undefined, | ||
}); | ||
if ( | ||
union.length === 2 && | ||
union.filter((x) => OpenApiTypeChecker.isNull(x)).length === 1 | ||
) { | ||
const type: OpenApi.IJsonSchema = union.filter( | ||
(x) => OpenApiTypeChecker.isNull(x) === false, | ||
)[0]; | ||
for (const key of [ | ||
"title", | ||
"description", | ||
"deprecated", | ||
"example", | ||
"examples", | ||
] as const) | ||
if (type[key] !== undefined) delete type[key]; | ||
} | ||
return { | ||
...(union.length === 0 | ||
? { type: undefined } | ||
: union.length === 1 | ||
? { ...union[0] } | ||
: { oneOf: union.map((u) => ({ ...u, "x-nullable": undefined })) }), | ||
...attribute, | ||
...{ "x-nullable": undefined }, | ||
}; | ||
}; | ||
visit(input); | ||
if ( | ||
nullable.value === true && | ||
!union.some((e) => (e as OpenApi.IJsonSchema.INull).type === "null") | ||
) | ||
union.push({ | ||
type: "null", | ||
default: nullable.default, | ||
}); | ||
if ( | ||
union.length === 2 && | ||
union.filter((x) => OpenApiTypeChecker.isNull(x)).length === 1 | ||
) { | ||
const type: OpenApi.IJsonSchema = union.filter( | ||
(x) => OpenApiTypeChecker.isNull(x) === false, | ||
)[0]; | ||
for (const key of [ | ||
"title", | ||
"description", | ||
"deprecated", | ||
"example", | ||
"examples", | ||
] as const) | ||
if (type[key] !== undefined) delete type[key]; | ||
} | ||
return { | ||
...(union.length === 0 | ||
? { type: undefined } | ||
: union.length === 1 | ||
? { ...union[0] } | ||
: { oneOf: union.map((u) => ({ ...u, "x-nullable": undefined })) }), | ||
...attribute, | ||
...{ "x-nullable": undefined }, | ||
const convertAllOfSchema = | ||
(definitions: Record<string, SwaggerV2.IJsonSchema>) => | ||
(input: SwaggerV2.IJsonSchema.IAllOf): OpenApi.IJsonSchema => { | ||
const objects: Array<SwaggerV2.IJsonSchema.IObject | null> = | ||
input.allOf.map((schema) => retrieveObject(definitions)(schema)); | ||
if (objects.some((obj) => obj === null)) | ||
return { | ||
type: undefined, | ||
...{ | ||
allOf: undefined, | ||
}, | ||
}; | ||
return { | ||
...input, | ||
type: "object", | ||
properties: Object.fromEntries( | ||
objects | ||
.map((o) => Object.entries(o?.properties ?? {})) | ||
.flat() | ||
.map( | ||
([key, value]) => | ||
[key, convertSchema(definitions)(value)] as const, | ||
), | ||
), | ||
...{ | ||
allOf: undefined, | ||
required: [...new Set(objects.map((o) => o?.required ?? []).flat())], | ||
}, | ||
}; | ||
}; | ||
}; | ||
const retrieveObject = | ||
(definitions: Record<string, SwaggerV2.IJsonSchema>) => | ||
( | ||
input: SwaggerV2.IJsonSchema, | ||
visited: Set<SwaggerV2.IJsonSchema> = new Set(), | ||
): SwaggerV2.IJsonSchema.IObject | null => { | ||
if (TypeChecker.isObject(input)) | ||
return input.properties !== undefined && !input.additionalProperties | ||
? input | ||
: null; | ||
else if (visited.has(input)) return null; | ||
else visited.add(input); | ||
if (TypeChecker.isReference(input)) | ||
return retrieveObject(definitions)( | ||
definitions?.[input.$ref.split("/").pop() ?? ""] ?? {}, | ||
visited, | ||
); | ||
return null; | ||
}; | ||
namespace TypeChecker { | ||
@@ -451,2 +527,6 @@ export const isBoolean = ( | ||
(schema as SwaggerV2.IJsonSchema.IReference).$ref !== undefined; | ||
export const isAllOf = ( | ||
schema: SwaggerV2.IJsonSchema, | ||
): schema is SwaggerV2.IJsonSchema.IAllOf => | ||
(schema as SwaggerV2.IJsonSchema.IAllOf).allOf !== undefined; | ||
export const isOneOf = ( | ||
@@ -453,0 +533,0 @@ schema: SwaggerV2.IJsonSchema, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1727816
30651
494