Skip to content

fix: EntitySchema without target or name silently produces "undefinedRepository" token #2686

Description

@jyx-07

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When an EntitySchema is constructed with neither options.target nor options.name set, getRepositoryToken() silently produces the token string "undefinedRepository" instead of throwing.

Two branches earlier in the same function, a null/undefined entity correctly throws CircularDependencyException — same class of misconfiguration but handled inconsistently.

Affected file: lib/common/typeorm.utils.ts (the getRepositoryToken function)

Current code:

if (entity instanceof EntitySchema) {
  return `${dataSourcePrefix}${
    entity.options.target ? entity.options.target.name : entity.options.name
  }Repository`;
}

Minimum reproduction code

This is a source-level defect found via static code review rather than a bug that needs a sample app to reproduce. The exact location is here:

if (entity instanceof EntitySchema) {
return `${dataSourcePrefix}${
entity.options.target ? entity.options.target.name : entity.options.name
}Repository`;
}

Steps to reproduce

  1. Call getRepositoryToken(entitySchema) where entitySchema is an EntitySchema instance whose options object has neither target nor name defined.
  2. Observe that the function returns the string "undefinedRepository" instead of throwing.

Expected behavior

Should throw a clear error when EntitySchema has neither target nor name defined, instead of silently producing an invalid token.

Proposed fix:

if (entity instanceof EntitySchema) {
  const schemaName = entity.options.target
    ? entity.options.target.name
    : entity.options.name;
  if (!schemaName) {
    throw new Error('EntitySchema must define either "target" or "name"');
  }
  return `${dataSourcePrefix}${schemaName}Repository`;
}

Package version

N/A — found via static code review of the repository at commit 3513624 (current master), not tied to a specific published release's reproduction.

NestJS version

N/A

Node.js version

N/A

In which operating systems have you tested?

N/A — static code review, not a runtime reproduction.

Other

I'd like to fix this — will open a PR shortly.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions