From 758843ffd1db61ffd2500015a16bcba8648f21ff Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 25 Jan 2024 17:29:07 +0100 Subject: [PATCH 1/3] enhancement: remove the TOrigin type parameter from Attribute.Relation --- .../types/src/modules/documents/params/attributes.ts | 3 +-- .../modules/entity-service/params/attributes/index.ts | 3 +-- .../core/types/src/types/core/attributes/relation.ts | 10 ---------- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/core/types/src/modules/documents/params/attributes.ts b/packages/core/types/src/modules/documents/params/attributes.ts index af5fd11237..2d874167df 100644 --- a/packages/core/types/src/modules/documents/params/attributes.ts +++ b/packages/core/types/src/modules/documents/params/attributes.ts @@ -91,8 +91,7 @@ export type GetValue = Utils.Expression. // Relation [ Utils.Expression.Extends>, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - TAttribute extends Attribute.Relation + TAttribute extends Attribute.Relation ? Utils.Expression.If< Utils.Expression.IsNotNever, Attribute.RelationPluralityModifier diff --git a/packages/core/types/src/modules/entity-service/params/attributes/index.ts b/packages/core/types/src/modules/entity-service/params/attributes/index.ts index 28cba45620..f33ae83b27 100644 --- a/packages/core/types/src/modules/entity-service/params/attributes/index.ts +++ b/packages/core/types/src/modules/entity-service/params/attributes/index.ts @@ -63,8 +63,7 @@ export type GetValue< // Relation [ Utils.Expression.Extends>, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - TAttribute extends Attribute.Relation + TAttribute extends Attribute.Relation ? Utils.Expression.If< Utils.Expression.IsNotNever, RelationInputValue diff --git a/packages/core/types/src/types/core/attributes/relation.ts b/packages/core/types/src/types/core/attributes/relation.ts index 3ee5209840..971ea17edc 100644 --- a/packages/core/types/src/types/core/attributes/relation.ts +++ b/packages/core/types/src/types/core/attributes/relation.ts @@ -3,12 +3,6 @@ import type { Attribute, Common } from '..'; import type { Utils } from '../..'; export type Relation< - // TODO: TOrigin was originally needed to infer precise attribute literal types by doing a reverse lookup - // on TTarget -> TOrigin relations. Due to errors because of Attribute.Any [relation] very generic - // representation, type mismatches were encountered and mappedBy/inversedBy are now regular strings. - // It is kept to allow for future iterations without breaking the current type API - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _TOrigin extends Common.UID.Schema = Common.UID.Schema, TRelationKind extends RelationKind.Any = RelationKind.Any, TTarget extends Common.UID.ContentType = Common.UID.ContentType > = Attribute.OfType<'relation'> & @@ -107,8 +101,6 @@ export type RelationValue< > = RelationPluralityModifier>; export type GetRelationValue = TAttribute extends Relation< - // eslint-disable-next-line @typescript-eslint/no-unused-vars - infer _TOrigin, infer TRelationKind, infer TTarget > @@ -116,8 +108,6 @@ export type GetRelationValue = TAttribut : never; export type GetRelationTarget = TAttribute extends Relation< - // eslint-disable-next-line @typescript-eslint/no-unused-vars - infer _TOrigin, // eslint-disable-next-line @typescript-eslint/no-unused-vars infer _TRelationKind, infer TTarget From 16ac13f00a7a8683623f2337cd103469f0f1597d Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 25 Jan 2024 17:30:21 +0100 Subject: [PATCH 2/3] enhancement: fix type generation for Attribute.Relation by removing uid --- .../lib/generators/common/models/mappers.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/utils/typescript/lib/generators/common/models/mappers.js b/packages/utils/typescript/lib/generators/common/models/mappers.js index 43bfe51da4..0f135e8ea6 100644 --- a/packages/utils/typescript/lib/generators/common/models/mappers.js +++ b/packages/utils/typescript/lib/generators/common/models/mappers.js @@ -69,7 +69,7 @@ module.exports = { params.push(...targetFieldParams); // If the options property is defined, transform it to - // a type literral node and add it to the params list + // a type literal node and add it to the params list if (_.isObject(options)) { params.push(toTypeLiteral(options)); } @@ -93,25 +93,18 @@ module.exports = { media() { return [withAttributeNamespace('Media')]; }, - relation({ uid, attribute }) { + relation({ attribute }) { const { relation, target } = attribute; const isMorphRelation = relation.toLowerCase().includes('morph'); if (isMorphRelation) { - return [ - withAttributeNamespace('Relation'), - [factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)], - ]; + return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]]; } return [ withAttributeNamespace('Relation'), - [ - factory.createStringLiteral(uid, true), - factory.createStringLiteral(relation, true), - factory.createStringLiteral(target, true), - ], + [factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)], ]; }, component({ attribute }) { From bcfeeb5b11ba9361eeb6c13c05865c28c1f6e548 Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 25 Jan 2024 17:40:04 +0100 Subject: [PATCH 3/3] fix: modify generator tests to reflect latest changes on Attribute.Relation --- .../generators/schemas/attributes.test.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js b/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js index 97021f703b..da281ff748 100644 --- a/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js +++ b/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js @@ -256,16 +256,13 @@ describe('Attributes', () => { defaultAssertions(typeNode, 'Attribute.Relation'); - expect(typeNode.typeArguments).toHaveLength(3); + expect(typeNode.typeArguments).toHaveLength(2); expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); - expect(typeNode.typeArguments[0].text).toBe('api::foo.foo'); + expect(typeNode.typeArguments[0].text).toBe('oneToOne'); expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); - expect(typeNode.typeArguments[1].text).toBe('oneToOne'); - - expect(typeNode.typeArguments[2].kind).toBe(ts.SyntaxKind.StringLiteral); - expect(typeNode.typeArguments[2].text).toBe('api::bar.bar'); + expect(typeNode.typeArguments[1].text).toBe('api::bar.bar'); }); test('Polymorphic relation', () => { @@ -274,13 +271,10 @@ describe('Attributes', () => { defaultAssertions(typeNode, 'Attribute.Relation'); - expect(typeNode.typeArguments).toHaveLength(2); + expect(typeNode.typeArguments).toHaveLength(1); expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); - expect(typeNode.typeArguments[0].text).toBe('api::foo.foo'); - - expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); - expect(typeNode.typeArguments[1].text).toBe('morphMany'); + expect(typeNode.typeArguments[0].text).toBe('morphMany'); }); });