Skip to content

Commit

Permalink
feat: only generate docs for methods that are in the serviceOptions m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
Mairu committed Dec 11, 2022
1 parent 3cf6c6b commit 31399da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const utils = require('./utils');
const { assignWithSet } = require('./helpers');
const { getCustomMethods } = require('./custom-methods');

const serviceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'];

const defaultMethods = [
{ operation: 'find', method: 'find', id: false, httpMethod: 'get', multi: false },
{ operation: 'get', method: 'get', id: true, httpMethod: 'get', multi: false },
Expand Down Expand Up @@ -136,6 +138,16 @@ class OpenApiGenerator {
if (doc.operations === undefined) {
doc.operations = {};
}

// disable methods that are not defined for feathers 5
if (serviceOptions && Array.isArray(serviceOptions.methods)) {
serviceMethods.forEach(serviceMethod => {
if (!serviceOptions.methods.includes(serviceMethod)) {
doc.operations[serviceMethod] = false;
}
});
}

const idName = service.id || 'id';
const idType = doc.idType || this.config.idType || 'integer';
let version = this.config.versionPrefix ? path.match(this.config.versionPrefix) : null;
Expand Down
36 changes: 36 additions & 0 deletions test/v3/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,42 @@ describe('openopi v3 generator', function () {
});
});
});

describe('serviceOptions (v5)', () => {
it('should consume consume docs from serviceOptions', () => {
delete service.docs;
gen.addService(service, 'message', {
methods: ['find', 'get'],
events: [],
docs: {
description: 'my custom description'
}
});

expect(specs.tags).to.deep.equal([
{
description: 'my custom description',
name: 'message'
}
]);
expect(specs.paths['/message'].get).to.exist;
expect(specs.paths['/message/{id}'].get).to.exist;
});

it('should disable methods that are missing from methods array', () => {
delete service.docs;
gen.addService(service, 'message', {
methods: ['find'],
events: [],
docs: {
description: 'my custom description'
}
});

expect(specs.paths['/message'].get).to.exist;
expect(specs.paths['/message/{id}']).to.not.exist;
});
});
});

describe('custom methods', function () {
Expand Down

0 comments on commit 31399da

Please sign in to comment.