From 514a6cdee79875068f0a927d078c56671918a4c2 Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Sun, 20 Aug 2023 14:00:48 -0700 Subject: [PATCH 1/5] Include required in pagination result --- lib/v3/generator.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/v3/generator.js b/lib/v3/generator.js index 278a6a0..c357fec 100644 --- a/lib/v3/generator.js +++ b/lib/v3/generator.js @@ -468,6 +468,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' }, From 236bcffa12d568c795ecb4b62075e328d023f9ee Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Mon, 25 Dec 2023 05:41:46 -0800 Subject: [PATCH 2/5] fix: multi create type --- lib/openapi.js | 4 +++- lib/utils.js | 2 +- lib/v3/generator.js | 26 +++++++++++++++++++++++++- types/index.d.ts | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/openapi.js b/lib/openapi.js index 4b813e6..3659e7c 100644 --- a/lib/openapi.js +++ b/lib/openapi.js @@ -12,6 +12,7 @@ const defaultMethods = [ { operation: 'update', method: 'update', id: true, httpMethod: 'put', multi: false }, { operation: 'patch', method: 'patch', id: true, httpMethod: 'patch', multi: false }, { operation: 'remove', method: 'remove', id: true, httpMethod: 'delete', multi: false }, + { operation: 'createMulti', method: 'create', id: false, httpMethod: 'post', multi: true }, { operation: 'updateMulti', method: 'update', id: false, httpMethod: 'put', multi: true }, { operation: 'patchMulti', method: 'patch', id: false, httpMethod: 'patch', multi: true }, { operation: 'removeMulti', method: 'remove', id: false, httpMethod: 'delete', multi: true } @@ -48,7 +49,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 ? ['remove', 'update', 'patch', 'create'] : service.options.multi + return operations.filter((operation) => _.isFunction(service[operation])); } return []; diff --git a/lib/utils.js b/lib/utils.js index a8625cb..c4b8286 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -193,7 +193,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc [baseSchemeName]: transformSchemaFn(resultSchema), [listSchemaName]: { type: 'array', - items: { $ref: `#/components/schemas/${baseSchemeName}` } + items: { $ref: `#/components/schemas/${baseSchemeName}Data` } } }; diff --git a/lib/v3/generator.js b/lib/v3/generator.js index c357fec..74f73c9 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, @@ -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, diff --git a/types/index.d.ts b/types/index.d.ts index f581515..0a989fb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -150,6 +150,7 @@ declare namespace feathersSwagger { update?: FnOperationSpecsGenerator; patch?: FnOperationSpecsGenerator; remove?: FnOperationSpecsGenerator; + createMulti?: FnOperationSpecsGenerator; updateMulti?: FnOperationSpecsGenerator; patchMulti?: FnOperationSpecsGenerator; removeMulti?: FnOperationSpecsGenerator; @@ -162,6 +163,7 @@ declare namespace feathersSwagger { update?: OperationConfig; patch?: OperationConfig; remove?: OperationConfig; + createMulti?: OperationConfig; updateMulti?: OperationConfig; patchMulti?: OperationConfig; removeMulti?: OperationConfig; From 97d4b961b573dfee3988d75127dfd26b0093b9f9 Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Tue, 26 Dec 2023 18:16:43 -0800 Subject: [PATCH 3/5] fix: multi create type fix: multi patch --- lib/openapi.js | 3 +-- lib/utils.js | 15 +++++++++++++++ lib/v3/generator.js | 2 +- test/v3/expected-memory-spec-multi-only.json | 2 +- test/v3/expected-memory-spec-pagination-find.json | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/openapi.js b/lib/openapi.js index 3659e7c..2579ba7 100644 --- a/lib/openapi.js +++ b/lib/openapi.js @@ -12,7 +12,6 @@ const defaultMethods = [ { operation: 'update', method: 'update', id: true, httpMethod: 'put', multi: false }, { operation: 'patch', method: 'patch', id: true, httpMethod: 'patch', multi: false }, { operation: 'remove', method: 'remove', id: true, httpMethod: 'delete', multi: false }, - { operation: 'createMulti', method: 'create', id: false, httpMethod: 'post', multi: true }, { operation: 'updateMulti', method: 'update', id: false, httpMethod: 'put', multi: true }, { operation: 'patchMulti', method: 'patch', id: false, httpMethod: 'patch', multi: true }, { operation: 'removeMulti', method: 'remove', id: false, httpMethod: 'delete', multi: true } @@ -49,7 +48,7 @@ function ignoreService (service, path, includeConfig, ignoreConfig) { function determineMultiOperations (service) { if (service.options && service.options.multi) { - const operations = service.options.multi === true ? ['remove', 'update', 'patch', 'create'] : service.options.multi + const operations = service.options.multi === true ? serviceMethods : service.options.multi; return operations.filter((operation) => _.isFunction(service[operation])); } diff --git a/lib/utils.js b/lib/utils.js index c4b8286..8530667 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -201,8 +201,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 +225,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) { diff --git a/lib/v3/generator.js b/lib/v3/generator.js index 74f73c9..97537c2 100644 --- a/lib/v3/generator.js +++ b/lib/v3/generator.js @@ -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) 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": { From 1b99fe20de38313e2b7b6b62cae8919e177df0cd Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Tue, 26 Dec 2023 18:23:07 -0800 Subject: [PATCH 4/5] revert: listSchemaName --- lib/utils.js | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 8530667..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', @@ -193,7 +171,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc [baseSchemeName]: transformSchemaFn(resultSchema), [listSchemaName]: { type: 'array', - items: { $ref: `#/components/schemas/${baseSchemeName}Data` } + items: { $ref: `#/components/schemas/${baseSchemeName}` } } }; @@ -250,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; } }); From e23f79094280571be800d029d4c2a3e41fe88466 Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Tue, 26 Dec 2023 18:39:46 -0800 Subject: [PATCH 5/5] revert: index.d.ts --- types/index.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 0a989fb..f581515 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -150,7 +150,6 @@ declare namespace feathersSwagger { update?: FnOperationSpecsGenerator; patch?: FnOperationSpecsGenerator; remove?: FnOperationSpecsGenerator; - createMulti?: FnOperationSpecsGenerator; updateMulti?: FnOperationSpecsGenerator; patchMulti?: FnOperationSpecsGenerator; removeMulti?: FnOperationSpecsGenerator; @@ -163,7 +162,6 @@ declare namespace feathersSwagger { update?: OperationConfig; patch?: OperationConfig; remove?: OperationConfig; - createMulti?: OperationConfig; updateMulti?: OperationConfig; patchMulti?: OperationConfig; removeMulti?: OperationConfig;