Skip to content

Commit 9286733

Browse files
Merge pull request #13923 from DylanVeldra/fix-tenant-context-payload
fix(core): merge req context with tenant payload in the request instance
2 parents 84b8744 + 81597f7 commit 9286733

9 files changed

+72
-11
lines changed

integration/scopes/e2e/durable-providers.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ describe('Durable providers', () => {
8989
expect(result.body).deep.equal({ tenantId: '3' });
9090
});
9191

92+
it(`should return the same tenantId both from durable request scoped service and non-durable request scoped service`, async () => {
93+
let result: request.Response;
94+
result = await new Promise<request.Response>(resolve =>
95+
performHttpCall(1, resolve, '/durable/request-context'),
96+
);
97+
expect(result.body).deep.equal({
98+
durableService: '1',
99+
nonDurableService: '1',
100+
});
101+
102+
result = await new Promise<request.Response>(resolve =>
103+
performHttpCall(2, resolve, '/durable/request-context'),
104+
);
105+
expect(result.body).deep.equal({
106+
durableService: '2',
107+
nonDurableService: '2',
108+
});
109+
});
110+
92111
it(`should not cache durable providers that throw errors`, async () => {
93112
let result: request.Response;
94113

integration/scopes/src/durable/durable-context-id.strategy.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { ContextId, ContextIdStrategy, HostComponentInfo } from '@nestjs/core';
22
import { Request } from 'express';
33

4+
export type TenantContext = {
5+
tenantId: string;
6+
forceError?: boolean;
7+
};
8+
49
const tenants = new Map<string, ContextId>();
510

611
export class DurableContextIdStrategy implements ContextIdStrategy {
@@ -17,10 +22,7 @@ export class DurableContextIdStrategy implements ContextIdStrategy {
1722
tenants.set(tenantId, tenantSubTreeId);
1823
}
1924

20-
const payload: {
21-
tenantId: string;
22-
forceError?: boolean;
23-
} = { tenantId };
25+
const payload: TenantContext = { tenantId };
2426
if (forceError) {
2527
payload.forceError = true;
2628
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { Controller, Get } from '@nestjs/common';
22
import { DurableService } from './durable.service';
3+
import { NonDurableService } from './non-durable.service';
34

45
@Controller('durable')
56
export class DurableController {
6-
constructor(private readonly durableService: DurableService) {}
7+
constructor(
8+
private readonly durableService: DurableService,
9+
private readonly nonDurableService: NonDurableService,
10+
) {}
711

812
@Get()
913
greeting(): string {
@@ -12,6 +16,16 @@ export class DurableController {
1216

1317
@Get('echo')
1418
echo() {
15-
return this.durableService.requestPayload;
19+
return {
20+
tenantId: this.durableService.getTenantId(),
21+
};
22+
}
23+
24+
@Get('request-context')
25+
getRequestContext() {
26+
return {
27+
durableService: this.durableService.getTenantId(),
28+
nonDurableService: this.nonDurableService.getTenantId(),
29+
};
1630
}
1731
}

integration/scopes/src/durable/durable.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { APP_GUARD } from '@nestjs/core';
33
import { DurableController } from './durable.controller';
44
import { DurableGuard } from './durable.guard';
55
import { DurableService } from './durable.service';
6+
import { NonDurableService } from './non-durable.service';
67

78
@Module({
89
controllers: [DurableController],
910
providers: [
1011
DurableService,
12+
NonDurableService,
1113
{
1214
provide: APP_GUARD,
1315
useClass: DurableGuard,

integration/scopes/src/durable/durable.service.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import {
55
Scope,
66
} from '@nestjs/common';
77
import { REQUEST } from '@nestjs/core';
8+
import { TenantContext } from './durable-context-id.strategy';
89

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

1314
constructor(
14-
@Inject(REQUEST)
15-
public readonly requestPayload: { tenantId: string; forceError: boolean },
15+
@Inject(REQUEST) private readonly requestPayload: TenantContext,
1616
) {
1717
if (requestPayload.forceError) {
1818
throw new PreconditionFailedException('Forced error');
@@ -23,4 +23,8 @@ export class DurableService {
2323
++this.instanceCounter;
2424
return `Hello world! Counter: ${this.instanceCounter}`;
2525
}
26+
27+
getTenantId() {
28+
return this.requestPayload.tenantId;
29+
}
2630
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Inject, Injectable, Scope } from '@nestjs/common';
2+
import { REQUEST } from '@nestjs/core';
3+
import { TenantContext } from './durable-context-id.strategy';
4+
5+
@Injectable()
6+
export class NonDurableService {
7+
constructor(
8+
@Inject(REQUEST) private readonly requestPayload: TenantContext,
9+
) {}
10+
11+
getTenantId() {
12+
return this.requestPayload.tenantId;
13+
}
14+
}

packages/core/middleware/middleware-module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ export class MiddlewareModule<
346346
configurable: false,
347347
});
348348

349-
const requestProviderValue = isTreeDurable ? contextId.payload : request;
349+
const requestProviderValue = isTreeDurable
350+
? contextId.payload
351+
: Object.assign(request, contextId.payload);
350352
this.container.registerRequestProvider(requestProviderValue, contextId);
351353
}
352354
return contextId;

packages/core/router/router-explorer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ export class RouterExplorer {
418418
configurable: false,
419419
});
420420

421-
const requestProviderValue = isTreeDurable ? contextId.payload : request;
421+
const requestProviderValue = isTreeDurable
422+
? contextId.payload
423+
: Object.assign(request, contextId.payload);
422424
this.container.registerRequestProvider(requestProviderValue, contextId);
423425
}
424426
return contextId;

packages/microservices/listeners-controller.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ export class ListenersController {
308308
configurable: false,
309309
});
310310

311-
const requestProviderValue = isTreeDurable ? contextId.payload : request;
311+
const requestProviderValue = isTreeDurable
312+
? contextId.payload
313+
: Object.assign(request, contextId.payload);
312314
this.container.registerRequestProvider(requestProviderValue, contextId);
313315
}
314316
return contextId;

0 commit comments

Comments
 (0)