mirror of
https://github.com/strapi/strapi.git
synced 2025-08-19 14:19:03 +00:00
Merge pull request #13458 from meganoob1337/strapi/fix/issue-13453-documentation-fix
fix #13453 Documentation Plugin doesn't generate Proper Type for the …
This commit is contained in:
commit
f5a62af92b
@ -20,6 +20,18 @@ const { hasFindMethod, isLocalizedPath } = require('./utils/routes');
|
|||||||
const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
||||||
// Store response and request schemas in an object
|
// Store response and request schemas in an object
|
||||||
let schemas = {};
|
let schemas = {};
|
||||||
|
let componentSchemas = {};
|
||||||
|
// adds a ComponentSchema to the Schemas so it can be used as Ref
|
||||||
|
const addComponentSchema = (schemaName, schema) => {
|
||||||
|
if (!Object.keys(schema) || !Object.keys(schema.properties)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
componentSchemas = {
|
||||||
|
...componentSchemas,
|
||||||
|
[schemaName]: schema,
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
};
|
||||||
// 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
|
// Check for localized paths
|
||||||
@ -56,7 +68,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
|
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
|
||||||
required: [...requiredAttributes, 'locale'],
|
required: [...requiredAttributes, 'locale'],
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
|
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addComponentSchema }),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -71,7 +83,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
data: {
|
data: {
|
||||||
required: requiredAttributes,
|
required: requiredAttributes,
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
|
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addComponentSchema }),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -85,7 +97,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: 'string' },
|
id: { type: 'string' },
|
||||||
...cleanSchemaAttributes(attributes),
|
...cleanSchemaAttributes(attributes, { addComponentSchema }),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -97,17 +109,37 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
// Build the list response schema
|
// Build the list response schema
|
||||||
schemas = {
|
schemas = {
|
||||||
...schemas,
|
...schemas,
|
||||||
[`${pascalCase(uniqueName)}ListResponse`]: {
|
[`${pascalCase(uniqueName)}ListResponseDataItem`]: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'string' },
|
||||||
|
attributes: {
|
||||||
|
type: 'object',
|
||||||
|
properties: cleanSchemaAttributes(attributes, {
|
||||||
|
addComponentSchema,
|
||||||
|
componentSchemaRefName: `#/components/schemas/${pascalCase(
|
||||||
|
uniqueName
|
||||||
|
)}ListResponseDataItemLocalized`,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[`${pascalCase(uniqueName)}ListResponseDataItemLocalized`]: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'string' },
|
||||||
|
attributes: {
|
||||||
|
type: 'object',
|
||||||
|
properties: cleanSchemaAttributes(attributes, { addComponentSchema }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[`${pascalCase(uniqueName)}ListResponse`]: {
|
||||||
properties: {
|
properties: {
|
||||||
data: {
|
data: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
items: {
|
items: {
|
||||||
type: 'object',
|
$ref: `#/components/schemas/${pascalCase(uniqueName)}ListResponseDataItem`,
|
||||||
properties: {
|
|
||||||
id: { type: 'string' },
|
|
||||||
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes) },
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
@ -131,22 +163,41 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
// Build the response schema
|
// Build the response schema
|
||||||
schemas = {
|
schemas = {
|
||||||
...schemas,
|
...schemas,
|
||||||
[`${pascalCase(uniqueName)}Response`]: {
|
[`${pascalCase(uniqueName)}ResponseDataObject`]: {
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
data: {
|
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: 'string' },
|
id: { type: 'string' },
|
||||||
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes) },
|
attributes: {
|
||||||
|
type: 'object',
|
||||||
|
properties: cleanSchemaAttributes(attributes, {
|
||||||
|
addComponentSchema,
|
||||||
|
componentSchemaRefName: `#/components/schemas/${pascalCase(
|
||||||
|
uniqueName
|
||||||
|
)}ResponseDataObjectLocalized`,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
[`${pascalCase(uniqueName)}ResponseDataObjectLocalized`]: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'string' },
|
||||||
|
attributes: {
|
||||||
|
type: 'object',
|
||||||
|
properties: cleanSchemaAttributes(attributes, { addComponentSchema }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[`${pascalCase(uniqueName)}Response`]: {
|
||||||
|
properties: {
|
||||||
|
data: {
|
||||||
|
$ref: `#/components/schemas/${pascalCase(uniqueName)}ResponseDataObject`,
|
||||||
|
},
|
||||||
meta: { type: 'object' },
|
meta: { type: 'object' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
return { ...schemas, ...componentSchemas };
|
||||||
return schemas;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildComponentSchema = (api) => {
|
const buildComponentSchema = (api) => {
|
||||||
|
@ -2,15 +2,18 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const getSchemaData = require('./get-schema-data');
|
const getSchemaData = require('./get-schema-data');
|
||||||
|
const pascalCase = require('./pascal-case');
|
||||||
/**
|
/**
|
||||||
* @description - Converts types found on attributes to OpenAPI acceptable data types
|
* @description - Converts types found on attributes to OpenAPI acceptable data types
|
||||||
*
|
*
|
||||||
* @param {object} attributes - The attributes found on a contentType
|
* @param {object} attributes - The attributes found on a contentType
|
||||||
* @param {{ typeMap: Map, isRequest: boolean }} opts
|
* @param {{ typeMap: Map, isRequest: boolean, addComponentSchema: function, componentSchemaRefName: string }} opts
|
||||||
* @returns Attributes using OpenAPI acceptable data types
|
* @returns Attributes using OpenAPI acceptable data types
|
||||||
*/
|
*/
|
||||||
const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = false } = {}) => {
|
const cleanSchemaAttributes = (
|
||||||
|
attributes,
|
||||||
|
{ typeMap = new Map(), isRequest = false, addComponentSchema = () => {}, componentSchemaRefName = '' } = {}
|
||||||
|
) => {
|
||||||
const attributesCopy = _.cloneDeep(attributes);
|
const attributesCopy = _.cloneDeep(attributes);
|
||||||
|
|
||||||
for (const prop of Object.keys(attributesCopy)) {
|
for (const prop of Object.keys(attributesCopy)) {
|
||||||
@ -86,20 +89,7 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
|
|||||||
}
|
}
|
||||||
case 'component': {
|
case 'component': {
|
||||||
const componentAttributes = strapi.components[attribute.component].attributes;
|
const componentAttributes = strapi.components[attribute.component].attributes;
|
||||||
|
const rawComponentSchema = {
|
||||||
if (attribute.repeatable) {
|
|
||||||
attributesCopy[prop] = {
|
|
||||||
type: 'array',
|
|
||||||
items: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
...(isRequest ? {} : { id: { type: 'string' } }),
|
|
||||||
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
attributesCopy[prop] = {
|
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
...(isRequest ? {} : { id: { type: 'string' } }),
|
...(isRequest ? {} : { id: { type: 'string' } }),
|
||||||
@ -109,20 +99,39 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const refComponentSchema = {
|
||||||
|
$ref: `#/components/schemas/${pascalCase(attribute.component)}Component`,
|
||||||
|
};
|
||||||
|
const componentExists = addComponentSchema(
|
||||||
|
`${pascalCase(attribute.component)}Component`,
|
||||||
|
rawComponentSchema
|
||||||
|
);
|
||||||
|
const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema;
|
||||||
|
if (attribute.repeatable) {
|
||||||
|
attributesCopy[prop] = {
|
||||||
|
type: 'array',
|
||||||
|
items: finalComponentSchema,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
attributesCopy[prop] = finalComponentSchema;
|
||||||
}
|
}
|
||||||
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 {
|
const rawComponentSchema = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
...(isRequest ? {} : { id: { type: 'string' } }),
|
...(isRequest ? {} : { id: { type: 'string' } }),
|
||||||
__component: { type: 'string' },
|
__component: { type: 'string' },
|
||||||
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest }),
|
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest, addComponentSchema }),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const refComponentSchema = { $ref: `#/components/schemas/${pascalCase(component)}` };
|
||||||
|
const componentExists = addComponentSchema(pascalCase(component), rawComponentSchema);
|
||||||
|
const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema;
|
||||||
|
return finalComponentSchema;
|
||||||
});
|
});
|
||||||
|
|
||||||
attributesCopy[prop] = {
|
attributesCopy[prop] = {
|
||||||
@ -171,8 +180,13 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
|
|||||||
|
|
||||||
if (prop === 'localizations') {
|
if (prop === 'localizations') {
|
||||||
attributesCopy[prop] = {
|
attributesCopy[prop] = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
data: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
items: { type: 'object', properties: {} },
|
items: componentSchemaRefName.length ? { $ref: componentSchemaRefName } : {},
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user