Skip to content

Commit

Permalink
chore: add integration test for omitempty encoding at ingress (#1450)
Browse files Browse the repository at this point in the history
Addresses
#1441 (comment)

Tested with `go test --tags=integration ./integration/... -run
TestHTTPEncodeOmitempty`
  • Loading branch information
deniseli authored May 9, 2024
1 parent a1631ce commit af7a19a
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go-runtime/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func TestMarshal(t *testing.T) {
ShouldntOmit string `json:""`
NotTagged string
}
type validateOmitemptyOption struct {
ShouldOmit ftl.Option[string] `json:",omitempty"`
ShouldntOmit ftl.Option[string] `json:""`
NotTagged ftl.Option[string]
}
tests := []struct {
name string
input any
Expand Down Expand Up @@ -65,6 +70,11 @@ func TestMarshal(t *testing.T) {
{name: "UnregisteredSumType", input: struct{ D unregistered }{variant{"hello"}}, err: `the only supported interface types are enums or any, not encoding_test.unregistered`},
{name: "OmitEmptyNotNull", input: validateOmitempty{"foo", "bar", "baz"}, expected: `{"shouldOmit":"foo","shouldntOmit":"bar","notTagged":"baz"}`},
{name: "OmitEmptyNull", input: validateOmitempty{}, expected: `{"shouldntOmit":"","notTagged":""}`},
{name: "OmitEmptyOptionNone", input: validateOmitemptyOption{
ShouldOmit: ftl.None[string](),
ShouldntOmit: ftl.None[string](),
NotTagged: ftl.None[string](),
}, expected: `{"shouldntOmit":null,"notTagged":null}`},
}

tr := typeregistry.NewTypeRegistry()
Expand Down
11 changes: 11 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ func TestSchemaGenerate(t *testing.T) {
)
}

func TestHttpEncodeOmitempty(t *testing.T) {
run(t,
copyModule("omitempty"),
deploy("omitempty"),
httpCall(http.MethodGet, "/get", jsonData(t, obj{}), func(resp *httpResponse) error {
assert.Equal(t, "{\"mustset\":\"\"}", repr.String(resp.jsonBody))
return nil
}),
)
}

func TestHttpIngress(t *testing.T) {
run(t,
copyModule("httpingress"),
Expand Down
2 changes: 2 additions & 0 deletions integration/testdata/go/omitempty/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "omitempty"
language = "go"
43 changes: 43 additions & 0 deletions integration/testdata/go/omitempty/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module ftl/omitempty

go 1.22.2

require github.com/TBD54566975/ftl v0.206.1

require (
connectrpc.com/connect v1.16.1 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.14.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
)

replace github.com/TBD54566975/ftl => ../../../..
136 changes: 136 additions & 0 deletions integration/testdata/go/omitempty/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions integration/testdata/go/omitempty/omitempty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package omitempty

import (
"context"
"fmt"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
)

type EchoRequest struct{}

type EchoResponse struct {
Error string `json:"error,omitempty"` // Should be omitted from marshaled JSON
MustSet string `json:"mustset"` // Should marshal to `"mustset":""`
}

//ftl:ingress http GET /get
func Get(ctx context.Context, req builtin.HttpRequest[EchoRequest]) (builtin.HttpResponse[EchoResponse, string], error) {
return EchoResponse{}, nil
}

0 comments on commit af7a19a

Please sign in to comment.