Skip to content

Commit

Permalink
private/apigen: Don't allow certain types for Request/Response
Browse files Browse the repository at this point in the history
We cannot map certain types to TypeScript, hence we verify them in the
Endpoint.Validate method.

Even some types could be somehow mapped, we don't want to add more
complexity or allow types that don't make sense to be for a request or
response.

Change-Id: I51ecee286e637b1160e967d77f9ce6c7403ddfdc
  • Loading branch information
ifraixedes committed Oct 4, 2023
1 parent 956109a commit 71c9547
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
30 changes: 30 additions & 0 deletions private/apigen/endpoint.go
Expand Up @@ -81,6 +81,36 @@ func (e *Endpoint) Validate() error {
return errsEndpoint.New("TypeScriptName doesn't match the regular expression %q", typeScriptNameRegExp)
}

if e.Request != nil {
switch k := reflect.TypeOf(e.Request).Kind(); k {
case reflect.Invalid,
reflect.Complex64,
reflect.Complex128,
reflect.Chan,
reflect.Func,
reflect.Interface,
reflect.Map,
reflect.Pointer,
reflect.UnsafePointer:
return errsEndpoint.New("Request cannot be of a type %q", k)
}
}

if e.Response != nil {
switch k := reflect.TypeOf(e.Response).Kind(); k {
case reflect.Invalid,
reflect.Complex64,
reflect.Complex128,
reflect.Chan,
reflect.Func,
reflect.Interface,
reflect.Map,
reflect.Pointer,
reflect.UnsafePointer:
return errsEndpoint.New("Response cannot be of a type %q", k)
}
}

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions private/apigen/endpoint_test.go
Expand Up @@ -4,8 +4,10 @@
package apigen

import (
"fmt"
"math/rand"
"net/http"
"reflect"
"strconv"
"testing"

Expand Down Expand Up @@ -104,6 +106,27 @@ func TestEndpoint_Validate(t *testing.T) {
},
errMsg: "TypeScriptName doesn't match the regular expression",
},
{
testName: "invalid Request type",
endpointFn: func() *Endpoint {
request := &struct {
Name string `json:"name"`
}{}
e := validEndpoint
e.Request = request
return &e
},
errMsg: fmt.Sprintf("Request cannot be of a type %q", reflect.Pointer),
},
{
testName: "invalid Response type",
endpointFn: func() *Endpoint {
e := validEndpoint
e.Response = map[string]string{}
return &e
},
errMsg: fmt.Sprintf("Response cannot be of a type %q", reflect.Map),
},
}

for _, tc := range tcases {
Expand Down

0 comments on commit 71c9547

Please sign in to comment.