Skip to content

fix: preserve chatflow id on API create#6419

Open
puneetdixit200 wants to merge 1 commit into
FlowiseAI:mainfrom
puneetdixit200:bugfix/preserve-chatflow-api-id
Open

fix: preserve chatflow id on API create#6419
puneetdixit200 wants to merge 1 commit into
FlowiseAI:mainfrom
puneetdixit200:bugfix/preserve-chatflow-api-id

Conversation

@puneetdixit200
Copy link
Copy Markdown

Summary

  • Preserve an explicit chatflow id when creating a chatflow through the API, so exported flows can be restored with their original ID.
  • Validate caller-provided IDs and reject duplicates before saving, avoiding accidental updates through the create path.
  • Add controller and service coverage for the create-ID path.

Fixes #6418

Testing

  • npx --yes pnpm@10.26.0 --filter flowise-components build
  • npx --yes pnpm@10.26.0 --dir packages/server exec jest src/controllers/chatflows/index.test.ts --runInBand
  • npx --yes pnpm@10.26.0 --dir packages/server exec jest src/services/chatflows/index.test.ts --runInBand
  • npx --yes pnpm@10.26.0 exec prettier --check packages/server/src/controllers/chatflows/index.ts packages/server/src/controllers/chatflows/index.test.ts packages/server/src/services/chatflows/index.ts packages/server/src/services/chatflows/index.test.ts
  • npx --yes pnpm@10.26.0 exec eslint packages/server/src/controllers/chatflows/index.ts packages/server/src/controllers/chatflows/index.test.ts packages/server/src/services/chatflows/index.ts packages/server/src/services/chatflows/index.test.ts
  • git diff --check

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enables the preservation of caller-provided IDs during chatflow creation, which is necessary for importing flows. It introduces validation logic to ensure that provided IDs are valid UUIDs and do not conflict with existing records, along with comprehensive unit tests for both the controller and service layers. Review feedback identifies that using Object.prototype.hasOwnProperty.call on a class instance may be unreliable due to TypeScript property initialization behavior and suggests using a nullish check instead.

const appServer = getRunningExpressApp()
const chatFlowRepository = appServer.AppDataSource.getRepository(ChatFlow)

if (Object.prototype.hasOwnProperty.call(newChatFlow, 'id')) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Using Object.prototype.hasOwnProperty.call on a class instance like newChatFlow (which is an instance of the ChatFlow entity) can be unreliable. Depending on the TypeScript configuration (specifically useDefineForClassFields), class properties might be initialized to undefined in the constructor, which would cause hasOwnProperty to return true even if the property wasn't explicitly provided by the caller. This would lead to a BAD_REQUEST error on every chatflow creation where an ID is not provided. A safer and more idiomatic approach is to use a nullish check.

Suggested change
if (Object.prototype.hasOwnProperty.call(newChatFlow, 'id')) {
if (newChatFlow.id != null) {
References
  1. In JavaScript/TypeScript, use loose equality (== null) as a standard idiom for a 'nullish' check that covers both null and undefined.

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.

Create a new chatflow using API not preserve id anymore

1 participant