Skip to content

fix: instantiate subclass in duplicate and preserve constructor in applyMixin (#2053) - #2283

Closed
EnesKartal wants to merge 1 commit into
redis:mainfrom
EnesKartal:fix-subclass-duplicate
Closed

EnesKartal wants to merge 1 commit into
redis:mainfrom
EnesKartal:fix-subclass-duplicate

Conversation

@EnesKartal

@EnesKartal EnesKartal commented Nov 8, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes #2053 by ensuring duplicate() instantiates the subclass constructor when Redis or Cluster is extended (e.g. class CustomRedis extends Redis {}), and preserving constructor identity when applying mixins.

Problem

  1. In lib/Redis.ts and lib/cluster/index.ts, duplicate() explicitly invoked new Redis(...) or new Cluster(...), discarding any subclass instance and methods.
  2. In lib/utils/applyMixin.ts, copying properties from source.prototype indiscriminately included 'constructor' (EventEmitter.prototype.constructor), overwriting derivedConstructor.prototype.constructor. This corrupted this.constructor on instances of Redis / Cluster to EventEmitter.

Solution

  1. In applyMixin.ts, explicitly skip copying 'constructor' when applying prototype mixins.
  2. In Redis.prototype.duplicate, resolve constructor dynamically via const Ctor = (this.constructor as typeof Redis) || Redis; return new Ctor(...).
  3. In Cluster.prototype.duplicate, resolve constructor dynamically via const Ctor = (this.constructor as typeof Cluster) || Cluster; return new Ctor(...).
  4. Added functional and unit tests verifying duplicate() retains subclass type and custom properties for both Redis and Cluster.

Checks

  • Functional and unit tests added and passing
  • TypeScript build (npm run build) passing without errors
  • Type definitions test (npm run test:tsd) passing
  • ESLint (npm run lint) passing without errors

Note

Low Risk
Small inheritance and mixin fixes with targeted tests; behavior change only affects users who extend Redis/Cluster or rely on correct constructor after mixins.

Overview
Fixes subclass extension behavior: duplicate() on Redis and Cluster now instantiates via this.constructor instead of always creating base Redis / Cluster instances, so custom subclasses keep their type and methods when duplicated.

applyMixin no longer copies the mixin’s constructor onto the derived prototype, which previously overwrote instance constructor with EventEmitter and broke the dynamic constructor lookup in duplicate().

Functional and unit tests assert duplicated clients remain instances of CustomRedis / CustomCluster.

Reviewed by Cursor Bugbot for commit 7b480cd. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T09:03:04.470033Z 7b480cd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b480cddc2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/Redis.ts
override?: Override
): Redis<ReplyMappingFromOptions<ReplyMapping, Override>> {
return new Redis({
const Ctor = (this.constructor as typeof Redis) || Redis;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid selecting a command as the duplicate constructor

When a client is created with scripts: { constructor: ... } (accepted by the Record<string, ...> option and installed by defineCommand as an own this.constructor method), this lookup selects the generated Lua-command function rather than the Redis class. new Ctor(...) then executes that function against an uninitialized object and throws before creating a duplicate, whereas the previous hard-coded constructor worked; the identical lookup in Cluster.duplicate has the same regression. Resolve the class from the prototype rather than the potentially shadowed instance property.

AGENTS.md reference: AGENTS.md:L88-L88

Useful? React with 👍 / 👎.

@PavelPashov

Copy link
Copy Markdown
Contributor

Thanks for the PR. We've decided not to change duplicate(); see #2053 for the reasoning and alternatives. Closing along with #2098, which takes the same approach.

@PavelPashov PavelPashov closed this Nov 9, 2026
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.

duplicate method should new subclasses

2 participants