Skip to content

Commit

Permalink
fix: transform aliases transitively (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell authored Jan 26, 2024
1 parent 1d8c5a6 commit 8f126ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
28 changes: 26 additions & 2 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func ValidateAndExtractRequestBody(route *dal.IngressRoute, r *http.Request, sch
requestMap["headers"] = r.Header
requestMap["body"] = bodyMap

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

err = validateRequestMap(request, []string{request.String()}, requestMap, sch)
if err != nil {
return nil, err
Expand Down Expand Up @@ -197,7 +202,6 @@ func buildRequest(route *dal.IngressRoute, r *http.Request, dataRef *schema.Data
requestMap[key] = value
}
}
transformAliasedFields(dataRef, sch, requestMap)

return requestMap, nil
}
Expand Down Expand Up @@ -452,12 +456,32 @@ func hasInvalidQueryChars(s string) bool {
return strings.ContainsAny(s, "{}[]|\\^`")
}

func transformAliasedFields(dataRef *schema.DataRef, sch *schema.Schema, request map[string]any) {
func transformAliasedFields(dataRef *schema.DataRef, sch *schema.Schema, request map[string]any) (map[string]any, error) {
data := sch.ResolveDataRef(dataRef)
if len(dataRef.TypeParameters) > 0 {
var err error
data, err = data.Monomorphise(dataRef.TypeParameters...)
if err != nil {
return nil, err
}
}

for _, field := range data.Fields {
if _, ok := request[field.Name]; !ok && field.Alias != "" {
request[field.Name] = request[field.Alias]
delete(request, field.Alias)
}

if d, ok := field.Type.(*schema.DataRef); ok {
if _, found := request[field.Name]; found {
rMap, err := transformAliasedFields(d, sch, request[field.Name].(map[string]any))
if err != nil {
return nil, err
}
request[field.Name] = rMap
}
}
}

return request, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import xyz.block.ftl.Context
import xyz.block.ftl.Ingress
import xyz.block.ftl.Method
import xyz.block.ftl.Verb
import xyz.block.ftl.Alias

class InvalidInput(val field: String) : Exception()

data class EchoRequest(val name: String?, @Alias("m") val metadata: String)
data class EchoRequest(val name: String?)
data class EchoResponse(val message: String)

class Echo {
@Throws(InvalidInput::class)
@Verb
@Ingress(Method.POST, "/echo")
@Ingress(Method.GET, "/echo")
fun echo(context: Context, req: EchoRequest): EchoResponse {
val response = context.call(TimeModuleClient::time, TimeRequest)
return EchoResponse(message = "Hello, ${req.name ?: "anonymous"}! The time is ${response.time}. Metadata: ${req.metadata}")
return EchoResponse(message = "Hello, ${req.name ?: "anonymous"}! The time is ${response.time}.")
}
}
2 changes: 1 addition & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestIntegration(t *testing.T) {
deploymentExists("echo"),
}},
{name: "CallEchoKotlin", assertions: assertions{
call("echo", "echo", obj{"name": "Alice", "metadata": "hi"}, func(t testing.TB, resp obj) {
call("echo", "echo", obj{"name": "Alice"}, func(t testing.TB, resp obj) {
message, ok := resp["message"].(string)
assert.True(t, ok, "message is not a string")
assert.True(t, regexp.MustCompile(`^Hello, Alice!`).MatchString(message), "%q does not match %q", message, `^Hello, Alice!`)
Expand Down

0 comments on commit 8f126ed

Please sign in to comment.