GoogleCloudPlatform / go-endpoints

Cloud Endpoints for Go
https://go-endpoints.appspot.com
Apache License 2.0
255 stars 56 forks source link

Field tags not working #72

Open robskie opened 9 years ago

robskie commented 9 years ago

Hi!

I'm trying to compare the behavior of python endpoints messages to request field tags, and I'm getting different results.

In python endpoints, when a required field does not have a value, the response would be a 400 Bad Request Error (and the service method would not be executed) while in the current implementation I'm getting an OK response (the service method is executed).

The default, min, and max tags are also not working.

campoy commented 9 years ago

Hi @robskie,

Could you provide some code pointing out the expected behavior and what you see instead?

Thanks

robskie commented 9 years ago

Sure. For example, I have this in python endpoints:

class RequestMessage(messages.Message):
    default_field = messages.IntegerField(1, default=3)
    required_field = messages.IntegerField(2, required=True)

@endpoints.api(name='fieldTest', version='v1')
class FieldTest(remote.Service):

    @endpoints.method(RequestMessage, message_types.VoidMessage,
                                      path='fields', http_method='GET', 
                                      name='fields.test')
    def fields_test(self, request):
        """Do something here"""
        return

And this in go endpoints:

type RequestMessage struct {
    DefaultField    int `json:"default_field" endpoints:"d=3"`
    RequiredField   int `json:"required_field" endpoints:"req"`
    RangeField      int `json:"range_field" endpoints:"d=5,min=1,max=10"`
}

func (fs *FieldTestService) Test(c context.Context, r *RequestMessage)  error {
    // Do something here
    return nil
}

I expected that go would have the same behavior as that of python endpoints, but I'm getting the following results:

Python endpoints doesn't have the min and max options, but I expect that the range_field would always be in the range [1, 10] when the method is executed otherwise, a Bad Request Error should be generated if the request value is not within the range. I also expect that an error be produced if the default value is not in the min-max range.

campoy commented 9 years ago

First attempt to fix this: https://github.com/GoogleCloudPlatform/go-endpoints/pull/87/files

For now it only adds "req" and "default". Will do "min" and "max" once this is merged.

campoy commented 9 years ago

Added support for min and max with https://github.com/GoogleCloudPlatform/go-endpoints/pull/95

enj commented 9 years ago

Does "req" not work with nested structs? The following does not throw an error when {"a":"s"} is sent as a Thing request value (I would think t and t.value would be required).

type Tester struct {
    Value string `json:"value" endpoints:"req"`
}

type Thing struct {
    A string `json:"a" endpoints:"req"`
    T Tester `json:"t" endpoints:"req"`
}