Skip to content

Commit

Permalink
fix: handle transformed ingress query params (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Feb 1, 2024
1 parent 3b36e78 commit 44d1479
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
62 changes: 25 additions & 37 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,66 +90,54 @@ func ValidateAndExtractRequestBody(route *dal.IngressRoute, r *http.Request, sch
return nil, fmt.Errorf("unknown verb %s", route.Verb)
}

var request *schema.DataRef
request, ok := verb.Request.(*schema.DataRef)
if !ok {
return nil, fmt.Errorf("verb %s input must be a data structure", verb.Name)
}

var body []byte

var requestMap map[string]any

if metadata, ok := verb.GetMetadataIngress().Get(); ok && metadata.Type == "http" {
pathParameters := map[string]string{}
matchSegments(route.Path, r.URL.Path, func(segment, value string) {
pathParameters[segment] = value
})

request, ok = verb.Request.(*schema.DataRef)
if !ok {
return nil, fmt.Errorf("verb %s input must be a data structure", verb.Name)
}

httpRequestBody, err := extractHTTPRequestBody(route, r, request, sch)
if err != nil {
return nil, err
}

requestMap := map[string]any{}
requestMap = map[string]any{}
requestMap["method"] = r.Method
requestMap["path"] = r.URL.Path
requestMap["pathParameters"] = pathParameters
requestMap["query"] = r.URL.Query()
requestMap["headers"] = r.Header
requestMap["body"] = httpRequestBody

requestMap, err = transformFromAliasedFields(request, sch, requestMap)
if err != nil {
return nil, err
}

err = validateRequestMap(request, []string{request.String()}, requestMap, sch)
if err != nil {
return nil, err
}

body, err = json.Marshal(requestMap)
if err != nil {
return nil, err
}
} else {
request, ok = verb.Request.(*schema.DataRef)
if !ok {
return nil, fmt.Errorf("verb %s input must be a data structure", verb.Name)
}

requestMap, err := buildRequestMap(route, r, request, sch)
var err error
requestMap, err = buildRequestMap(route, r, request, sch)
if err != nil {
return nil, err
}
}

err = validateRequestMap(request, []string{verb.Request.String()}, requestMap, sch)
if err != nil {
return nil, err
}
requestMap, err := transformFromAliasedFields(request, sch, requestMap)
if err != nil {
return nil, err
}

body, err = json.Marshal(requestMap)
if err != nil {
return nil, err
}
err = validateRequestMap(request, []string{request.String()}, requestMap, sch)
if err != nil {
return nil, err
}

body, err = json.Marshal(requestMap)
if err != nil {
return nil, err
}

return body, nil
Expand Down Expand Up @@ -453,7 +441,7 @@ func parseQueryParams(values url.Values, data *schema.Data) (map[string]any, err

var field *schema.Field
for _, f := range data.Fields {
if f.Name == key {
if (f.Alias != "" && f.Alias == key) || f.Name == key {
field = f
}
for _, typeParam := range data.TypeParameters {
Expand Down
7 changes: 6 additions & 1 deletion examples/go/httpingress/httpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"ftl/builtin"
)

type Nested struct {
GoodStuff string `alias:"good_stuff"`
}

type GetRequest struct {
UserID string `alias:"userId"`
PostID string `alias:"postId"`
}

type GetResponse struct {
Message string `alias:"random"`
Nested Nested `alias:"nested"`
}

// Example: curl -i http://localhost:8892/ingress/http/users/123/posts?postId=456
Expand All @@ -25,7 +30,7 @@ func Get(ctx context.Context, req builtin.HttpRequest[GetRequest]) (builtin.Http
return builtin.HttpResponse[GetResponse]{
Status: 200,
Headers: map[string][]string{"Get": {"Header from FTL"}},
Body: GetResponse{Message: fmt.Sprintf("UserID, %s : PostID %s", req.Body.UserID, req.Body.PostID)},
Body: GetResponse{Message: fmt.Sprintf("UserID, %s : PostID %s", req.Body.UserID, req.Body.PostID), Nested: Nested{GoodStuff: "Nested Good Stuff"}},
}, nil
}

Expand Down

0 comments on commit 44d1479

Please sign in to comment.