Skip to content

Commit

Permalink
private/apigen: Validate param types
Browse files Browse the repository at this point in the history
The Go generator only supports certain types as query and path
parameters and it panics on any an unsupported type.

The Document and TypeScript generator don't have any validation for
them. TypeScript generator generates code that compiles, however, it
won't work properly with all the types not supported by the Go
generator.

Because it doesn't make sense that some types may work on the TypeScript
generator, while the Go generator doesn't, doing the validation in the
Param constructor is better because it reports the issue without having
to run the Go generator and it gives a more understanding panic message.

TypeScript generator generates code that works properly with all the
types supported by the Go generator, hence, there isn't any change int
he TypeScript generator in this commit.

Change-Id: I03085283942a98341726a1560f511a46540df9f5
  • Loading branch information
ifraixedes authored and Storj Robot committed Oct 10, 2023
1 parent 0db898b commit 386a978
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions private/apigen/endpoint.go
Expand Up @@ -9,8 +9,11 @@ import (
"reflect"
"regexp"
"strings"
"time"

"github.com/zeebo/errs"

"storj.io/common/uuid"
)

var (
Expand Down Expand Up @@ -282,6 +285,24 @@ type Param struct {

// NewParam constructor which creates new Param entity by given name and type.
func NewParam(name string, instance interface{}) Param {
switch t := reflect.TypeOf(instance); t {
case reflect.TypeOf(uuid.UUID{}), reflect.TypeOf(time.Time{}):
default:
switch k := t.Kind(); k {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String:
default:
panic(
fmt.Sprintf(
`Unsupported parameter, only types: %q, %q, string, and "unsigned numbers" are supported . Found type=%q, Kind=%q`,
reflect.TypeOf(uuid.UUID{}),
reflect.TypeOf(time.Time{}),
t,
k,
),
)
}
}

return Param{
Name: name,
Type: reflect.TypeOf(instance),
Expand Down

0 comments on commit 386a978

Please sign in to comment.