Adjusted Review changes

This commit is contained in:
Andrei Wilkens 2022-06-30 13:01:19 +02:00
parent 9e7f3e4c5f
commit 9b57d63af4
2 changed files with 84 additions and 75 deletions

View File

@ -21,15 +21,15 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
let schemas = {};
let componentSchemas = {};
const addSchema = (schemaName, schema) => {
if(!Object.keys(schema) || !Object.keys(schema.properties)) {
if (!Object.keys(schema) || !Object.keys(schema.properties)) {
return false;
}
componentSchemas = {
...componentSchemas,
[schemaName]: schema
}
[schemaName]: schema,
};
return true;
}
};
// Get all the route methods
const routeMethods = routeInfo.routes.map(route => route.method);
// Check for localized paths
@ -105,26 +105,37 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
// Build the list response schema
schemas = {
...schemas,
[`${pascalCase(uniqueName)}ListResponseDataItem`]:{
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes, { addSchema, dataTypeName: `#/components/schemas/${pascalCase(uniqueName)}ListResponseDataItemLocalized`}) },
[`${pascalCase(uniqueName)}ListResponseDataItem`]: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: {
type: 'object',
properties: cleanSchemaAttributes(attributes, {
addSchema,
componentSchemaRefName: `#/components/schemas/${pascalCase(
uniqueName
)}ListResponseDataItemLocalized`,
}),
},
},
[`${pascalCase(uniqueName)}ListResponseDataItemLocalized`]: {
},
[`${pascalCase(uniqueName)}ListResponseDataItemLocalized`]: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes, { addSchema }) },
},
properties: cleanSchemaAttributes(attributes, { addSchema }),
},
},
},
[`${pascalCase(uniqueName)}ListResponse`]: {
properties: {
data: {
type: 'array',
items: {
"$ref": `#/components/schemas/${pascalCase(uniqueName)}ListResponseDataItem`
$ref: `#/components/schemas/${pascalCase(uniqueName)}ListResponseDataItem`,
},
},
meta: {
@ -149,29 +160,40 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
schemas = {
...schemas,
[`${pascalCase(uniqueName)}ResponseDataObject`]: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes, { addSchema, dataTypeName: `#/components/schemas/${pascalCase(uniqueName)}ResponseDataObjectLocalized` }) },
},
properties: cleanSchemaAttributes(attributes, {
addSchema,
componentSchemaRefName: `#/components/schemas/${pascalCase(
uniqueName
)}ResponseDataObjectLocalized`,
}),
},
},
},
[`${pascalCase(uniqueName)}ResponseDataObjectLocalized`]: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes, { addSchema }) },
},
properties: cleanSchemaAttributes(attributes, { addSchema }),
},
},
},
[`${pascalCase(uniqueName)}Response`]: {
properties: {
data: {
"$ref": `#/components/schemas/${pascalCase(uniqueName)}ResponseDataObject`
$ref: `#/components/schemas/${pascalCase(uniqueName)}ResponseDataObject`,
},
meta: { type: 'object' },
},
},
};
return {...schemas, ...componentSchemas};
return { ...schemas, ...componentSchemas };
};
const buildComponentSchema = api => {

View File

@ -10,7 +10,10 @@ const pascalCase = require('./pascal-case');
* @param {{ typeMap: Map, isRequest: boolean }} opts
* @returns Attributes using OpenAPI acceptable data types
*/
const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = false, addSchema = () => {}, dataTypeName = "" } = {}, ) => {
const cleanSchemaAttributes = (
attributes,
{ typeMap = new Map(), isRequest = false, addSchema = () => {}, componentSchemaRefName = '' } = {}
) => {
const attributesCopy = _.cloneDeep(attributes);
for (const prop in attributesCopy) {
@ -86,67 +89,49 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
}
case 'component': {
const componentAttributes = strapi.components[attribute.component].attributes;
const componentExists = addSchema(pascalCase(attribute.component),{
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, {
typeMap,
isRequest,
}),
},
})
const rawComponentSchema = {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, {
typeMap,
isRequest,
}),
},
};
const refComponentSchema = {
$ref: `#/components/schemas/${`${pascalCase(attribute.component)}Component`}`,
};
const componentExists = addSchema(
`${pascalCase(attribute.component)}Component`,
rawComponentSchema
);
const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema;
if (attribute.repeatable) {
attributesCopy[prop] = {
type: 'array',
items: componentExists ?
{"$ref": `#/components/schemas/${pascalCase(attribute.component)}`}
: {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, {
typeMap,
isRequest,
}),
},
}
};
items: finalComponentSchema,
};
} else {
attributesCopy[prop] = componentExists ?
{"$ref": `#/components/schemas/${pascalCase(attribute.component)}`}
: {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, {
typeMap,
isRequest,
}),
},
};
attributesCopy[prop] = finalComponentSchema;
}
break;
}
case 'dynamiczone': {
const components = attribute.components.map(component => {
const componentAttributes = strapi.components[component].attributes;
const componentExists = addSchema(pascalCase(component),{
const rawComponentSchema = {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
__component: { type: 'string' },
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest, addSchema }),
},
})
return componentExists ? {"$ref": `#/components/schemas/${pascalCase(component)}`} : {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
__component: { type: 'string' },
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest, addSchema }),
},
}
};
const refComponentSchema = { $ref: `#/components/schemas/${pascalCase(component)}` };
const componentExists = addSchema(pascalCase(component), rawComponentSchema);
const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema;
return finalComponentSchema;
});
attributesCopy[prop] = {
@ -196,10 +181,12 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa
if (prop === 'localizations') {
attributesCopy[prop] = {
type: 'object',
properties: { data: {
type: 'array',
items: dataTypeName.length ? { '$ref' : dataTypeName } : {}
} },
properties: {
data: {
type: 'array',
items: componentSchemaRefName.length ? { $ref: componentSchemaRefName } : {},
},
},
};
break;
}