Compare commits

..

7 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
Hein
b157379ff8 fix(restheadspec): return 200 OK with empty body instead of 204 on zero results
Frontend clients are sensitive to 204 No Content responses; always return 200
with an empty array/object and rely on X-No-Data-Found header to signal absence
of records.

Also treat "change" as an alias for "update" in recursive CUD processing.
2026-05-19 08:56:11 +02:00
Hein
52752d9c8b fix(bun): adjust field alignment in BunSelectQuery struct
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -33m53s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -33m29s
Build , Vet Test, and Lint / Lint Code (push) Failing after -33m33s
Build , Vet Test, and Lint / Build (push) Successful in -33m41s
Tests / Unit Tests (push) Successful in -34m28s
Tests / Integration Tests (push) Failing after -34m36s
2026-05-18 17:12:32 +02:00
Hein
baca5ad29e fix(bun): add relation alias handling for separate-query preloads
* implement preloadRelationAlias to rewrite WHERE conditions
* update Where method to handle relation alias in queries
2026-05-18 17:12:21 +02:00
Hein
53ab22ce02 fix(nestedCUD): handle error in processChildRelations gracefully 2026-05-18 16:14:24 +02:00
Hein
09a3dc92b9 fix(restheadspec): normalize empty results to objects instead of arrays 2026-05-18 14:37:46 +02:00
5 changed files with 118 additions and 50 deletions

View File

@@ -289,19 +289,20 @@ func (b *BunAdapter) DriverName() string {
// BunSelectQuery implements SelectQuery for Bun // BunSelectQuery implements SelectQuery for Bun
type BunSelectQuery struct { type BunSelectQuery struct {
query *bun.SelectQuery query *bun.SelectQuery
db bun.IDB // Store DB connection for count queries db bun.IDB // Store DB connection for count queries
hasModel bool // Track if Model() was called hasModel bool // Track if Model() was called
schema string // Separated schema name schema string // Separated schema name
tableName string // Just the table name, without schema tableName string // Just the table name, without schema
entity string entity string
tableAlias string tableAlias string
driverName string // Database driver name (postgres, sqlite, mssql) driverName string // Database driver name (postgres, sqlite, mssql)
inJoinContext bool // Track if we're in a JOIN relation context inJoinContext bool // Track if we're in a JOIN relation context
joinTableAlias string // Alias to use for JOIN conditions joinTableAlias string // Alias to use for JOIN conditions
skipAutoDetect bool // Skip auto-detection to prevent circular calls skipAutoDetect bool // Skip auto-detection to prevent circular calls
customPreloads map[string][]func(common.SelectQuery) common.SelectQuery // Relations to load with custom implementation preloadRelationAlias string // Relation alias used in separate-query preloads (e.g. "tprp" for relation "TPRP")
metricsEnabled bool customPreloads map[string][]func(common.SelectQuery) common.SelectQuery // Relations to load with custom implementation
metricsEnabled bool
} }
func (b *BunSelectQuery) Model(model interface{}) common.SelectQuery { func (b *BunSelectQuery) Model(model interface{}) common.SelectQuery {
@@ -346,12 +347,14 @@ func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.Se
} }
func (b *BunSelectQuery) Where(query string, args ...interface{}) common.SelectQuery { func (b *BunSelectQuery) Where(query string, args ...interface{}) common.SelectQuery {
// If we're in a JOIN context, add table prefix to unqualified columns
if b.inJoinContext && b.joinTableAlias != "" { if b.inJoinContext && b.joinTableAlias != "" {
query = addTablePrefix(query, b.joinTableAlias) query = addTablePrefix(query, b.joinTableAlias)
} else if b.preloadRelationAlias != "" && b.tableName != "" {
// Separate-query preload: the caller may have written conditions using the
// relation name as a prefix (e.g. "TPRP.col"). Bun uses the real table name
// as the alias, so rewrite any such references to use tableName instead.
query = replaceRelationAlias(query, b.preloadRelationAlias, b.tableName)
} else if b.tableAlias != "" && b.tableName != "" { } else if b.tableAlias != "" && b.tableName != "" {
// If we have a table alias defined, check if the query references a different alias
// This can happen in preloads where the user expects a certain alias but Bun generates another
query = normalizeTableAlias(query, b.tableAlias, b.tableName) query = normalizeTableAlias(query, b.tableAlias, b.tableName)
} }
b.query = b.query.Where(query, args...) b.query = b.query.Where(query, args...)
@@ -487,6 +490,30 @@ func normalizeTableAlias(query, expectedAlias, tableName string) string {
return modified return modified
} }
// replaceRelationAlias rewrites WHERE conditions written with a relation alias prefix
// (e.g. "TPRP.col") to use the real table name that bun uses in separate queries
// (e.g. "t_proposalinstance.col"). Only called for separate-query preload wrappers.
func replaceRelationAlias(query, relationAlias, tableName string) string {
if relationAlias == "" || tableName == "" || query == "" {
return query
}
parts := strings.FieldsFunc(query, func(r rune) bool {
return r == ' ' || r == '(' || r == ')' || r == ','
})
modified := query
for _, part := range parts {
if dotIndex := strings.Index(part, "."); dotIndex > 0 {
prefix := part[:dotIndex]
column := part[dotIndex+1:]
if strings.EqualFold(prefix, relationAlias) {
logger.Debug("Replacing relation alias '%s' with table name '%s' in preload WHERE condition", prefix, tableName)
modified = strings.ReplaceAll(modified, part, tableName+"."+column)
}
}
}
return modified
}
func isJoinKeyword(word string) bool { func isJoinKeyword(word string) bool {
switch strings.ToUpper(word) { switch strings.ToUpper(word) {
case "JOIN", "INNER", "LEFT", "RIGHT", "FULL", "OUTER", "CROSS": case "JOIN", "INNER", "LEFT", "RIGHT", "FULL", "OUTER", "CROSS":
@@ -676,8 +703,20 @@ func (b *BunSelectQuery) PreloadRelation(relation string, apply ...func(common.S
wrapper.tableAlias = provider.TableAlias() wrapper.tableAlias = provider.TableAlias()
logger.Debug("Preload relation '%s' using table alias: %s", relation, wrapper.tableAlias) logger.Debug("Preload relation '%s' using table alias: %s", relation, wrapper.tableAlias)
} }
} }
// Fallback: if the model didn't provide a table name, ask bun directly.
if wrapper.tableName == "" {
wrapper.schema, wrapper.tableName = parseTableName(sq.GetTableName(), b.driverName)
}
// For separate-query preloads (has-many), bun aliases the related table using
// the actual table name, not the relation name. Record the relation alias so
// Where() can rewrite conditions like "TPRP.col" to "t_proposalinstance.col".
wrapper.preloadRelationAlias = strings.ToLower(relation)
logger.Debug("Preload relation '%s' registered alias '%s' for separate-query WHERE rewriting", relation, wrapper.preloadRelationAlias)
// Start with the interface value (not pointer) // Start with the interface value (not pointer)
current := common.SelectQuery(wrapper) current := common.SelectQuery(wrapper)

View File

@@ -141,8 +141,12 @@ func (p *NestedCUDProcessor) ProcessNestedCUD(
logger.Debug("Skipping insert for %s - no data columns besides _request", tableName) logger.Debug("Skipping insert for %s - no data columns besides _request", tableName)
} }
case "update": case "update", "change":
// Only perform update if we have data to update // 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 { if hasData {
rows, err := p.processUpdate(ctx, regularData, tableName, data[pkName]) rows, err := p.processUpdate(ctx, regularData, tableName, data[pkName])
if err != nil { if err != nil {
@@ -171,10 +175,15 @@ func (p *NestedCUDProcessor) ProcessNestedCUD(
} }
case "delete": 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) // Process child relations first (for referential integrity)
if err := p.processChildRelations(ctx, "delete", data[pkName], relationFields, result.RelationData, modelType, parentIDs); err != nil { 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) logger.Error("Failed to process child relations before delete: table=%s, id=%v, relations=%+v, error=%v", tableName, data[pkName], relationFields, err)
return nil, fmt.Errorf("failed to process child relations before delete: %w", err) return nil, fmt.Errorf("failed to process child relations: %w", err)
} }
rows, err := p.processDelete(ctx, tableName, data[pkName]) rows, err := p.processDelete(ctx, tableName, data[pkName])

View File

@@ -51,6 +51,31 @@ func ExtractTableNameOnly(fullName string) string {
return fullName[startIndex:] 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. // 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 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. // If neither condition is met, it returns the original type.

View File

@@ -9,29 +9,29 @@ import (
"github.com/bitechdev/ResolveSpec/pkg/common" "github.com/bitechdev/ResolveSpec/pkg/common"
) )
// Test that normalizeResultArray returns empty array when no records found without ID // Test that normalizeResultArray returns empty object when no records found (single-record mode)
func TestNormalizeResultArray_EmptyArrayWhenNoID(t *testing.T) { func TestNormalizeResultArray_EmptyArrayWhenNoID(t *testing.T) {
handler := &Handler{} handler := &Handler{}
tests := []struct { tests := []struct {
name string name string
input interface{} input interface{}
shouldBeEmptyArr bool shouldBeEmptyObj bool
}{ }{
{ {
name: "nil should return empty array", name: "nil should return empty object",
input: nil, input: nil,
shouldBeEmptyArr: true, shouldBeEmptyObj: true,
}, },
{ {
name: "empty slice should return empty array", name: "empty slice should return empty object",
input: []*EmptyTestModel{}, input: []*EmptyTestModel{},
shouldBeEmptyArr: true, shouldBeEmptyObj: true,
}, },
{ {
name: "single element should return the element", name: "single element should return the element",
input: []*EmptyTestModel{{ID: 1, Name: "test"}}, input: []*EmptyTestModel{{ID: 1, Name: "test"}},
shouldBeEmptyArr: false, shouldBeEmptyObj: false,
}, },
{ {
name: "multiple elements should return the slice", name: "multiple elements should return the slice",
@@ -39,7 +39,7 @@ func TestNormalizeResultArray_EmptyArrayWhenNoID(t *testing.T) {
{ID: 1, Name: "test1"}, {ID: 1, Name: "test1"},
{ID: 2, Name: "test2"}, {ID: 2, Name: "test2"},
}, },
shouldBeEmptyArr: false, shouldBeEmptyObj: false,
}, },
} }
@@ -47,25 +47,25 @@ func TestNormalizeResultArray_EmptyArrayWhenNoID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := handler.normalizeResultArray(tt.input) result := handler.normalizeResultArray(tt.input)
// For cases that should return empty array // For cases that should return empty object
if tt.shouldBeEmptyArr { if tt.shouldBeEmptyObj {
emptyArr, ok := result.([]interface{}) emptyObj, ok := result.(map[string]interface{})
if !ok { if !ok {
t.Errorf("Expected empty array []interface{}{}, got %T: %v", result, result) t.Errorf("Expected empty object map[string]interface{}{}, got %T: %v", result, result)
return return
} }
if len(emptyArr) != 0 { if len(emptyObj) != 0 {
t.Errorf("Expected empty array with length 0, got length %d", len(emptyArr)) t.Errorf("Expected empty object with length 0, got length %d", len(emptyObj))
} }
// Verify it serializes to [] and not null // Verify it serializes to {} and not null
jsonBytes, err := json.Marshal(result) jsonBytes, err := json.Marshal(result)
if err != nil { if err != nil {
t.Errorf("Failed to marshal result: %v", err) t.Errorf("Failed to marshal result: %v", err)
return return
} }
if string(jsonBytes) != "[]" { if string(jsonBytes) != "{}" {
t.Errorf("Expected JSON '[]', got '%s'", string(jsonBytes)) t.Errorf("Expected JSON '{}', got '%s'", string(jsonBytes))
} }
} }
}) })
@@ -138,12 +138,12 @@ func TestSendResponseWithOptions_NoDataFoundHeader(t *testing.T) {
t.Errorf("Expected X-No-Data-Found header to be 'true', got '%s'", mockWriter.headers["X-No-Data-Found"]) t.Errorf("Expected X-No-Data-Found header to be 'true', got '%s'", mockWriter.headers["X-No-Data-Found"])
} }
// Check status code is 200 // Check status code is 200 even when no records found
if mockWriter.statusCode != 200 { if mockWriter.statusCode != 200 {
t.Errorf("Expected status code 200, got %d", mockWriter.statusCode) t.Errorf("Expected status code 200, got %d", mockWriter.statusCode)
} }
// Verify the body is an empty array // Verify the body is an empty array (list request, SingleRecordAsObject not set)
if mockWriter.body == nil { if mockWriter.body == nil {
t.Error("Expected body to be set, got nil") t.Error("Expected body to be set, got nil")
} else { } else {

View File

@@ -2502,14 +2502,12 @@ func (h *Handler) sendResponseWithOptions(w common.ResponseWriter, data interfac
w.SetHeader("X-No-Data-Found", "true") w.SetHeader("X-No-Data-Found", "true")
} }
w.WriteHeader(http.StatusOK)
// Normalize single-record arrays to objects if requested // Normalize single-record arrays to objects if requested
if options != nil && options.SingleRecordAsObject { if options != nil && options.SingleRecordAsObject {
data = h.normalizeResultArray(data) data = h.normalizeResultArray(data)
} }
// Return data as-is without wrapping in common.Response w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(data); err != nil { if err := w.WriteJSON(data); err != nil {
logger.Error("Failed to write JSON response: %v", err) logger.Error("Failed to write JSON response: %v", err)
@@ -2520,7 +2518,7 @@ func (h *Handler) sendResponseWithOptions(w common.ResponseWriter, data interfac
// Returns the single element if data is a slice/array with exactly one element, otherwise returns data unchanged // Returns the single element if data is a slice/array with exactly one element, otherwise returns data unchanged
func (h *Handler) normalizeResultArray(data interface{}) interface{} { func (h *Handler) normalizeResultArray(data interface{}) interface{} {
if data == nil { if data == nil {
return []interface{}{} return map[string]interface{}{}
} }
// Use reflection to check if data is a slice or array // Use reflection to check if data is a slice or array
@@ -2535,15 +2533,15 @@ func (h *Handler) normalizeResultArray(data interface{}) interface{} {
// Return the single element // Return the single element
return dataValue.Index(0).Interface() return dataValue.Index(0).Interface()
} else if dataValue.Len() == 0 { } else if dataValue.Len() == 0 {
// Keep empty array as empty array, don't convert to empty object // Single-record request with no result → empty object
return []interface{}{} return map[string]interface{}{}
} }
} }
if dataValue.Kind() == reflect.String { if dataValue.Kind() == reflect.String {
str := dataValue.String() str := dataValue.String()
if str == "" || str == "null" { if str == "" || str == "null" {
return []interface{}{} return map[string]interface{}{}
} }
} }
@@ -2552,9 +2550,6 @@ func (h *Handler) normalizeResultArray(data interface{}) interface{} {
// sendFormattedResponse sends response with formatting options // sendFormattedResponse sends response with formatting options
func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{}, metadata *common.Metadata, options ExtendedRequestOptions) { func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{}, metadata *common.Metadata, options ExtendedRequestOptions) {
// Normalize single-record arrays to objects if requested
httpStatus := http.StatusOK
// Handle nil data - convert to empty array // Handle nil data - convert to empty array
if data == nil { if data == nil {
data = []interface{}{} data = []interface{}{}
@@ -2591,7 +2586,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
switch options.ResponseFormat { switch options.ResponseFormat {
case "simple": case "simple":
// Simple format: just return the data array // Simple format: just return the data array
w.WriteHeader(httpStatus) w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(data); err != nil { if err := w.WriteJSON(data); err != nil {
logger.Error("Failed to write JSON response: %v", err) logger.Error("Failed to write JSON response: %v", err)
} }
@@ -2603,7 +2598,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
if metadata != nil { if metadata != nil {
response["count"] = metadata.Total response["count"] = metadata.Total
} }
w.WriteHeader(httpStatus) w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(response); err != nil { if err := w.WriteJSON(response); err != nil {
logger.Error("Failed to write JSON response: %v", err) logger.Error("Failed to write JSON response: %v", err)
} }
@@ -2614,7 +2609,7 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{
Data: data, Data: data,
Metadata: metadata, Metadata: metadata,
} }
w.WriteHeader(httpStatus) w.WriteHeader(http.StatusOK)
if err := w.WriteJSON(response); err != nil { if err := w.WriteJSON(response); err != nil {
logger.Error("Failed to write JSON response: %v", err) logger.Error("Failed to write JSON response: %v", err)
} }