Skip to content

Commit

Permalink
move function
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe committed Oct 4, 2024
1 parent 96e9f11 commit 42d62bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 19 additions & 1 deletion backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ingress

import (
"encoding/json"
"fmt"
"math/rand"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion backend/controller/timeline/timeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/TBD54566975/ftl/backend/controller/artefacts"
"io"
"net/http"
"net/url"
"reflect"
"testing"
"time"

"github.com/TBD54566975/ftl/backend/controller/artefacts"

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/optional"

Expand Down

0 comments on commit 42d62bf

Please sign in to comment.