From 42d62bf6e11a166fd38e8c34c0d0f5d9406ef3d1 Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Fri, 4 Oct 2024 14:24:39 -0500 Subject: [PATCH] move function --- backend/controller/controller.go | 20 +++++++++++++++++++- backend/controller/ingress/ingress.go | 19 ------------------- backend/controller/timeline/timeline_test.go | 3 ++- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/backend/controller/controller.go b/backend/controller/controller.go index bbdc451fe1..9c2a141b46 100644 --- a/backend/controller/controller.go +++ b/backend/controller/controller.go @@ -1032,7 +1032,7 @@ func (s *Service) callWithRequest( return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("verb %q is not exported", verbRef)) } - err = ingress.ValidateCallBody(req.Msg.Body, verb, sch) + err = validateCallBody(req.Msg.Body, verb, sch) if err != nil { observability.Calls.Request(ctx, req.Msg.Verb, start, optional.Some("invalid request: invalid call body")) return nil, err @@ -1906,6 +1906,24 @@ func makeBackoff(min, max time.Duration) backoff.Backoff { } } +func validateCallBody(body []byte, verb *schema.Verb, sch *schema.Schema) error { + var root any + err := json.Unmarshal(body, &root) + if err != nil { + return fmt.Errorf("request body is not valid JSON: %w", err) + } + + 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) + } + return nil +} + type Route struct { Module string Deployment model.DeploymentKey diff --git a/backend/controller/ingress/ingress.go b/backend/controller/ingress/ingress.go index d1fe8388c1..5a48025956 100644 --- a/backend/controller/ingress/ingress.go +++ b/backend/controller/ingress/ingress.go @@ -1,7 +1,6 @@ package ingress import ( - "encoding/json" "fmt" "math/rand" "strings" @@ -49,24 +48,6 @@ func matchSegments(pattern, urlPath string, onMatch func(segment, value string)) return true } -func ValidateCallBody(body []byte, verb *schema.Verb, sch *schema.Schema) error { - var root any - err := json.Unmarshal(body, &root) - if err != nil { - return fmt.Errorf("request body is not valid JSON: %w", err) - } - - 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) - } - return nil -} - func getField(name string, ref *schema.Ref, sch *schema.Schema) (*schema.Field, error) { data, err := sch.ResolveMonomorphised(ref) if err != nil { diff --git a/backend/controller/timeline/timeline_test.go b/backend/controller/timeline/timeline_test.go index d33913afc6..5821171d8c 100644 --- a/backend/controller/timeline/timeline_test.go +++ b/backend/controller/timeline/timeline_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "github.com/TBD54566975/ftl/backend/controller/artefacts" "io" "net/http" "net/url" @@ -12,6 +11,8 @@ import ( "testing" "time" + "github.com/TBD54566975/ftl/backend/controller/artefacts" + "github.com/alecthomas/assert/v2" "github.com/alecthomas/types/optional"