Skip to content

Commit

Permalink
fix(various):
Browse files Browse the repository at this point in the history
 - need to clone schema before passing it to fastest-validator
 - add information on object
  • Loading branch information
thib3113 committed Nov 27, 2023
1 parent ea13885 commit 457b587
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -45,6 +45,8 @@
},
"keywords": [
"openapi",
"openapi 3.1",
"fastest-validator",
"swagger",
"moleculer",
"moleculer-web"
Expand Down
17 changes: 7 additions & 10 deletions src/Converters/FastestValidatorConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
? []
: (
[
Expand All @@ -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<string, string>)?.schema as ValidationRuleObject;
const baseRule = this.validator.getRuleFromSchema(clonedRule)?.schema as ValidationRuleObject;
const rule = {
...parentProperties,
...baseRule
Expand Down
9 changes: 1 addition & 8 deletions src/MoleculerOpenAPIGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/OpenApiGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
};
Expand Down

0 comments on commit 457b587

Please sign in to comment.