Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): merge request context with tenant context payload in the request singleton #13485

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions integration/scopes/e2e/durable-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ describe('Durable providers', () => {
);
expect(result.body).deep.equal({ tenantId: '3' });
});

it(`should return the same tenantId both from durable request scoped service and non-durable request scoped service`, async () => {
let result: request.Response;
result = await new Promise<request.Response>(resolve =>
performHttpCall(1, resolve, '/durable/request-context'),
);
expect(result.body).deep.equal({
durableService: '1',
nonDurableService: '1',
});

result = await new Promise<request.Response>(resolve =>
performHttpCall(2, resolve, '/durable/request-context'),
);
expect(result.body).deep.equal({
durableService: '2',
nonDurableService: '2',
});
});
});

after(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ContextId, ContextIdStrategy, HostComponentInfo } from '@nestjs/core';
import { Request } from 'express';

export type TenantContext = {
tenantId: string;
};

const tenants = new Map<string, ContextId>();

export class DurableContextIdStrategy implements ContextIdStrategy {
Expand All @@ -17,7 +21,7 @@ export class DurableContextIdStrategy implements ContextIdStrategy {
return {
resolve: (info: HostComponentInfo) =>
info.isTreeDurable ? tenantSubTreeId : contextId,
payload: { tenantId },
payload: { tenantId } satisfies TenantContext,
};
}
}
18 changes: 16 additions & 2 deletions integration/scopes/src/durable/durable.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Controller, Get } from '@nestjs/common';
import { DurableService } from './durable.service';
import { NonDurableService } from './non-durable.service';

@Controller('durable')
export class DurableController {
constructor(private readonly durableService: DurableService) {}
constructor(
private readonly durableService: DurableService,
private readonly nonDurableService: NonDurableService,
) {}

@Get()
greeting(): string {
Expand All @@ -12,6 +16,16 @@ export class DurableController {

@Get('echo')
echo() {
return this.durableService.requestPayload;
return {
tenantId: this.durableService.getTenantId(),
};
}

@Get('request-context')
getRequestContext() {
return {
durableService: this.durableService.getTenantId(),
nonDurableService: this.nonDurableService.getTenantId(),
};
}
}
2 changes: 2 additions & 0 deletions integration/scopes/src/durable/durable.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { APP_GUARD } from '@nestjs/core';
import { DurableController } from './durable.controller';
import { DurableGuard } from './durable.guard';
import { DurableService } from './durable.service';
import { NonDurableService } from './non-durable.service';

@Module({
controllers: [DurableController],
providers: [
DurableService,
NonDurableService,
{
provide: APP_GUARD,
useClass: DurableGuard,
Expand Down
9 changes: 8 additions & 1 deletion integration/scopes/src/durable/durable.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TenantContext } from './durable-context-id.strategy';

@Injectable({ scope: Scope.REQUEST, durable: true })
export class DurableService {
public instanceCounter = 0;

constructor(@Inject(REQUEST) public readonly requestPayload: unknown) {}
constructor(
@Inject(REQUEST) private readonly requestPayload: TenantContext,
) {}

greeting() {
++this.instanceCounter;
return `Hello world! Counter: ${this.instanceCounter}`;
}

getTenantId() {
return this.requestPayload.tenantId;
}
}
14 changes: 14 additions & 0 deletions integration/scopes/src/durable/non-durable.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TenantContext } from './durable-context-id.strategy';

@Injectable()
export class NonDurableService {
constructor(
@Inject(REQUEST) private readonly requestPayload: TenantContext,
) {}

getTenantId() {
return this.requestPayload.tenantId;
}
}
4 changes: 3 additions & 1 deletion packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ export class MiddlewareModule<
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ export class RouterExplorer {
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down
4 changes: 3 additions & 1 deletion packages/microservices/listeners-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ export class ListenersController {
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down