Skip to content

JobListParams: OrderByFinalizedAt guard is inverted — rejects valid finalized-only filter, silently accepts meaningless non-finalized sort #1326

Description

@tsushanth

Bug

JobListParams.toDBParams() in job_list_params.go has an inverted guard when validating state filters for JobListOrderByFinalizedAt.

Affected code (line ~244)

if p.sortField == JobListOrderByFinalizedAt {
    currentNonFinalizedStates := make([]rivertype.JobState, 0, len(p.states))
    for _, state := range p.states {
        switch state {
        case rivertype.JobStateAvailable, rivertype.JobStatePending, ...:
            currentNonFinalizedStates = append(currentNonFinalizedStates, state)
        case rivertype.JobStateCancelled, rivertype.JobStateCompleted, rivertype.JobStateDiscarded:
        }
    }
    // Comment says: "user overrode the States list with only non-finalized states"
    if len(currentNonFinalizedStates) == 0 {   // ← INVERTED
        return nil, fmt.Errorf("cannot order by finalized_at with non-finalized state filters %+v",
            currentNonFinalizedStates)
    }
}

What's wrong

The comment says the guard is checking for "only non-finalized states" — but the condition len(currentNonFinalizedStates) == 0 is true when there are zero non-finalized states, meaning all states are finalized. That's actually the correct usage for JobListOrderByFinalizedAt.

The inverted conditions:

States passed currentNonFinalizedStates len == 0 Result
Only finalized (e.g. Cancelled) empty true ❌ Error returned (valid usage blocked)
Only non-finalized (e.g. Available) [Available] false ✅ No error (meaningless sort allowed)

When a caller passes only non-finalized states and orders by finalized_at, every result row has finalized_at = NULL — the sort is meaningless. But no error is returned. The one case that should work (all finalized states) is incorrectly rejected.

Additionally, the error message prints currentNonFinalizedStates (the empty slice []) rather than the actual states being used, making the message uninformative.

Suggested fix

if len(currentNonFinalizedStates) == len(p.states) && len(p.states) > 0 {
    return nil, fmt.Errorf("cannot order by finalized_at with only non-finalized state filters %+v",
        currentNonFinalizedStates)
}

This fires when ALL states are non-finalized (the truly invalid case) and prints the actual problematic states in the error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions