diff --git a/lib/openapi.js b/lib/openapi.js index 4b813e6..2579ba7 100644 --- a/lib/openapi.js +++ b/lib/openapi.js @@ -48,7 +48,8 @@ function ignoreService (service, path, includeConfig, ignoreConfig) { function determineMultiOperations (service) { if (service.options && service.options.multi) { - return ['remove', 'update', 'patch', 'create'].filter((operation) => _.isFunction(service[operation])); + const operations = service.options.multi === true ? serviceMethods : service.options.multi; + return operations.filter((operation) => _.isFunction(service[operation])); } return []; diff --git a/lib/utils.js b/lib/utils.js index a8625cb..531654b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -50,28 +50,6 @@ exports.idPathParameters = function idPathParameters (idName, idSeparator) { return `{${Array.isArray(idName) ? idName.join(`}${idSeparator}{`) : idName}}`; }; -const allowedDefaultRefs = [ - 'findResponse', - 'getResponse', - 'createRequest', - 'createResponse', - 'createMultiRequest', - 'createMultiResponse', - 'updateRequest', - 'updateResponse', - 'updateMultiRequest', - 'updateMultiResponse', - 'patchRequest', - 'patchResponse', - 'patchMultiRequest', - 'patchMultiResponse', - 'removeResponse', - 'removeMultiResponse', - 'filterParameter', - 'sortParameter', - 'queryParameters' -]; - exports.defaultTransformSchema = function defaultTransformSchema (schema) { const allowedProperties = pick(schema, [ 'title', @@ -201,8 +179,15 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc if (dataSchema) { const dataSchemeName = dataSchema.$id || `${baseSchemeName}Data`; + const dataListSchemeName = `${dataSchemeName}List`; + serviceDocs.schemas[dataSchemeName] = transformSchemaFn(dataSchema); + serviceDocs.schemas[dataListSchemeName] = { + type: 'array', + items: { $ref: `#/components/schemas/${dataSchemeName}` } + }; serviceDocs.refs.createRequest = dataSchemeName; + serviceDocs.refs.createMultiRequest = { refs: [dataSchemeName, dataListSchemeName], type: 'oneOf' }; serviceDocs.refs.updateRequest = dataSchemeName; } @@ -218,8 +203,16 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc if (patchSchema) { const patchSchemaName = patchSchema.$id || `${baseSchemeName}PatchData`; + const patchListSchemeName = `${patchSchemaName}List`; + serviceDocs.schemas[patchSchemaName] = transformSchemaFn(patchSchema); + serviceDocs.schemas[patchListSchemeName] = { + type: 'array', + items: { $ref: `#/components/schemas/${patchSchemaName}` } + }; + serviceDocs.refs.patchRequest = patchSchemaName; + serviceDocs.refs.patchMultiRequest = patchListSchemeName; } if (!docs || !docs.model) { @@ -235,9 +228,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc const schemaName = schema.$id; if (schemaName) { serviceDocs.schemas[schemaName] = transformSchemaFn(schema); - if (allowedDefaultRefs.includes(key)) { - serviceDocs.refs[key] = schemaName; - } + serviceDocs.refs[key] = schemaName; } }); diff --git a/lib/v3/generator.js b/lib/v3/generator.js index 278a6a0..97537c2 100644 --- a/lib/v3/generator.js +++ b/lib/v3/generator.js @@ -104,7 +104,7 @@ class OpenApiV3Generator extends AbstractApiGenerator { getResponse: model, createRequest: model, createResponse: model, - createMultiRequest: { refs: [model, modelList], type: 'oneOf' }, + createMultiRequest: { refs: [`${model}Data`, modelList], type: 'oneOf' }, createMultiResponse: { refs: [model, modelList], type: 'oneOf' }, updateRequest: model, updateResponse: model, @@ -219,7 +219,7 @@ class OpenApiV3Generator extends AbstractApiGenerator { const multi = multiOperations.includes('create'); return { tags, - description: 'Creates a new resource with data.', + description: multi ? 'Creates one or multiple resources.' : 'Creates a new resource with data.', requestBody: { required: true, content: multi ? jsonSchemaRef(refs.createMultiRequest) : jsonSchemaRef(refs.createRequest) @@ -266,6 +266,30 @@ class OpenApiV3Generator extends AbstractApiGenerator { security: utils.security('update', securities, security) }; }, + createMulti ({ tags, security, securities, refs }) { + return { + tags, + description: 'Creates one or multiple resources.', + parameters: [], + requestBody: { + required: true, + content: jsonSchemaRef(refs.createMultiRequest) + }, + responses: { + 200: { + description: 'success', + content: jsonSchemaRef(refs.createMultiResponse) + }, + 401: { + description: 'not authenticated' + }, + 500: { + description: 'general error' + } + }, + security: utils.security('create', securities, security) + }; + }, updateMulti ({ tags, security, securities, refs }) { return { tags, @@ -468,6 +492,7 @@ class OpenApiV3Generator extends AbstractApiGenerator { this.specs.components.schemas[pagination] = { title: `${modelName} pagination result`, type: 'object', + required: true, properties: { total: { type: 'integer' }, limit: { type: 'integer' }, diff --git a/test/v3/expected-memory-spec-multi-only.json b/test/v3/expected-memory-spec-multi-only.json index c7bd6b4..4f8781d 100644 --- a/test/v3/expected-memory-spec-multi-only.json +++ b/test/v3/expected-memory-spec-multi-only.json @@ -6,7 +6,7 @@ "paths": { "/message": { "post": { - "description": "Creates a new resource with data.", + "description": "Creates one or multiple resources.", "parameters": [], "requestBody": { "content": { diff --git a/test/v3/expected-memory-spec-pagination-find.json b/test/v3/expected-memory-spec-pagination-find.json index 5ea73b2..49304ae 100644 --- a/test/v3/expected-memory-spec-pagination-find.json +++ b/test/v3/expected-memory-spec-pagination-find.json @@ -17,6 +17,7 @@ "type": "array" }, "messagePagination": { + "required": true, "title": "message pagination result", "type": "object", "properties": {