Skip to content

feat(cli): retain property metadata in relation decorator arguments - #11883

Open
aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/retain-prop-details
Open

aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/retain-prop-details

Conversation

@aaqilniz

@aaqilniz aaqilniz commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Ensure property decorator metadata (e.g., column configurations, nullability) is preserved and placed in the correct argument lot across relation generators (hasMany, hasOne, referencesMany, belongsTo).

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>

@samarpanB samarpanB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. Preserving existing property metadata when converting properties into relations is a useful improvement, especially for belongsTo and referencesMany.

However, the implementation currently applies the same metadata-forwarding approach across relation decorators with different APIs, which can produce invalid generated TypeScript.

There are also concerns around decorator lookup, preservation of optionality and property types, and string-based metadata reconstruction.

I've left inline comments with the specific issues. I think we should address these and add targeted integration coverage before merging.

if (decoratorArguments.length === 1) {
decoratorArguments.push('{}');
}
decoratorArguments.push(`{${cleanedPropertyMetadata}}`);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about adding property metadata as a third argument here.

Unlike belongsTo and referencesMany, the hasMany and hasOne decorators only accept two arguments (targetResolver and definition). The same issue applies to hasManyThrough.

If the existing property has metadata, this change can generate TypeScript that doesn't compile.

Can we limit third-argument property metadata preservation to decorators that actually support it (belongsTo and referencesMany), rather than applying the same approach across all relation generators?

const decorators = property.getDecorators();
if (decorators.length === 0) return '';

const firstArg = decorators[0].getArguments()[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we explicitly look for the @property decorator instead of assuming the first decorator contains the property metadata?

Currently, decorators[0].getArguments()[0] could return an argument from another decorator, resulting in incorrect metadata being carried into the generated relation.

The existing doesRelationExist() helper also assumes decorator ordering, so we should make sure both parts of the conversion logic behave consistently.

Can we retrieve the @property decorator by name and handle cases where it doesn't exist?

let isPropertyOptional = false;
if (
modelPropertyMetadata &&
modelPropertyMetadata.includes('nullable: true')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should infer TypeScript optionality from the nullable: true string in the property metadata.

For example, an existing declaration such as companyId?: number may not contain nullable: true, but the generated relation property would lose the optional marker.

Similarly, database nullability and TypeScript optionality are not necessarily equivalent.

Can we preserve the original property's question token (?) and declared type directly from the AST, rather than deriving them from metadata strings? We should also reconsider stripping | undefined from the original type.

if (modelPropertyMetadata && modelPropertyMetadata.trim() !== '') {
// Strip outer braces if modelPropertyMetadata is passed as a braced string like "{ nullable: true }"
cleanedPropertyMetadata = modelPropertyMetadata
.replace(/^\{|\}$/g, '')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the existing @property() argument is always an inline object literal.

For example, if the model uses @property(companyIdsPropertyDefinition), stripping braces and wrapping the expression in a new object would change its meaning instead of preserving it.

Since we're already using ts-morph, can we preserve the original metadata expression directly or use AST-based handling instead of manipulating the expression as a string?

This would also avoid fragile behavior with more complex property definitions.

name?: string;

@referencesMany(() => Account)
@referencesMany(() => Account, {}, {type: 'array', })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated snapshots demonstrate that a third decorator argument is generated, but they don't establish that the metadata is correctly preserved or that the generated model remains valid.

Could we add integration tests covering:

  • Existing @property() with PostgreSQL-specific metadata (e.g., dataType: 'jsonb')
  • Optional properties and union types
  • Custom relation names and foreign keys
  • Metadata defined through a variable rather than an inline object
  • All affected relation generators

I'd also like to see TypeScript compilation validation of the generated models, particularly for hasMany and hasOne, where the decorator signatures don't support a third argument.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants