feathersjs-ecosystem / feathers-swagger

Add documentation to your FeatherJS services and feed them to Swagger UI.
MIT License
226 stars 63 forks source link

Error: Invalid service object passed for path `docs` #241

Closed ImanHz closed 1 year ago

ImanHz commented 1 year ago

I am using feathers-swagger version 2.0.0 and feathersjs version 5.0.0-pre.29 and trying to add swagger UI to my application. Configuration in the app.ts:


import swagger from 'feathers-swagger';
...
...
app.configure(swagger({
  ui: swagger.swaggerUI({docsPath: '/docs'}),
  openApiVersion: 2,
  specs: {
    info: {
      title: 'Some title',
      version: '0.0.1',
    },
    schemes: ['http'] // Optionally set the protocol schema used (sometimes required when host on https)
  }
}));

In one of my services, called countries, I first added required doc attribute to the service class (class.ts):

export class CountriesService extends KnexService<CountriesResult, CountriesData, CountriesParams> implements ServiceSwaggerAddon{
  constructor(options: KnexAdapterOptions, _app: Application, docs: ServiceSwaggerOptions) {
    super(options);
    this.docs = docs;
  }
   docs: ServiceSwaggerOptions;
}

and then in the service.ts:

  const docs = {
    description: 'A service for fetching country names',
    definitions: {
      countries: {
        type: 'object',
        required: ['text'],
        properties: {
          text: {
            type: 'string',
            description: 'The message text'
          },
          useId: {
            type: 'string',
            description: 'The id of the country'
          }
        }
      }
    }
  }
  app.use('countries', new CountriesService(options, app, docs), {
    methods: ['find', 'get', 'create', 'update', 'patch', 'remove'],
    events: []
  })
  app.service('countries').hooks(countriesHooks)
}

Running the app, causes an error:

throw new Error(Invalid service object passed for path \${location}`) ^ Error: Invalid service object passed for pathdocs` at wrapService (.../node_modules/@feathersjs/feathers/src/service.ts:68:11) at Feathers.use (.../node_modules/@feathersjs/feathers/src/application.ts:104:37) at Feathers.use (.../node_modules/@feathersjs/koa/src/index.ts:38:37) at initSwaggerUI (.../node_modules/feathers-swagger/lib/swagger-ui-dist.js:53:9) at Feathers. (.../node_modules/feathers-swagger/lib/index.js:45:7) at Feathers.configure .../node_modules/@feathersjs/feathers/src/application.ts:60:14) at Object. (.../src/app.ts:22:5) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Module.m._compile (.../node_modules/ts-node/src/index.ts:1618:23) at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

Node version: 16.17.1 feathers-swagger: 2.0.0 feathersjs :5.0.0-pre.29 feathers/koa: 5.0.0-pre.29 os: linux, ubuntu 22

asadoll commented 1 year ago

I believe this comment suggests a workaround.

Mairu commented 1 year ago

Hi, version 2.0.0 will not work with dove and koa, you can try the https://github.com/feathersjs-ecosystem/feathers-swagger/tree/custom-methods-v5 branch directly from GitHub, because no npm version exists yet.

The way you added the docs property to the service instance should work, although you could add it as shown in the linked comment with less code.

Mairu commented 1 year ago

Pre release has been published as 3.0.0-pre.0