From a4d386a00a69481e13fbbdceabc8d37c9a507e61 Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Fri, 29 Apr 2022 10:05:29 +0200 Subject: [PATCH] fix localizations request schema --- .../helpers/build-api-endpoint-path.js | 4 +++- .../services/helpers/build-component-schema.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/plugins/documentation/server/services/helpers/build-api-endpoint-path.js b/packages/plugins/documentation/server/services/helpers/build-api-endpoint-path.js index 8b232e6bc0..4e8e6cfb86 100644 --- a/packages/plugins/documentation/server/services/helpers/build-api-endpoint-path.js +++ b/packages/plugins/documentation/server/services/helpers/build-api-endpoint-path.js @@ -93,6 +93,7 @@ const getPaths = ({ routeInfo, uniqueName, contentTypeInfo }) => { const paths = contentTypeRoutes.reduce((acc, route) => { // TODO: Find a more reliable way to determine list of entities vs a single entity const isListOfEntities = hasFindMethod(route.handler); + const isLocalizationPath = route.path.includes('localizations'); const methodVerb = route.method.toLowerCase(); const hasPathParams = route.path.includes('/:'); const pathWithPrefix = getPathWithPrefix(routeInfo.prefix, route); @@ -116,12 +117,13 @@ const getPaths = ({ routeInfo, uniqueName, contentTypeInfo }) => { } if (['post', 'put'].includes(methodVerb)) { + const refName = isLocalizationPath ? 'LocalizationRequest' : 'Request'; const requestBody = { required: true, content: { 'application/json': { schema: { - $ref: `#/components/schemas/${pascalCase(uniqueName)}Request`, + $ref: `#/components/schemas/${pascalCase(uniqueName)}${refName}`, }, }, }, diff --git a/packages/plugins/documentation/server/services/helpers/build-component-schema.js b/packages/plugins/documentation/server/services/helpers/build-component-schema.js index c9487404bb..49a332b04a 100644 --- a/packages/plugins/documentation/server/services/helpers/build-component-schema.js +++ b/packages/plugins/documentation/server/services/helpers/build-component-schema.js @@ -20,6 +20,9 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => { let schemas = {}; // Get all the route methods const routeMethods = routeInfo.routes.map((route) => route.method); + const hasLocalizationPath = routeInfo.routes.filter((route) => + route.path.includes('localizations') + ).length; // When the route methods contain any post or put requests if (routeMethods.includes('POST') || routeMethods.includes('PUT')) { @@ -34,6 +37,21 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => { ? Object.assign({}, ...requiredAttributes) : attributes; + if (hasLocalizationPath) { + const localizationsRequestAttributes = { + ...requestAttributes, + locale: { type: 'string' }, + }; + + schemas = { + ...schemas, + [`${pascalCase(uniqueName)}LocalizationRequest`]: { + type: 'object', + properties: cleanSchemaAttributes(localizationsRequestAttributes, { isRequest: true }), + }, + }; + } + // Build the request schema schemas = { ...schemas,