Skip to content

Commit

Permalink
fix: multi create type
Browse files Browse the repository at this point in the history
fix: multi patch
  • Loading branch information
AshotN committed Dec 27, 2023
1 parent 236bcff commit 97d4b96
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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]));
}

Expand Down
15 changes: 15 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/v3/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/v3/expected-memory-spec-multi-only.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"paths": {
"/message": {
"post": {
"description": "Creates a new resource with data.",
"description": "Creates one or multiple resources.",
"parameters": [],
"requestBody": {
"content": {
Expand Down
1 change: 1 addition & 0 deletions test/v3/expected-memory-spec-pagination-find.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"type": "array"
},
"messagePagination": {
"required": true,
"title": "message pagination result",
"type": "object",
"properties": {
Expand Down

0 comments on commit 97d4b96

Please sign in to comment.