Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

// apiJSONMarshaler returns the JSON marshaler for the gRPC gateway REST API.
// EmitUnpopulated ensures list responses include empty collections and strings
// (e.g. {"records":[],"next_page_token":""}) per the OpenAPI spec.
func apiJSONMarshaler() *runtime.JSONPb {
return &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}
}

// profilingServerAddress returns the address to bind the profiling server to.
// The profiling server is always bound to loopback (127.0.0.1) to prevent
// unauthenticated access from outside the pod.
Expand Down Expand Up @@ -239,14 +254,7 @@ func main() {

// Create the authorization authCheck
var authCheck auth.Checker
serverMuxOptions := []runtime.ServeMuxOption{runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
})}
serverMuxOptions := []runtime.ServeMuxOption{runtime.WithMarshalerOption(runtime.MIMEWildcard, apiJSONMarshaler())}
if serverConfig.AUTH_DISABLE {
log.Warn("Kubernetes RBAC authorization check disabled - all requests will be allowed by the API server")
authCheck = &auth.AllowAll{}
Expand Down
44 changes: 44 additions & 0 deletions cmd/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"bytes"
"context"
"encoding/json"
"io"
"strings"
"testing"

"github.com/golang-jwt/jwt/v4"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
v1alpha2pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -68,6 +70,48 @@ func contextWithLogger(w io.Writer) context.Context {
return ctxzap.ToContext(context.Background(), logger)
}

func TestAPIJSONMarshaler_EmptyListRecordsResponse(t *testing.T) {
m := apiJSONMarshaler()
got, err := m.Marshal(&v1alpha2pb.ListRecordsResponse{})
if err != nil {
t.Fatalf("Marshal(): %v", err)
}
var parsed map[string]json.RawMessage
if err := json.Unmarshal(got, &parsed); err != nil {
t.Fatalf("json.Unmarshal(): %v", err)
}
if _, ok := parsed["records"]; !ok {
t.Fatalf("missing records field in %s", got)
}
if string(parsed["records"]) != "[]" {
t.Fatalf("records = %s, want []", parsed["records"])
}
if string(parsed["next_page_token"]) != `""` {
t.Fatalf("next_page_token = %s, want \"\"", parsed["next_page_token"])
}
}

func TestAPIJSONMarshaler_EmptyListResultsResponse(t *testing.T) {
m := apiJSONMarshaler()
got, err := m.Marshal(&v1alpha2pb.ListResultsResponse{})
if err != nil {
t.Fatalf("Marshal(): %v", err)
}
var parsed map[string]json.RawMessage
if err := json.Unmarshal(got, &parsed); err != nil {
t.Fatalf("json.Unmarshal(): %v", err)
}
if _, ok := parsed["results"]; !ok {
t.Fatalf("missing results field in %s", got)
}
if string(parsed["results"]) != "[]" {
t.Fatalf("results = %s, want []", parsed["results"])
}
if string(parsed["next_page_token"]) != `""` {
t.Fatalf("next_page_token = %s, want \"\"", parsed["next_page_token"])
}
}

func Test_profilingServerAddress(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading