diff --git a/README.md b/README.md index ea7a5e6..ea9a0b7 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,22 @@ This is a fork of moleculer-auto-openapi . Documentation need to be rewritten ! -TODO +Actually works only with a modified version of fastest-validator : github:thib3113/fastest-validator#fork +waiting a PR -[] - check multipart upload => bad content type -[] - allow to add custom mappers -[] - tests merges +## TODO + +- check multipart upload => bad content type +- allow to add custom mappers +- tests merges + +## Key features + - support multiple moleculer-web server, allowing to separate apis + - `Fastest-Validator` support, to generate openapi directly from parameters, with examples + - Openapi 3.1 support + - cached openapi, and regeneration when needed + - granular and reusable configuration + - Typescript exports of mixin settings, and openapi parameters # typescript this package rely on the library `openapi-types` feel free to install it in your devDependencies to better typing; diff --git a/package.json b/package.json index 21b2442..83f2816 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@spailybot/moleculer-auto-openapi", "version": "0.1.4", - "description": "Auto generate openapi(swagger) scheme for moleculer", + "description": "Generate openapi scheme for moleculer", "main": "./lib/index.cjs", "module": "./lib/index.mjs", "types": "./lib/index.d.ts", @@ -45,6 +45,8 @@ }, "keywords": [ "openapi", + "openapi 3.1", + "fastest-validator", "swagger", "moleculer", "moleculer-web" diff --git a/src/Converters/FastestValidatorConverter.ts b/src/Converters/FastestValidatorConverter.ts index 115b281..128b54e 100644 --- a/src/Converters/FastestValidatorConverter.ts +++ b/src/Converters/FastestValidatorConverter.ts @@ -42,7 +42,7 @@ export class FastestValidatorConverter implements IConverter { delete schema.$$root; - return this.getSchemaObjectFromRule(schema as ValidationRule); + return this.getSchemaObjectFromRule(schema as ValidationRuleObject); } public getSchemaObjectFromRule( @@ -54,9 +54,12 @@ export class FastestValidatorConverter implements IConverter { throw new Error(`bad initialisation . validator ? ${!!this.validator} | string mapper ${!!this.mappers?.string}`); } + //clone the object, else fastestValidator will remove $$oa + const clonedRule: ValidationRule = typeof pRule === 'object' ? (Array.isArray(pRule) ? [...pRule] : { ...pRule }) : pRule; + //extract known params extensions const extensions: Array<[string, string | boolean]> = - Array.isArray(pRule) || typeof pRule !== 'object' || !pRule.$$oa + Array.isArray(clonedRule) || typeof clonedRule !== 'object' || !clonedRule.$$oa ? [] : ( [ @@ -73,15 +76,9 @@ export class FastestValidatorConverter implements IConverter { extension: EOAExtensions.deprecated } ] as Array<{ property: keyof FVOARuleMetaKeys; extension: EOAExtensions }> - ).map(({ property, extension }) => { - const value = pRule.$$oa[property]; - - delete pRule.$$oa[property]; - - return [extension, value]; - }); + ).map(({ property, extension }) => [extension, clonedRule.$$oa[property]]); - const baseRule = this.validator.getRuleFromSchema(pRule as Record)?.schema as ValidationRuleObject; + const baseRule = this.validator.getRuleFromSchema(clonedRule)?.schema as ValidationRuleObject; const rule = { ...parentProperties, ...baseRule diff --git a/src/MoleculerOpenAPIGenerator.ts b/src/MoleculerOpenAPIGenerator.ts index 2af9249..443ed40 100644 --- a/src/MoleculerOpenAPIGenerator.ts +++ b/src/MoleculerOpenAPIGenerator.ts @@ -146,18 +146,11 @@ export class MoleculerOpenAPIGenerator { const services = await this.fetchServicesWithActions(ctx); - let aliases = await this.getAliases(ctx, services); + const aliases = await this.getAliases(ctx, services); - // this.attachParamsAndOpenapiFromEveryActionToRoutes(routes, services); - // - // routes = Object.fromEntries(Object.entries(routes).filter(([name, r]) => r.openapi !== false)); - // return new OpenApiGenerator(this.logger, this.validator, JSON.parse(JSON.stringify(this.settings.openapi))).generate( version, aliases ); - // this.routesToOpenApi(routes, doc); - // - // return doc; } } diff --git a/src/OpenApiGenerator.ts b/src/OpenApiGenerator.ts index b60d88e..ee349ca 100644 --- a/src/OpenApiGenerator.ts +++ b/src/OpenApiGenerator.ts @@ -421,6 +421,10 @@ export class OpenApiGenerator { }) ); + if (this.components.schemas[schemeName]) { + this.logger.warn(`Generator - schema ${schemeName} already exist and will be overwrite`); + } + this.components.schemas[schemeName] = { type: 'object', properties, @@ -463,6 +467,8 @@ export class OpenApiGenerator { if (rule.type == 'object' && rule.properties) { // create child schema per object return { + summary: rule.title, + deprecated: rule.deprecated, description: rule.description, ...this._createSchemaComponentFromObject(nextSchemeName, rule.properties, { default: rule.default }) };