fix: allow identical singular and plural labels for objects (#18678)

## Summary

Closes #18673

Some languages (e.g., German "Unternehmen") and even English words
(sheep, deer, aircraft, series) have identical singular and plural
forms. Twenty previously blocked saving when labels matched, making it
impossible to correctly name objects in these cases.

- **Labels** are purely display strings — removed the equality
validation from both the frontend Zod schema and backend validator
- **API names** (nameSingular/namePlural) must stay different since they
generate distinct GraphQL resolvers (`findOne` vs `findMany`,
`createOne` vs `createMany`, etc.) and REST endpoints — this validation
is preserved
- Added a shared `computeMetadataNamesFromLabels` util in
`twenty-shared` that auto-appends `'s'` to the plural API name when both
labels produce the same camelCase name (e.g., "Unternehmen" →
`unternehmen` / `unternehmens`)
- Both the frontend form and backend sync-check use the same shared util
— single source of truth, no duplicated logic

**No retroactive impact**: since the old code prevented identical labels
from ever being saved, no existing workspace has `labelSingular ===
labelPlural`.

## Test plan

- [x] New unit tests for `computeMetadataNamesFromLabels` (7 tests:
standard labels, Sheep, Unternehmen, Aircraft, empty labels, different
labels, applyCustomSuffix)
- [x] Updated frontend schema validation tests (identical labels with
different names now passes; identical names still fails)
- [x] Updated backend integration test cases (removed identical-label
failing cases)
- [ ] Manual: create a new object with identical singular/plural labels
(e.g. "Sheep" / "Sheep") — should save successfully with API names
`sheep` / `sheeps`
- [ ] Manual: verify existing objects with different labels still work
unchanged


Made with [Cursor](https://cursor.com)
This commit is contained in:
Félix Malfait
2026-03-16 18:07:34 +01:00
committed by GitHub
parent 13ff7af297
commit c4e55d08ff
23 changed files with 204 additions and 1572 deletions
@@ -0,0 +1,96 @@
import { computeMetadataNamesFromLabelsOrThrow } from '@/metadata/utils/compute-metadata-names-from-labels-or-throw.util';
describe('computeMetadataNamesFromLabelsOrThrow', () => {
it('should compute different names from different labels', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Company',
labelPlural: 'Companies',
}),
).toEqual({
nameSingular: 'company',
namePlural: 'companies',
});
});
it('should append "s" to plural name when labels produce identical names', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Sheep',
labelPlural: 'Sheep',
}),
).toEqual({
nameSingular: 'sheep',
namePlural: 'sheeps',
});
});
it('should handle German words with identical singular and plural', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Unternehmen',
labelPlural: 'Unternehmen',
}),
).toEqual({
nameSingular: 'unternehmen',
namePlural: 'unternehmens',
});
});
it('should not append "s" when labels produce different names', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Person',
labelPlural: 'People',
}),
).toEqual({
nameSingular: 'person',
namePlural: 'people',
});
});
it('should handle empty labels gracefully', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: '',
labelPlural: '',
}),
).toEqual({
nameSingular: '',
namePlural: '',
});
});
it('should apply custom suffix for reserved words by default', () => {
const result = computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Job',
labelPlural: 'Jobs',
});
expect(result.nameSingular).toBe('jobCustom');
expect(result.namePlural).toBe('jobsCustom');
});
it('should skip custom suffix when applyCustomSuffix is false', () => {
const result = computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Job',
labelPlural: 'Jobs',
applyCustomSuffix: false,
});
expect(result.nameSingular).toBe('job');
expect(result.namePlural).toBe('jobs');
});
it('should handle case-insensitive collision after camelCase conversion', () => {
expect(
computeMetadataNamesFromLabelsOrThrow({
labelSingular: 'Aircraft',
labelPlural: 'Aircraft',
}),
).toEqual({
nameSingular: 'aircraft',
namePlural: 'aircrafts',
});
});
});
@@ -0,0 +1,2 @@
// PostgreSQL max identifier length (NAMEDATALEN - 1)
export const IDENTIFIER_MAX_CHAR_LENGTH = 63;
@@ -14,6 +14,7 @@ export {
} from './check-if-field-is-label-identifier.util';
export { ALL_METADATA_NAME } from './constants/all-metadata-name.constant';
export { DEFAULT_RELATIONS_OBJECTS_STANDARD_IDS } from './constants/default-relations-object-standard-ids.constant';
export { IDENTIFIER_MAX_CHAR_LENGTH } from './constants/identifier-max-char-length.constant';
export { RESERVED_METADATA_NAME_KEYWORDS } from './constants/reserved-metadata-name-keywords.constant';
export { STANDARD_OBJECTS } from './constants/standard-object.constant';
export type { AllMetadataName } from './types/all-metadata-name.type';
@@ -25,3 +26,4 @@ export type {
export { WorkspaceMigrationV2ExceptionCode } from './types/MetadataValidationError';
export { addCustomSuffixIfIsReserved } from './utils/add-custom-suffix-if-reserved.util';
export { computeMetadataNameFromLabel } from './utils/compute-metadata-name-from-label.util';
export { computeMetadataNamesFromLabelsOrThrow } from './utils/compute-metadata-names-from-labels-or-throw.util';
@@ -0,0 +1,28 @@
import { IDENTIFIER_MAX_CHAR_LENGTH } from '@/metadata/constants/identifier-max-char-length.constant';
import { computeMetadataNameFromLabel } from '@/metadata/utils/compute-metadata-name-from-label.util';
export const computeMetadataNamesFromLabelsOrThrow = ({
labelSingular,
labelPlural,
applyCustomSuffix = true,
}: {
labelSingular: string;
labelPlural: string;
applyCustomSuffix?: boolean;
}): { nameSingular: string; namePlural: string } => {
const nameSingular = computeMetadataNameFromLabel({
label: labelSingular,
applyCustomSuffix,
});
let namePlural = computeMetadataNameFromLabel({
label: labelPlural,
applyCustomSuffix,
});
if (namePlural !== '' && namePlural === nameSingular) {
namePlural = (namePlural + 's').slice(0, IDENTIFIER_MAX_CHAR_LENGTH);
}
return { nameSingular, namePlural };
};