Skip to content

Commit

Permalink
feat: add directive to enable lenient json
Browse files Browse the repository at this point in the history
fixes #2132
  • Loading branch information
worstell committed Aug 15, 2024
1 parent acb7f5d commit 93c6c6a
Show file tree
Hide file tree
Showing 19 changed files with 897 additions and 515 deletions.
7 changes: 6 additions & 1 deletion backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func ValidateCallBody(body []byte, verb *schema.Verb, sch *schema.Schema) error
if err != nil {
return fmt.Errorf("request body is not valid JSON: %w", err)
}
err = schema.ValidateJSONValue(verb.Request, []string{verb.Request.String()}, root, sch)

var opts []schema.EncodingOption
if e, ok := slices.FindVariant[*schema.MetadataEncoding](verb.Metadata); ok && e.Lenient {
opts = append(opts, schema.LenientMode())
}
err = schema.ValidateJSONValue(verb.Request, []string{verb.Request.String()}, root, sch, opts...)
if err != nil {
return fmt.Errorf("could not validate HTTP request body: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion backend/controller/ingress/ingress_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/repr"

in "github.com/TBD54566975/ftl/integration"
)
Expand Down Expand Up @@ -144,6 +143,14 @@ func TestHttpIngress(t *testing.T) {
assert.Equal(t, []string{"application/json; charset=utf-8"}, resp.Headers["Content-Type"])
assert.Equal(t, in.JsonData(t, in.Obj{"Message": "hello"}), resp.BodyBytes)
}),
// not lenient
in.HttpCall(http.MethodPost, "/users", nil, in.JsonData(t, in.Obj{"user_id": 123, "postId": 345, "extra": "blah"}), func(t testing.TB, resp *in.HTTPResponse) {
assert.Equal(t, 400, resp.Status)
}),
// lenient
in.HttpCall(http.MethodPost, "/lenient", nil, in.JsonData(t, in.Obj{"user_id": 123, "postId": 345, "extra": "blah"}), func(t testing.TB, resp *in.HTTPResponse) {
assert.Equal(t, 201, resp.Status)
}),
)
}

Expand Down
7 changes: 6 additions & 1 deletion backend/controller/ingress/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/TBD54566975/ftl/backend/controller/dal"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/slices"
)

// BuildRequestBody extracts the HttpRequest body from an HTTP request.
Expand Down Expand Up @@ -84,7 +85,11 @@ func BuildRequestBody(route *dal.IngressRoute, r *http.Request, sch *schema.Sche
return nil, err
}

err = schema.ValidateRequestMap(request, []string{request.String()}, requestMap, sch)
var opts []schema.EncodingOption
if e, ok := slices.FindVariant[*schema.MetadataEncoding](verb.Metadata); ok && e.Lenient {
opts = append(opts, schema.LenientMode())
}
err = schema.ValidateRequestMap(request, []string{request.String()}, requestMap, sch, opts...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions backend/controller/ingress/testdata/go/httpingress/httpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,13 @@ type DirectTypeAlias = lib.NonFTLType
func External2(ctx context.Context, req builtin.HttpRequest[DirectTypeAlias]) (builtin.HttpResponse[DirectTypeAlias, string], error) {
return builtin.HttpResponse[DirectTypeAlias, string]{Body: ftl.Some(req.Body)}, nil
}

//ftl:ingress http POST /lenient
//ftl:encoding lenient
func Lenient(ctx context.Context, req builtin.HttpRequest[PostRequest]) (builtin.HttpResponse[PostResponse, string], error) {
return builtin.HttpResponse[PostResponse, string]{
Status: 201,
Headers: map[string][]string{"Post": {"Header from FTL"}},
Body: ftl.Some(PostResponse{Success: true}),
}, nil
}
Loading

0 comments on commit 93c6c6a

Please sign in to comment.