Skip to content

Conversation

wingsbob
Copy link

Ran into an issue where functions which have had properties assigned to them were stripped off when upgrading from nestjs-cls@3 to nestjs-cls@4 and above

I believe this should illustrate where this was an issue:

import { ClsModule } from 'nestjs-cls';

export const PROVIDER_SYMBOL = Symbol();

export class ContrivedExample {
  public property: (() => void) & { foo: string; bar: { baz: string } };
  constructor() {
    const someFunction = () => {};

    this.property = Object.assign(someFunction, {
      foo: 'foo',
      bar: { baz: 'baz' },
    });
  }
}
export const ContrivedExampleModule = ClsModule.forFeatureAsync({
  provide: PROVIDER_SYMBOL,
  imports: [],
  inject: [],
  useFactory: () => new ContrivedExample(),
});

This is just hooking the example up to a controller

import { Module } from '@nestjs/common';
import { HealthController } from './health.controller';
import { ContrivedExampleModule } from '../example.module';

@Module({
  imports: [ContrivedExampleModule],
  controllers: [HealthController],
})
export class HealthModule {}

When we try to access things within the controller, we see that the properties have mysteriously vanished

import { Controller, Get, Inject } from '@nestjs/common';
import { PROVIDER_SYMBOL, type ContrivedExample } from '../example.module';

@Controller('health')
export class HealthController {
  constructor(
    @Inject(PROVIDER_SYMBOL) private readonly example: ContrivedExample
  ) {}
  @Get()
  getHealth(): { status: 'ok' } {
    this.example.property(); // Works just fine
    console.log(this.example.property.bar.baz); // Errors as `bar` is undefined due to `property` being bound
    return { status: 'ok' };
  }
}

@Papooch
Copy link
Owner

Papooch commented Oct 6, 2025

Hi, sorry, I only noticed this PR now. Thank you for the fix! Can I also ask you to include a test case for this behavior?

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