Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): populate snippet defaults before response (#45496)

非法操作bc675eb0e063
diff --git a/api/services/snippet_service.py b/api/services/snippet_service.py
index 9e8d46656c6..7aea927fa76 100644
--- a/api/services/snippet_service.py
+++ b/api/services/snippet_service.py
@@ -322,6 +322,7 @@ class SnippetService:
with self._session_scope() as session:
session.add(snippet)
+ session.flush()
self._commit_if_owned(session)
return snippet
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_snippets.py b/api/tests/unit_tests/controllers/console/workspace/test_snippets.py
index ea9a6a57859..56ff7cd657c 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_snippets.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_snippets.py
@@ -6,6 +6,7 @@ from unittest.mock import Mock
import pytest
from flask import Flask
from pydantic import ValidationError
+from sqlalchemy import select
from sqlalchemy.orm import Session
from werkzeug.exceptions import BadRequest, NotFound
@@ -174,6 +175,27 @@ def test_create_snippet_defaults_unknown_type_and_returns_created(
assert create_snippet.call_args.kwargs["snippet_type"] == snippets_module.SnippetType.NODE
[email protected]("implicit_returning", [True, False])
+def test_create_snippet_returns_database_defaults_without_committing(
+ app: Flask, sqlite_session: Session, monkeypatch: pytest.MonkeyPatch, implicit_returning: bool
+):
+ monkeypatch.setattr(CustomizedSnippet.__table__, "implicit_returning", implicit_returning)
+ monkeypatch.setattr(CustomizedSnippet.__mapper__, "_compiled_cache", {})
+ api = snippets_module.CustomizedSnippetsApi()
+ handler = unwrap(api.post)
+ payload = snippets_module.CreateSnippetPayload(name="Snippet", type="node")
+
+ with app.test_request_context("/workspaces/current/customized-snippets", method="POST"):
+ response, status_code = handler(api, payload, sqlite_session, "tenant-1", _account())
+
+ assert status_code == 201
+ assert (response["version"], response["use_count"], response["is_published"]) == (1, 0, False)
+ snippet_id = response["id"]
+ assert sqlite_session.scalar(select(CustomizedSnippet).where(CustomizedSnippet.id == snippet_id)) is not None
+ sqlite_session.rollback()
+ assert sqlite_session.scalar(select(CustomizedSnippet).where(CustomizedSnippet.id == snippet_id)) is None
+
+
def test_create_snippet_rejects_forbidden_nodes(app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session):
user = _account("account-1")
create_snippet = Mock()