fix: enforce the user to pass in property with id suffix for morph relations in Rest API (#18335)

REST API allowed users to pass in targetOpportunity, targetPerson,
targetCompany etc when trying to create a noteTarget or a taskTarget.
The request went through, we got back a 201, the record was created, but
the relationship was never established since the FK was empty in the
database.

This PR enforces users to send in the property with the `Id` suffix for
consistency. So, the user sends in targetOpportunityId, targetPersonId,
targetCompanyId etc.

<p align="center">
<img width="854" height="480" alt="image"
src="https://github.com/user-attachments/assets/ed50a623-68d4-4266-baf1-e94a657c3fd4"
/>
</p>

If the users try to send without the "Id" suffix, they get an error
explaining what to do.

<p align="center">
<img width="854" height="480" alt="image"
src="https://github.com/user-attachments/assets/8bab399b-7a04-4b6a-86b5-6f0e0b1ecd5d"
/>
</p>

Additionally, the documentation itself contains the correct property
names.

<p align="center">
<img width="854" height="480" alt="image"
src="https://github.com/user-attachments/assets/83e51cd6-8ef7-4a4d-8696-ab37cc4a9dd6"
/>
</p>

Finally, the filters also enforce this "Id" suffix convention in the GET
request.

<p align="center">
<img width="854" height="480" alt="image"
src="https://github.com/user-attachments/assets/168a2f09-1242-40fa-bd84-1f7d9c60357c"
/>
</p>

Edit: Updated error messages after the screenshots were taken to make
them a little generic. Secondly, this PR also fixes the issue of morph
relation ids and objects not appearing in the response (when depth is
1).
This commit is contained in:
Abdullah.
2026-03-06 18:21:19 +05:00
committed by GitHub
parent 1f1da901ea
commit 9f9a6a45dd
17 changed files with 238 additions and 70 deletions
@@ -1,7 +1,10 @@
import { FieldMetadataType, RelationType } from 'twenty-shared/types';
import { getFlatFieldsFromFlatObjectMetadata } from 'src/engine/api/graphql/workspace-schema-builder/utils/get-flat-fields-for-flat-object-metadata.util';
import { computeMorphOrRelationFieldJoinColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-morph-or-relation-field-join-column-name.util';
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { isMorphOrRelationFlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/utils/is-morph-or-relation-flat-field-metadata.util';
import { isFlatFieldMetadataOfType } from 'src/engine/metadata-modules/flat-field-metadata/utils/is-flat-field-metadata-of-type.util';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
export type FieldMapsForObject = {
@@ -24,14 +27,16 @@ export const buildFieldMapsFromFlatObjectMetadata = (
for (const field of objectFields) {
fieldIdByName[field.name] = field.id;
if (isMorphOrRelationFlatFieldMetadata(field)) {
const joinColumnName = (
field.settings as { joinColumnName?: string } | undefined
)?.joinColumnName;
if (
(isFlatFieldMetadataOfType(field, FieldMetadataType.RELATION) ||
isFlatFieldMetadataOfType(field, FieldMetadataType.MORPH_RELATION)) &&
field.settings.relationType === RelationType.MANY_TO_ONE
) {
const joinColumnName = computeMorphOrRelationFieldJoinColumnName({
name: field.name,
});
if (joinColumnName) {
fieldIdByJoinColumnName[joinColumnName] = field.id;
}
fieldIdByJoinColumnName[joinColumnName] = field.id;
}
}