Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with FeathersJS #17

Closed
bravo-kernel opened this issue Sep 12, 2019 · 3 comments
Closed

Integration with FeathersJS #17

bravo-kernel opened this issue Sep 12, 2019 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@bravo-kernel
Copy link
Collaborator

bravo-kernel commented Sep 12, 2019

Follow these steps to automatically generate model schemas for feathers-swagger.

  1. 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);
};
  1. 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.

@justraman
Copy link

.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
Screenshot from 2020-05-07 13-01-22

@jackywxd
Copy link

jackywxd commented Jun 17, 2020

@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}` },
            },
     
}

@Kelvince01
Copy link

This works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants