Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(api): reject empty provider instance deletes (#18919)

huyua910e211494bc1
diff --git a/internal/handler/providers.go b/internal/handler/providers.go
index a96d5674ac..5aedb4e59c 100644
--- a/internal/handler/providers.go
+++ b/internal/handler/providers.go
@@ -641,7 +641,7 @@ func (h *ProviderHandler) AlterProviderInstance(c *gin.Context) {
}
type DropProviderInstanceRequest struct {
- Instances []string `json:"instances" binding:"required"`
+ Instances []string `json:"instances" binding:"required,min=1,dive,required"`
}
func (h *ProviderHandler) DropProviderInstance(c *gin.Context) {
diff --git a/internal/handler/providers_test.go b/internal/handler/providers_test.go
index 77dfda7e72..a88632d5b1 100644
--- a/internal/handler/providers_test.go
+++ b/internal/handler/providers_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"github.com/gin-gonic/gin"
+ "github.com/gin-gonic/gin/binding"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -17,6 +18,32 @@ import (
"ragflow/internal/service"
)
+func TestDropProviderInstanceRequestRequiresNonEmptyInstances(t *testing.T) {
+ tests := []struct {
+ name string
+ payload string
+ valid bool
+ }{
+ {name: "missing", payload: `{}`, valid: false},
+ {name: "empty", payload: `{"instances":[]}`, valid: false},
+ {name: "empty element", payload: `{"instances":[""]}`, valid: false},
+ {name: "instance", payload: `{"instances":["instance-a"]}`, valid: true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ var req DropProviderInstanceRequest
+ if err := json.Unmarshal([]byte(tt.payload), &req); err != nil {
+ t.Fatal(err)
+ }
+ err := binding.Validator.ValidateStruct(&req)
+ if (err == nil) != tt.valid {
+ t.Fatalf("validation error = %v, valid = %v", err, tt.valid)
+ }
+ })
+ }
+}
+
func setupProviderHandlerTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})