private/apigen: Don't allow certain types for Request/Response
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
This commit is contained in:
parent
956109a097
commit
71c9547de5
@ -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
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,10 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user