From 44d1479901a712219c6ae7e9c8e835e70e7db7e0 Mon Sep 17 00:00:00 2001 From: Wes Date: Wed, 31 Jan 2024 20:22:00 -0700 Subject: [PATCH] fix: handle transformed ingress query params (#864) --- backend/controller/ingress/ingress.go | 62 +++++++++++--------------- examples/go/httpingress/httpingress.go | 7 ++- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/backend/controller/ingress/ingress.go b/backend/controller/ingress/ingress.go index 262e48171d..6a5759431b 100644 --- a/backend/controller/ingress/ingress.go +++ b/backend/controller/ingress/ingress.go @@ -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 @@ -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 { diff --git a/examples/go/httpingress/httpingress.go b/examples/go/httpingress/httpingress.go index b55a31f83d..8cc75561ba 100644 --- a/examples/go/httpingress/httpingress.go +++ b/examples/go/httpingress/httpingress.go @@ -8,6 +8,10 @@ import ( "ftl/builtin" ) +type Nested struct { + GoodStuff string `alias:"good_stuff"` +} + type GetRequest struct { UserID string `alias:"userId"` PostID string `alias:"postId"` @@ -15,6 +19,7 @@ type GetRequest struct { type GetResponse struct { Message string `alias:"random"` + Nested Nested `alias:"nested"` } // Example: curl -i http://localhost:8892/ingress/http/users/123/posts?postId=456 @@ -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 }