API: prevent "zero" timestamp on ticket creation

In Go, an un-initialized `time.Time` has a value of "0" which the
GraphQL server will accept (as it is not `nil`) and PostgreSQL will
write it to the database. However, the API will afterwards fail to
return the creation date for this ticket, as the value is considered
null, but the ticket type defined to not allow null.

As a first step, prevent clients from creating tickets with the "0"
timestamp by adding a validation that checks the value.

References: https://todo.sr.ht/~sircmpwn/todo.sr.ht/259
This commit is contained in:
Conrad Hoffmann 2022-04-04 15:17:48 +02:00 committed by Drew DeVault
parent bb414aae31
commit c28e447d07
1 changed files with 5 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"strings"
"time"
"git.sr.ht/~sircmpwn/core-go/auth"
"git.sr.ht/~sircmpwn/core-go/config"
@ -690,6 +691,10 @@ func (r *mutationResolver) SubmitTicket(ctx context.Context, trackerID int, inpu
validation.Expect(tracker.OwnerID == user.UserID,
"Cannot configure creation time unless you are the owner of this tracker").
WithField("created")
var zeroDate time.Time
validation.Expect(*input.Created != zeroDate,
"Cannot use zero value for creation time").
WithField("created")
}
if !validation.Ok() {
return nil, nil