mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-01 13:33:48 +08:00
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary Aligns Go and Python error codes/messages so both backends honor the same RESTful API contract, removing implementation-specific error leaks (MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in favor of clean business error codes. **Chat list** — invalid `orderby` now returns code 101 (was: raw Python `AttributeError` code 100); invalid `page`/`page_size` values fall back to defaults (was: raw `ValueError`/`ProgrammingError` code 100). **Dataset create/update/delete** — adds UUID validation (101), extra-field rejection (101), duplicate-id detection (101), content-type / JSON-syntax / object-shape checks (101), and "lacks permission" for nonexistent datasets (IDOR). Create auto-deduplicates dataset names. Pagerank updates tolerate a missing ES index. List response includes `parser_config` and `pagerank`. **Session list/update** — adds filtering, sorting, and pagination support. Empty payloads are valid no-ops. Authorization errors map to code 109. **Chunk list** — doc object uses Python key names (`chunk_count`, `dataset_id`, `chunk_method`, run text status). Add validates list element types. **Document update** — adds `chunk_method` alias, pydantic-style Field error messages, metadata index auto-create with refresh, and "These documents do not belong to dataset" messages. List validates `metadata_condition` and reports ownership errors for unmatched name/id filters. **Search completion** — `kb_ids` ownership failure returns code 102 instead of 109. Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on both Go and Python backends with real LLM keys).
This commit is contained in:
@@ -88,28 +88,60 @@ func (h *DatasetsHandler) ListDatasets(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Mirror pydantic's extra="forbid" on ListDatasetReq.
|
||||
for param := range c.Request.URL.Query() {
|
||||
if !listDatasetsAllowedParams[param] {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Extra inputs are not permitted: %s", param))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Mirror the pydantic validation of Python's ListDatasetReq.
|
||||
page := 1
|
||||
if pageStr := c.Query("page"); pageStr != "" {
|
||||
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
|
||||
page = p
|
||||
p, err := strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be a valid integer, unable to parse string as an integer")
|
||||
return
|
||||
}
|
||||
if p < 1 {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be greater than or equal to 1")
|
||||
return
|
||||
}
|
||||
page = p
|
||||
}
|
||||
|
||||
pageSize := 30
|
||||
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
|
||||
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
|
||||
pageSize = ps
|
||||
ps, err := strconv.Atoi(pageSizeStr)
|
||||
if err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be a valid integer, unable to parse string as an integer")
|
||||
return
|
||||
}
|
||||
if ps < 1 {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be greater than or equal to 1")
|
||||
return
|
||||
}
|
||||
pageSize = ps
|
||||
}
|
||||
|
||||
orderby := "create_time"
|
||||
if queryOrderby := c.Query("orderby"); queryOrderby != "" {
|
||||
if queryOrderby, exists := c.GetQuery("orderby"); exists {
|
||||
if queryOrderby != "create_time" && queryOrderby != "update_time" {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be 'create_time' or 'update_time'")
|
||||
return
|
||||
}
|
||||
orderby = queryOrderby
|
||||
}
|
||||
|
||||
desc := true
|
||||
if descStr := c.Query("desc"); descStr != "" {
|
||||
desc = strings.ToLower(descStr) == "true"
|
||||
parsed, ok := parsePythonBool(descStr)
|
||||
if !ok {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Input should be a valid boolean, unable to interpret input")
|
||||
return
|
||||
}
|
||||
desc = parsed
|
||||
}
|
||||
|
||||
keywords := ""
|
||||
@@ -128,6 +160,14 @@ func (h *DatasetsHandler) ListDatasets(c *gin.Context) {
|
||||
ownerIDs = ext.OwnerIDs
|
||||
}
|
||||
|
||||
// Mirror pydantic: a present-but-empty id fails UUID validation.
|
||||
if rawID, exists := c.GetQuery("id"); exists {
|
||||
if _, err := dataset.NormalizeDatasetID(strings.TrimSpace(rawID)); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
data, total, code, err := h.datasetsService.ListDatasets(
|
||||
ctx,
|
||||
@@ -162,11 +202,21 @@ func (h *DatasetsHandler) CreateDataset(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, _, ok := parseJSONRequestObject(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var req service.CreateDatasetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := json.Unmarshal(bodyBytes, &req); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
|
||||
return
|
||||
}
|
||||
// Mirror Python's pydantic required validation.
|
||||
if req.Name == "" || (len(bodyBytes) > 0 && jsonNullValue(bodyBytes, "name")) {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, "Field validation for 'name' failed on the 'required' tag")
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
|
||||
@@ -200,6 +250,96 @@ func (h *DatasetsHandler) GetDataset(c *gin.Context) {
|
||||
}
|
||||
|
||||
// UpdateDataset Update a dataset.
|
||||
// parsePythonBool mirrors pydantic's boolean coercion for query params:
|
||||
// accepts true/false/1/0/yes/no/y/n (case-insensitive).
|
||||
func parsePythonBool(s string) (bool, bool) {
|
||||
switch strings.ToLower(s) {
|
||||
case "true", "1", "yes", "y", "on":
|
||||
return true, true
|
||||
case "false", "0", "no", "n", "off":
|
||||
return false, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
// parseJSONRequestObject mirrors Python's validate_and_parse_json_request:
|
||||
// content-type check first, then JSON syntax, then object shape. On failure it
|
||||
// writes the error response and returns ok=false.
|
||||
func parseJSONRequestObject(c *gin.Context) (body []byte, raw map[string]interface{}, ok bool) {
|
||||
if c.ContentType() != "application/json" {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Unsupported content type: Expected application/json, got %s", c.GetHeader("Content-Type")))
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
body, err := c.GetRawData()
|
||||
if err != nil || len(body) == 0 {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Malformed JSON syntax: Missing commas/brackets or invalid encoding")
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
var payload interface{}
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Malformed JSON syntax: Missing commas/brackets or invalid encoding")
|
||||
return nil, nil, false
|
||||
}
|
||||
raw, isObject := payload.(map[string]interface{})
|
||||
if !isObject {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Invalid request payload: expected object, got %s", pythonJSONTypeName(payload)))
|
||||
return nil, nil, false
|
||||
}
|
||||
return body, raw, true
|
||||
}
|
||||
|
||||
// jsonNullValue checks if a field is explicitly null in the JSON body.
|
||||
func jsonNullValue(body []byte, field string) bool {
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &m); err != nil {
|
||||
return false
|
||||
}
|
||||
raw, ok := m[field]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return strings.TrimSpace(string(raw)) == "null"
|
||||
}
|
||||
|
||||
// pythonJSONTypeName maps a decoded JSON value to the Python type name used in
|
||||
// the "Invalid request payload" contract message.
|
||||
func pythonJSONTypeName(v interface{}) string {
|
||||
switch v.(type) {
|
||||
case string:
|
||||
return "str"
|
||||
case []interface{}:
|
||||
return "list"
|
||||
case float64:
|
||||
return "float"
|
||||
case bool:
|
||||
return "bool"
|
||||
case nil:
|
||||
return "NoneType"
|
||||
default:
|
||||
return "object"
|
||||
}
|
||||
}
|
||||
|
||||
// listDatasetsAllowedParams mirrors the query field set of Python's
|
||||
// ListDatasetReq (BaseListReq + include_parsing_status/ext; `type` is handled
|
||||
// before validation in the Python endpoint).
|
||||
var listDatasetsAllowedParams = map[string]bool{
|
||||
"id": true, "name": true, "page": true, "page_size": true,
|
||||
"orderby": true, "desc": true, "include_parsing_status": true,
|
||||
"ext": true, "type": true,
|
||||
}
|
||||
|
||||
// updateDatasetAllowedFields mirrors the field set of Python's UpdateDatasetReq
|
||||
// (CreateDatasetReq fields + dataset_id/pagerank/language/connectors).
|
||||
var updateDatasetAllowedFields = map[string]bool{
|
||||
"name": true, "avatar": true, "description": true, "embedding_model": true,
|
||||
"permission": true, "parse_type": true, "pipeline_id": true, "chunk_method": true,
|
||||
"parser_id": true, "parser_config": true, "auto_metadata_config": true, "ext": true,
|
||||
"dataset_id": true, "pagerank": true, "language": true, "connectors": true,
|
||||
}
|
||||
|
||||
func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
@@ -218,15 +358,19 @@ func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
|
||||
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "dataset id is required")
|
||||
return
|
||||
}
|
||||
// Mirror the pydantic UUID validation of Python's UpdateDatasetReq.
|
||||
if _, err := dataset.NormalizeDatasetID(datasetID); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, err := c.GetRawData()
|
||||
if err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
|
||||
bodyBytes, _, ok := parseJSONRequestObject(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdateDatasetRequest
|
||||
if err = json.Unmarshal(bodyBytes, &req); err != nil {
|
||||
if err := json.Unmarshal(bodyBytes, &req); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
|
||||
return
|
||||
}
|
||||
@@ -234,8 +378,15 @@ func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
|
||||
// Detect an explicitly provided parser_config key (even {} or null) so it is not
|
||||
// rejected as "no properties were modified", mirroring the Python contract.
|
||||
var providedFields map[string]json.RawMessage
|
||||
if err = json.Unmarshal(bodyBytes, &providedFields); err == nil {
|
||||
if err := json.Unmarshal(bodyBytes, &providedFields); err == nil {
|
||||
_, req.ParserConfigProvided = providedFields["parser_config"]
|
||||
// Mirror pydantic's extra="forbid" on UpdateDatasetReq.
|
||||
for field := range providedFields {
|
||||
if !updateDatasetAllowedFields[field] {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Extra inputs are not permitted: %s", field))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
@@ -396,13 +547,40 @@ func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, rawPayload, ok := parseJSONRequestObject(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Mirror pydantic's extra="forbid" on DeleteDatasetReq.
|
||||
for field := range rawPayload {
|
||||
if field != "ids" && field != "delete_all" {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Extra inputs are not permitted: %s", field))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var req struct {
|
||||
IDs *[]string `json:"ids"`
|
||||
DeleteAll bool `json:"delete_all,omitempty"`
|
||||
}
|
||||
if c.Request.ContentLength > 0 {
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
|
||||
if err := json.Unmarshal(bodyBytes, &req); err != nil {
|
||||
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Mirror DeleteReq duplicate detection.
|
||||
if req.IDs != nil {
|
||||
seen := make(map[string]int, len(*req.IDs))
|
||||
duplicates := make([]string, 0)
|
||||
for _, id := range *req.IDs {
|
||||
seen[id]++
|
||||
if seen[id] == 2 {
|
||||
duplicates = append(duplicates, id)
|
||||
}
|
||||
}
|
||||
if len(duplicates) > 0 {
|
||||
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("Duplicate ids: '%s'", strings.Join(duplicates, ", ")))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user