mirror of
https://github.com/strapi/strapi.git
synced 2025-10-12 08:36:40 +00:00
fix localization response
This commit is contained in:
parent
f3b295f823
commit
2c60df54cd
@ -7,7 +7,7 @@ const pascalCase = require('./utils/pascal-case');
|
|||||||
const queryParams = require('./utils/query-params');
|
const queryParams = require('./utils/query-params');
|
||||||
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
||||||
const getApiResponses = require('./utils/get-api-responses');
|
const getApiResponses = require('./utils/get-api-responses');
|
||||||
const { hasFindMethod } = require('./utils/routes');
|
const { hasFindMethod, isLocalizedPath } = require('./utils/routes');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Parses a route with ':variable'
|
* @description Parses a route with ':variable'
|
||||||
@ -93,12 +93,17 @@ const getPaths = ({ routeInfo, uniqueName, contentTypeInfo }) => {
|
|||||||
const paths = contentTypeRoutes.reduce((acc, route) => {
|
const paths = contentTypeRoutes.reduce((acc, route) => {
|
||||||
// TODO: Find a more reliable way to determine list of entities vs a single entity
|
// TODO: Find a more reliable way to determine list of entities vs a single entity
|
||||||
const isListOfEntities = hasFindMethod(route.handler);
|
const isListOfEntities = hasFindMethod(route.handler);
|
||||||
const isLocalizationPath = route.path.includes('localizations');
|
const isLocalizationPath = isLocalizedPath(route.path);
|
||||||
const methodVerb = route.method.toLowerCase();
|
const methodVerb = route.method.toLowerCase();
|
||||||
const hasPathParams = route.path.includes('/:');
|
const hasPathParams = route.path.includes('/:');
|
||||||
const pathWithPrefix = getPathWithPrefix(routeInfo.prefix, route);
|
const pathWithPrefix = getPathWithPrefix(routeInfo.prefix, route);
|
||||||
const routePath = hasPathParams ? parsePathWithVariables(pathWithPrefix) : pathWithPrefix;
|
const routePath = hasPathParams ? parsePathWithVariables(pathWithPrefix) : pathWithPrefix;
|
||||||
const { responses } = getApiResponses(uniqueName, route, isListOfEntities);
|
const { responses } = getApiResponses({
|
||||||
|
uniqueName,
|
||||||
|
route,
|
||||||
|
isListOfEntities,
|
||||||
|
isLocalizationPath,
|
||||||
|
});
|
||||||
|
|
||||||
const swaggerConfig = {
|
const swaggerConfig = {
|
||||||
responses,
|
responses,
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const cleanSchemaAttributes = require('./utils/clean-schema-attributes');
|
const cleanSchemaAttributes = require('./utils/clean-schema-attributes');
|
||||||
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
||||||
const pascalCase = require('./utils/pascal-case');
|
const pascalCase = require('./utils/pascal-case');
|
||||||
const { hasFindMethod } = require('./utils/routes');
|
const { hasFindMethod, isLocalizedPath } = require('./utils/routes');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @decription Get all open api schema objects for a given content type
|
* @decription Get all open api schema objects for a given content type
|
||||||
@ -20,13 +21,24 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
let schemas = {};
|
let schemas = {};
|
||||||
// Get all the route methods
|
// Get all the route methods
|
||||||
const routeMethods = routeInfo.routes.map((route) => route.method);
|
const routeMethods = routeInfo.routes.map((route) => route.method);
|
||||||
|
// Check for localized paths
|
||||||
const hasLocalizationPath = routeInfo.routes.filter((route) =>
|
const hasLocalizationPath = routeInfo.routes.filter((route) =>
|
||||||
route.path.includes('localizations')
|
isLocalizedPath(route.path)
|
||||||
).length;
|
).length;
|
||||||
|
|
||||||
// When the route methods contain any post or put requests
|
// When the route methods contain any post or put requests
|
||||||
if (routeMethods.includes('POST') || routeMethods.includes('PUT')) {
|
if (routeMethods.includes('POST') || routeMethods.includes('PUT')) {
|
||||||
const requiredAttributes = Object.entries(attributes)
|
const attributesToOmit = [
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
'publishedAt',
|
||||||
|
'publishedBy',
|
||||||
|
'updatedBy',
|
||||||
|
'createdBy',
|
||||||
|
'localizations',
|
||||||
|
];
|
||||||
|
const attributesForRequest = _.omit(attributes, attributesToOmit);
|
||||||
|
|
||||||
|
const requiredAttributes = Object.entries(attributesForRequest)
|
||||||
.filter(([, attribute]) => attribute.required)
|
.filter(([, attribute]) => attribute.required)
|
||||||
.map(([attributeName, attribute]) => {
|
.map(([attributeName, attribute]) => {
|
||||||
return { [attributeName]: attribute };
|
return { [attributeName]: attribute };
|
||||||
@ -35,19 +47,14 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
const requestAttributes =
|
const requestAttributes =
|
||||||
routeMethods.includes('POST') && requiredAttributes.length
|
routeMethods.includes('POST') && requiredAttributes.length
|
||||||
? Object.assign({}, ...requiredAttributes)
|
? Object.assign({}, ...requiredAttributes)
|
||||||
: attributes;
|
: attributesForRequest;
|
||||||
|
|
||||||
if (hasLocalizationPath) {
|
if (hasLocalizationPath) {
|
||||||
const localizationsRequestAttributes = {
|
|
||||||
...requestAttributes,
|
|
||||||
locale: { type: 'string' },
|
|
||||||
};
|
|
||||||
|
|
||||||
schemas = {
|
schemas = {
|
||||||
...schemas,
|
...schemas,
|
||||||
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
|
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: cleanSchemaAttributes(localizationsRequestAttributes, { isRequest: true }),
|
properties: cleanSchemaAttributes(requestAttributes, { isRequest: true }),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'dynamiczone': {
|
case 'dynamiczone': {
|
||||||
const components = attribute.components.map(component => {
|
const components = attribute.components.map((component) => {
|
||||||
const componentAttributes = strapi.components[component].attributes;
|
const componentAttributes = strapi.components[component].attributes;
|
||||||
return {
|
return {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@ -169,6 +169,14 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prop === 'localizations') {
|
||||||
|
attributesCopy[prop] = {
|
||||||
|
type: 'array',
|
||||||
|
items: { type: 'object', properties: {} },
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (!attribute.target || typeMap.has(attribute.target)) {
|
if (!attribute.target || typeMap.has(attribute.target)) {
|
||||||
attributesCopy[prop] = {
|
attributesCopy[prop] = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
|
@ -11,7 +11,12 @@ const pascalCase = require('./pascal-case');
|
|||||||
*
|
*
|
||||||
* @returns The Swagger responses
|
* @returns The Swagger responses
|
||||||
*/
|
*/
|
||||||
const getApiResponse = (name, route, isListOfEntities = false) => {
|
const getApiResponse = ({
|
||||||
|
uniqueName,
|
||||||
|
route,
|
||||||
|
isListOfEntities = false,
|
||||||
|
isLocalizationPath = false,
|
||||||
|
}) => {
|
||||||
const getSchema = () => {
|
const getSchema = () => {
|
||||||
if (route.method === 'DELETE') {
|
if (route.method === 'DELETE') {
|
||||||
return {
|
return {
|
||||||
@ -20,18 +25,22 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isListOfEntities) {
|
if (isLocalizationPath) {
|
||||||
return { $ref: `#/components/schemas/${pascalCase(name)}ListResponse` };
|
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}LocalizationResponse` };
|
||||||
}
|
}
|
||||||
|
|
||||||
return { $ref: `#/components/schemas/${pascalCase(name)}Response` };
|
if (isListOfEntities) {
|
||||||
|
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}ListResponse` };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}Response` };
|
||||||
};
|
};
|
||||||
|
|
||||||
const schema = getSchema();
|
const schema = getSchema();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responses: {
|
responses: {
|
||||||
'200': {
|
200: {
|
||||||
description: 'OK',
|
description: 'OK',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
@ -39,7 +48,7 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'400': {
|
400: {
|
||||||
description: 'Bad Request',
|
description: 'Bad Request',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
@ -49,7 +58,7 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'401': {
|
401: {
|
||||||
description: 'Unauthorized',
|
description: 'Unauthorized',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
@ -59,7 +68,7 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'403': {
|
403: {
|
||||||
description: 'Forbidden',
|
description: 'Forbidden',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
@ -69,7 +78,7 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'404': {
|
404: {
|
||||||
description: 'Not Found',
|
description: 'Not Found',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
@ -79,7 +88,7 @@ const getApiResponse = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'500': {
|
500: {
|
||||||
description: 'Internal Server Error',
|
description: 'Internal Server Error',
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user