Skip to content

fix: trim whitespace from instanceName on creation#2546

Merged
DavidsonGomes merged 1 commit into
evolution-foundation:developfrom
brunovigo24:fix/trim-instance-name-whitespace
May 17, 2026
Merged

fix: trim whitespace from instanceName on creation#2546
DavidsonGomes merged 1 commit into
evolution-foundation:developfrom
brunovigo24:fix/trim-instance-name-whitespace

Conversation

@brunovigo24
Copy link
Copy Markdown

@brunovigo24 brunovigo24 commented May 17, 2026

📋 Description

Applies .trim() to instanceName at the beginning of createInstance in the instance controller. This ensures that any leading or trailing whitespace is removed before the instance name is persisted to the database.

🔗 Related Issue

Closes #2543

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced

Steps to reproduce and verify the fix:

  1. Create an instance with a trailing space in the name (e.g. "my-instance ")
  2. Confirm the instance is stored without the whitespace
  3. Call DELETE /instance/delete/my-instance — should return 200 instead of 404

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios

📝 Additional Notes

The root cause was that instance names with trailing spaces were stored as-is in the database, but URL path parameters used in delete/fetch operations naturally strip that whitespace — causing a name mismatch and a 404. The fix is applied at the entry point of creation so no invalid names ever reach the database.

Summary by Sourcery

Bug Fixes:

  • Trim leading and trailing whitespace from instance names before persisting them when creating instances.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 17, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds trimming of leading/trailing whitespace to instance names at the start of instance creation so only normalized names are persisted and used downstream.

Sequence diagram for instanceName trimming during instance creation

sequenceDiagram
  actor Client
  participant InstanceController
  participant ChannelController as channelController

  Client->>InstanceController: createInstance(instanceData)
  InstanceController->>InstanceController: instanceData.instanceName.trim()
  InstanceController->>ChannelController: init(instanceData, configService, eventEmitter)
  ChannelController-->>Client: createdInstanceResponse
Loading

File-Level Changes

Change Details Files
Normalize instance names on creation by trimming surrounding whitespace before initializing the instance.
  • Apply .trim() to the instanceName field at the beginning of createInstance
  • Use optional chaining when accessing instanceName to avoid errors if the field is undefined
  • Ensure that all subsequent logic in createInstance operates on the trimmed instanceName value, aligning stored names with those used in URL path parameters
src/api/controllers/instance.controller.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#2543 Trim leading and trailing whitespace from instanceName on creation in the backend so that no new instances are stored with whitespace-padded names.
#2543 Ensure the Manager UI preserves and sends the exact stored instanceName (including any leading/trailing whitespace) when performing operations like delete/logout/connect, so that existing whitespace-padded instances can be managed via the UI. The PR only modifies the backend createInstance controller to call .trim() on instanceName before initialization. It does not include any changes to the Manager UI or to how instance names are used in subsequent operations, so it does not help with managing already-existing instances that have whitespace in their names.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider avoiding mutation of the instanceData DTO inside the controller by normalizing instanceName earlier in the request pipeline (e.g., via a validation/transform pipe or factory) so the controller only deals with already-sanitized input.
  • If there are other code paths that create or update instances (e.g., update endpoints, background jobs, or seeding scripts), it may be safer to centralize instanceName normalization in a shared helper rather than only in createInstance to keep behavior consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider avoiding mutation of the `instanceData` DTO inside the controller by normalizing `instanceName` earlier in the request pipeline (e.g., via a validation/transform pipe or factory) so the controller only deals with already-sanitized input.
- If there are other code paths that create or update instances (e.g., update endpoints, background jobs, or seeding scripts), it may be safer to centralize `instanceName` normalization in a shared helper rather than only in `createInstance` to keep behavior consistent.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@brunovigo24
Copy link
Copy Markdown
Author

Thanks for the review! After analyzing the full request pipeline, I believe the current placement is the right approach for this specific case.

On moving normalization earlier in the pipeline:
The RouterBroker.dataValidate() is a shared base class that handles all routes across the system. Adding a trim there would affect fields in unrelated routes unnecessarily. The controller is the appropriate entry point for this business logic normalization.

On centralizing in a shared helper:
This would make sense if instanceName were persisted in multiple places, but it isn't. All other methods (deleteInstance, connectToWhatsapp, logout, etc.) receive the instance name via URL path parameters — not from user input. Since the fix prevents whitespace from ever being saved to the database, those methods will always receive clean names. The problem is solved at the source.

On JSONSchema validation:
A pattern constraint could reject names with whitespace, but it cannot transform/trim them — so it doesn't solve the root cause either.

The single-line fix in createInstance is intentionally minimal: it targets the only point where instanceName comes from user input and gets persisted, without touching shared infrastructure.

@DavidsonGomes
Copy link
Copy Markdown
Member

Atualizei a base do PR de main para develop (que é a branch ativa de desenvolvimento). Com isso o GitHub recalculou o diff e detectou conflito.

Pode rebasar em develop para resolver? Algo como:

git fetch upstream
git rebase upstream/develop
git push --force-with-lease

Depois disso, o fix de 1 linha (instanceData.instanceName?.trim()) está pronto para merge. Obrigado!

Prevents instances with trailing/leading whitespace in their names
from becoming undeletable via the API or UI. Fixes evolution-foundation#2543.
@DavidsonGomes DavidsonGomes force-pushed the fix/trim-instance-name-whitespace branch from b84e8e3 to c8add7a Compare May 17, 2026 22:21
@DavidsonGomes
Copy link
Copy Markdown
Member

@brunovigo24 fiz um rebase na sua branch removendo commits de main que já estavam em develop (mudanças de branding EvolutionAPIevolution-foundation em README/CONTRIBUTING/.gitmodules). Seu commit fix: trim whitespace from instanceName on creation foi preservado com autoria.

Force-push aplicado em brunovigo24/evolution-api:fix/trim-instance-name-whitespace. PR agora está MERGEABLE contra develop.

Obrigado pela contribuição!

Copy link
Copy Markdown
Member

@DavidsonGomes DavidsonGomes left a comment

Choose a reason for hiding this comment

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

LGTM após rebase em develop. Fix de 1 linha (instanceData.instanceName?.trim()), seguro, com optional chaining defensivo.

Edge case não-bloqueante para follow-up: nome só com espaços vira string vazia e ainda passa pela validação JSONSchema. Considerar adicionar if (!instanceData.instanceName) throw new BadRequestException('instanceName cannot be empty after trim') em PR futuro.

@DavidsonGomes DavidsonGomes merged commit 3570d7e into evolution-foundation:develop May 17, 2026
1 check passed
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.

UI fails to delete instance when instanceName contains trailing whitespace (404 "instance does not exist")

2 participants