Integration with FeathersJS
bravo-kernel opened this issue · 3 comments
Follow these steps to automatically generate model schemas for feathers-swagger.
- Create a
src/sequelize-to-json-schemas.js
schema configuration file:
const {
JsonSchemaManager,
OpenApi3Strategy,
} = require('@alt3/sequelize-to-json-schemas');
module.exports = function init(app) {
const schemaManager = new JsonSchemaManager({
baseUri: 'https://api.example.com',
absolutePaths: true,
});
const openApi3Strategy = new OpenApi3Strategy();
app.set('jsonSchemaManager', schemaManager);
app.set('openApi3Strategy', openApi3Strategy);
};
- Update the feathers-swagger configuration in
src/app.js
so that it looks similar to:
app.configure(sequelizeToJsonSchemas);
// Set up feathers-swagger with auto-generated OpenAPI v3 model schemas
app.configure(
swagger({
openApiVersion: 3,
uiIndex: true,
docsPath: '/docs',
docsJsonPath: '/docs/schema',
specs: {
info: {
title: 'Your title',
description: 'Your description',
version: '0.0.1',
},
},
defaults: {
schemasGenerator(service, model, modelName) {
const modelSchema = app
.get('jsonSchemaManager')
.generate(
service.options.Model,
app.get('openApi3Strategy'),
service.options.Model.options.jsonSchema,
);
return {
[model]: modelSchema,
[`${model}_list`]: {
title: `${modelName} list`,
type: 'array',
items: { $ref: `#/components/schemas/${model}` },
},
};
},
},
}),
);
Model specific overrides
Model specific overrides MUST be specified in your sequelize model file as options.jsonSchema
so they get picked up automatically when generating the model schemas. E.g.
model.options.jsonSchema = {
title: 'Custom model title'
}
On same level as/directly below
model.associations
Configuration Options
Please refer to the README for all available configuration options.
.generate(service.service.options.Model, app.get('openApi3Strategy'), service.options.Model.options.jsonSchema);
^
TypeError: Cannot read property 'options' of undefined
Getting this issue when i config swagger before calling services.
Issue disappear when i configure swagger after services, but at that time docs have no operations as you can see in screenshot
@justraman I had the same issue. It seems that was caused by the /auth (authentication) service. Feathers internal authentication service doesn't have options and Model properties. The workaround is simple, just check the existence of the properties before using them:
if (service.options && service.options.Model) {
const modelSchema = app
.get("jsonSchemaManager")
.generate(
service.options.Model,
app.get("openApi3Strategy"),
service.options.Model.options.jsonSchema
);
return {
[model]: modelSchema,
[`${model}_list`]: {
title: `${modelName} list`,
type: "array",
items: { $ref: `#/components/schemas/${model}` },
},
}
This works for me