Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An/multi create fix #256

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down
41 changes: 16 additions & 25 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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;
}
});

Expand Down
29 changes: 27 additions & 2 deletions lib/v3/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down 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 Expand Up @@ -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,
Expand Down Expand Up @@ -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' },
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
Loading