Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(template): answer 400 for null fields and a missing identifier

Yudhi Armyndhariseb7af4a3a5cf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9a0602859..8269e0bdf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A bulk batch sends each message through the session's current engine, so a reconnect or restart mid-batch no longer fails every remaining message.
- `POST /messages/send-bulk` answers 400 for an item with an empty `chatId`, a text item without text, or a media item without a `url` or `base64` under its type; such items used to be accepted with 202 and fail later.
- `POST /messages/send-bulk` answers 400, not 500, when a concurrent request already created the same `batchId`.
+- `PUT /templates/:id` answers 400, not 500, for a `null` `name` or `body`.
+- `POST /messages/send-template` without `templateId` or `templateName` answers 400 instead of 404.
## [0.23.5] - 2026-09-14
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index f93071372..995224742 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -1713,7 +1713,7 @@ Render a stored text template (header/body/footer joined by blank lines, `{{vars
Delegates to the send-text path after rendering.
-**Errors:** `400` unknown body field, validation failure, or session not active · `401` missing/invalid API key · `403` key role below OPERATOR · `404` session or template not found · `500` engine error · `409` conflict or engine not ready (retryable)
+**Errors:** `400` unknown body field, validation failure, neither `templateId` nor `templateName` given, or session not active · `401` missing/invalid API key · `403` key role below OPERATOR · `404` session or template not found · `500` engine error · `409` conflict or engine not ready (retryable)
#### POST /api/sessions/:sessionId/messages/send-image
@@ -3369,8 +3369,8 @@ Update a template's name/body/header/footer (partial; only provided fields chang
| Field | Type | Required | Constraints | Description |
| ------ | ------ | -------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| name | string | no | if present: non-empty, max 100 chars | Applied only when not `undefined`. Duplicate name → `409`. |
-| body | string | no | if present: non-empty, max 4096 chars | Applied only when not `undefined`. |
+| name | string | no | if present: non-empty, max 100 chars | Applied only when not `undefined`; explicit `null` → `400`. Duplicate name → `409`. |
+| body | string | no | if present: non-empty, max 4096 chars | Applied only when not `undefined`; explicit `null` → `400`. |
| header | string | no | max 1024 chars | Applied only when not `undefined`. The update path does **not** coerce to `null`, so passing explicit `null` fails `@IsString`; omit the key to leave it unchanged. |
| footer | string | no | max 1024 chars | Applied only when not `undefined`. |
diff --git a/src/modules/template/dto/template.dto.spec.ts b/src/modules/template/dto/template.dto.spec.ts
new file mode 100644
index 000000000..85769981e
--- /dev/null
+++ b/src/modules/template/dto/template.dto.spec.ts
@@ -0,0 +1,19 @@
+import { BadRequestException, ValidationPipe } from '@nestjs/common';
+import { UpdateTemplateDto } from './template.dto';
+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';
+
+describe('UpdateTemplateDto', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const through = (value: object): Promise<unknown> =>
+ pipe.transform(value, { type: 'body', metatype: UpdateTemplateDto });
+
+ it('accepts a partial update that leaves name and body out', async () => {
+ await expect(through({ footer: 'bye' })).resolves.toMatchObject({ footer: 'bye' });
+ });
+
+ // name and body are NOT NULL columns: an explicit null that passed validation reached save() and
+ // answered 500.
+ it.each(['name', 'body'])('rejects an explicit null %s', async field => {
+ await expect(through({ [field]: null })).rejects.toBeInstanceOf(BadRequestException);
+ });
+});
diff --git a/src/modules/template/dto/template.dto.ts b/src/modules/template/dto/template.dto.ts
index 39d3154be..b4d3f3b7f 100644
--- a/src/modules/template/dto/template.dto.ts
+++ b/src/modules/template/dto/template.dto.ts
@@ -1,5 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
-import { IsString, IsNotEmpty, IsOptional, MaxLength } from 'class-validator';
+import { IsString, IsNotEmpty, IsOptional, MaxLength, ValidateIf } from 'class-validator';
const NAME_MAX_LENGTH = 100;
const BODY_MAX_LENGTH = 4096;
@@ -49,14 +49,15 @@ export class CreateTemplateDto {
export class UpdateTemplateDto {
@ApiPropertyOptional({ description: 'Template name', maxLength: NAME_MAX_LENGTH })
- @IsOptional()
+ // Not @IsOptional: that also skips null, which then reaches the NOT NULL column as a 500.
+ @ValidateIf((o: UpdateTemplateDto) => o.name !== undefined)
@IsString()
@IsNotEmpty()
@MaxLength(NAME_MAX_LENGTH)
name?: string;
@ApiPropertyOptional({ description: 'Template body with {{variable}} placeholders', maxLength: BODY_MAX_LENGTH })
- @IsOptional()
+ @ValidateIf((o: UpdateTemplateDto) => o.body !== undefined)
@IsString()
@IsNotEmpty()
@MaxLength(BODY_MAX_LENGTH)
diff --git a/src/modules/template/template.service.spec.ts b/src/modules/template/template.service.spec.ts
index a9a0a97f8..8571ca183 100644
--- a/src/modules/template/template.service.spec.ts
+++ b/src/modules/template/template.service.spec.ts
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
-import { ConflictException, NotFoundException } from '@nestjs/common';
+import { BadRequestException, ConflictException, NotFoundException } from '@nestjs/common';
import { TemplateService } from './template.service';
import { Template } from './entities/template.entity';
import { Session } from '../session/entities/session.entity';
@@ -141,8 +141,10 @@ describe('TemplateService', () => {
await expect(service.resolve('sess-1', { templateName: 'nope' })).rejects.toThrow(NotFoundException);
});
- it('should throw NotFoundException when neither id nor name is provided', async () => {
- await expect(service.resolve('sess-1', {})).rejects.toThrow(NotFoundException);
+ // A malformed request, not a missing resource: a client reading 404 as "template deleted" would
+ // take the wrong branch.
+ it('should throw BadRequestException when neither id nor name is provided', async () => {
+ await expect(service.resolve('sess-1', {})).rejects.toThrow(BadRequestException);
});
});
diff --git a/src/modules/template/template.service.ts b/src/modules/template/template.service.ts
index 8c2138c70..9f5dfdfec 100644
--- a/src/modules/template/template.service.ts
+++ b/src/modules/template/template.service.ts
@@ -1,4 +1,4 @@
-import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
+import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Template } from './entities/template.entity';
@@ -52,8 +52,8 @@ export class TemplateService {
}
/**
- * Resolve a template for a session by id or by name. Throws NotFoundException
- * when neither identifier matches. Used by the send-template message flow.
+ * Resolve a template for a session by id or by name. Throws NotFoundException when the identifier
+ * matches nothing, BadRequestException when neither is given. Used by the send-template message flow.
*/
async resolve(sessionId: string, identifier: { templateId?: string; templateName?: string }): Promise<Template> {
const { templateId, templateName } = identifier;
@@ -75,7 +75,7 @@ export class TemplateService {
return template;
}
- throw new NotFoundException('Either templateId or templateName must be provided');
+ throw new BadRequestException('Either templateId or templateName must be provided');
}
async update(sessionId: string, id: string, dto: UpdateTemplateDto): Promise<Template> {
diff --git a/test/message-send.e2e-spec.ts b/test/message-send.e2e-spec.ts
index e9f4d149a..d7802aca2 100644
--- a/test/message-send.e2e-spec.ts
+++ b/test/message-send.e2e-spec.ts
@@ -266,8 +266,8 @@ describe('Message send endpoints (e2e)', () => {
expect(engine.sendTextMessage).not.toHaveBeenCalled();
});
- it('send-template without either identifier is a 404 naming the requirement', async () => {
- const res = await post('send-template', { chatId: '[email protected]' }).expect(404);
+ it('send-template without either identifier is a 400 naming the requirement', async () => {
+ const res = await post('send-template', { chatId: '[email protected]' }).expect(400);
expect((res.body as { message: string }).message).toBe('Either templateId or templateName must be provided');
expect(engine.sendTextMessage).not.toHaveBeenCalled();