Skip to content

Commit

Permalink
Api docs remove Relations from Post & Patch (twentyhq#5817)
Browse files Browse the repository at this point in the history
* Remove relations where they cannot be used
* Removed duplicated schema for findMany
* Reuse schema for Relation variant to reduce size of sent json object

closes twentyhq#5778

---------

Co-authored-by: Félix Malfait <[email protected]>
Co-authored-by: martmull <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent 58fb86f commit 4994a9c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ describe('computeSchemaComponents', () => {
type: 'string',
enum: ['OPTION_1', 'OPTION_2'],
},
fieldRelation: {
type: 'array',
items: {
$ref: '#/components/schemas/ToObjectMetadataName',
},
},
fieldPosition: {
type: 'number',
},
Expand Down Expand Up @@ -145,16 +139,28 @@ describe('computeSchemaComponents', () => {
},
},
},
ObjectsName: {
description: 'A list of objectsName',
'ObjectName with Relations': {
allOf: [
{
$ref: '#/components/schemas/ObjectName',
},
{
properties: {
fieldRelation: {
type: 'array',
items: {
$ref: '#/components/schemas/ToObjectMetadataName',
},
},
},
type: 'object',
},
],
description: undefined,
example: {
fieldNumber: '',
},
items: {
$ref: '#/components/schemas/ObjectName',
},
required: ['fieldNumber'],
type: 'array',
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const getSchemaComponentsProperties = (
item: ObjectMetadataEntity,
): Properties => {
return item.fields.reduce((node, field) => {
if (field.type == FieldMetadataType.RELATION) {
return node;
}

let itemProperty = {} as Property;

switch (field.type) {
Expand All @@ -61,18 +65,6 @@ const getSchemaComponentsProperties = (
enum: field.options.map((option: { value: string }) => option.value),
};
break;
case FieldMetadataType.RELATION:
if (field.fromRelationMetadata?.toObjectMetadata.nameSingular) {
itemProperty = {
type: 'array',
items: {
$ref: `#/components/schemas/${capitalize(
field.fromRelationMetadata?.toObjectMetadata.nameSingular || '',
)}`,
},
};
}
break;
case FieldMetadataType.LINK:
case FieldMetadataType.LINKS:
case FieldMetadataType.CURRENCY:
Expand Down Expand Up @@ -106,6 +98,37 @@ const getSchemaComponentsProperties = (
}, {} as Properties);
};

const getSchemaComponentsRelationProperties = (
item: ObjectMetadataEntity,
): Properties => {
return item.fields.reduce((node, field) => {
let itemProperty = {} as Property;

if (field.type == FieldMetadataType.RELATION) {
if (field.fromRelationMetadata?.toObjectMetadata.nameSingular) {
itemProperty = {
type: 'array',
items: {
$ref: `#/components/schemas/${capitalize(
field.fromRelationMetadata?.toObjectMetadata.nameSingular || '',
)}`,
},
};
}
}

if (field.description) {
itemProperty.description = field.description;
}

if (Object.keys(itemProperty).length) {
node[field.name] = itemProperty;
}

return node;
}, {} as Properties);
};

const getRequiredFields = (item: ObjectMetadataEntity): string[] => {
return item.fields.reduce((required, field) => {
if (!field.isNullable && field.defaultValue === null) {
Expand All @@ -118,14 +141,14 @@ const getRequiredFields = (item: ObjectMetadataEntity): string[] => {
}, [] as string[]);
};

const computeBatchSchemaComponent = (
const computeSchemaComponent = (
item: ObjectMetadataEntity,
): OpenAPIV3_1.SchemaObject => {
const result = {
type: 'array',
description: `A list of ${item.namePlural}`,
items: { $ref: `#/components/schemas/${capitalize(item.nameSingular)}` },
example: [{}],
type: 'object',
description: item.description,
properties: getSchemaComponentsProperties(item),
example: {},
} as OpenAPIV3_1.SchemaObject;

const requiredFields = getRequiredFields(item);
Expand All @@ -145,13 +168,20 @@ const computeBatchSchemaComponent = (
return result;
};

const computeSchemaComponent = (
const computeRelationSchemaComponent = (
item: ObjectMetadataEntity,
): OpenAPIV3_1.SchemaObject => {
const result = {
type: 'object',
description: item.description,
properties: getSchemaComponentsProperties(item),
allOf: [
{
$ref: `#/components/schemas/${capitalize(item.nameSingular)}`,
},
{
type: 'object',
properties: getSchemaComponentsRelationProperties(item),
},
],
example: {},
} as OpenAPIV3_1.SchemaObject;

Expand All @@ -178,7 +208,8 @@ export const computeSchemaComponents = (
return objectMetadataItems.reduce(
(schemas, item) => {
schemas[capitalize(item.nameSingular)] = computeSchemaComponent(item);
schemas[capitalize(item.namePlural)] = computeBatchSchemaComponent(item);
schemas[capitalize(item.nameSingular) + ' with Relations'] =
computeRelationSchemaComponent(item);

return schemas;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
getFindOneResponse200,
getUpdateOneResponse200,
} from 'src/engine/core-modules/open-api/utils/responses.utils';
import { getRequestBody } from 'src/engine/core-modules/open-api/utils/request-body.utils';
import {
getArrayRequestBody,
getRequestBody,
} from 'src/engine/core-modules/open-api/utils/request-body.utils';

export const computeBatchPath = (
item: ObjectMetadataEntity,
Expand All @@ -22,7 +25,7 @@ export const computeBatchPath = (
summary: `Create Many ${item.namePlural}`,
operationId: `createMany${capitalize(item.namePlural)}`,
parameters: [{ $ref: '#/components/parameters/depth' }],
requestBody: getRequestBody(capitalize(item.namePlural)),
requestBody: getArrayRequestBody(capitalize(item.nameSingular)),
responses: {
'201': getCreateManyResponse201(item),
'400': { $ref: '#/components/responses/400' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ export const getRequestBody = (name: string) => {
},
};
};

export const getArrayRequestBody = (name: string) => {
return {
required: true,
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: `#/components/schemas/${name}`,
},
},
},
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const getFindManyResponse200 = (
items: {
$ref: `#/components/schemas/${capitalize(
item.nameSingular,
)}`,
)} with Relations`,
},
},
},
Expand Down Expand Up @@ -54,7 +54,9 @@ export const getFindOneResponse200 = (
type: 'object',
properties: {
[item.nameSingular]: {
$ref: `#/components/schemas/${capitalize(item.nameSingular)}`,
$ref: `#/components/schemas/${capitalize(
item.nameSingular,
)} with Relations`,
},
},
},
Expand Down

0 comments on commit 4994a9c

Please sign in to comment.