Compare commits

...

2 Commits

Author SHA1 Message Date
Hein
3d2e11eeed fix(restheadspec): always respond 200 OK regardless of result count in sendFormattedResponse
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -33m27s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -32m51s
Build , Vet Test, and Lint / Lint Code (push) Failing after -33m3s
Build , Vet Test, and Lint / Build (push) Successful in -33m10s
Tests / Unit Tests (push) Successful in -33m58s
Tests / Integration Tests (push) Failing after -34m20s
2026-05-19 09:46:25 +02:00
Hein
4493bfa40f feat(reflection): add IsEmptyValue helper; guard CUD ops against missing PK
Add reflection.IsEmptyValue to detect nil, empty string, and zero numbers.
Use it in recursive CUD processing to skip update/delete when the primary
key is absent, logging a warning instead of proceeding with an invalid operation.
2026-05-19 09:14:19 +02:00
3 changed files with 38 additions and 6 deletions

View File

@@ -143,6 +143,10 @@ func (p *NestedCUDProcessor) ProcessNestedCUD(
case "update", "change":
// Only perform update if we have data to update
if reflection.IsEmptyValue(data[pkName]) {
logger.Warn("Skipping update for %s - no primary key", tableName)
return result, nil
}
if hasData {
rows, err := p.processUpdate(ctx, regularData, tableName, data[pkName])
if err != nil {
@@ -171,10 +175,15 @@ func (p *NestedCUDProcessor) ProcessNestedCUD(
}
case "delete":
if reflection.IsEmptyValue(data[pkName]) {
logger.Warn("Skipping delete for %s - no primary key", tableName)
return result, nil
}
// Process child relations first (for referential integrity)
if err := p.processChildRelations(ctx, "delete", data[pkName], relationFields, result.RelationData, modelType, parentIDs); err != nil {
logger.Error("Failed to process child relations before delete: table=%s, id=%v, relations=%+v, error=%v", tableName, data[pkName], relationFields, err)
return nil, nil
return nil, fmt.Errorf("failed to process child relations: %w", err)
}
rows, err := p.processDelete(ctx, tableName, data[pkName])

View File

@@ -51,6 +51,31 @@ func ExtractTableNameOnly(fullName string) string {
return fullName[startIndex:]
}
// IsEmptyValue reports whether v is nil, an empty string, or a zero number.
func IsEmptyValue(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
return true
}
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.String:
return rv.String() == ""
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rv.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return rv.Uint() == 0
case reflect.Float32, reflect.Float64:
return rv.Float() == 0
}
return false
}
// GetPointerElement returns the element type if the provided reflect.Type is a pointer.
// If the type is a slice of pointers, it returns the element type of the pointer within the slice.
// If neither condition is met, it returns the original type.

View File

@@ -2561,10 +2561,8 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
dataLen := reflection.Len(data)
// Add X-No-Data-Found header when no records were found
httpStatus := http.StatusOK
if dataLen == 0 {
w.SetHeader("X-No-Data-Found", "true")
httpStatus = http.StatusNoContent
}
// Apply normalization after header is set
@@ -2588,7 +2586,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
switch options.ResponseFormat {
case "simple":
// Simple format: just return the data array
w.WriteHeader(httpStatus)
w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(data); err != nil {
logger.Error("Failed to write JSON response: %v", err)
}
@@ -2600,7 +2598,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
if metadata != nil {
response["count"] = metadata.Total
}
w.WriteHeader(httpStatus)
w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(response); err != nil {
logger.Error("Failed to write JSON response: %v", err)
}
@@ -2611,7 +2609,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
Data: data,
Metadata: metadata,
}
w.WriteHeader(httpStatus)
w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(response); err != nil {
logger.Error("Failed to write JSON response: %v", err)
}