GoogleCloudPlatform / go-endpoints

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

APIs having `map` data type in the return #103

Open wiless opened 9 years ago

wiless commented 9 years ago

The endpoint discovery fails, if the return type of a method has structure with fields of type map. Even if is of basic type map[int]string. Would be better to have better error messages.

campoy commented 9 years ago

The documentation specifies that the returned value needs to be a pointer to struct.

If you need to return a map you should return define some type:

type Response struct {
   M map[int]string
}

And then define your method as returning that response:

func (s *Svc) SomeMethod(c context.Context, r *Request) (*Response, error) { ... }
wiless commented 9 years ago

The return object was a structure but it had a field of Kind map.

campoy commented 9 years ago

Please provide code that causes the problem.

wiless commented 9 years ago

I tested it on the helloworld.go

replaced

// GreetingsList is the response type for GreetingService.List.
type GreetingsList struct {
    Items []*Greeting `json:"items"`
}

with

// GreetingsList is the response type for GreetingService.List.
type GreetingsList struct {
    Items []*Greeting `json:"items"`
    Dummy map[int]string
}

and if you visit http://localhost:8080/_ah/api/discovery/v1/apis return this error {"error": {"message": "BackendService.getApiConfigs Error"}}

I probably think it is because of the inability of json to Marshal/Unmarshal map types.

etherealmachine commented 9 years ago

It's actually a problem with the discovery api generation. In apiconfig.go, line 269, Alex's TODO needs to be fixed. Currently the code spits out an invalid API schema that consumers cannot parse. I would be willing to help, but I can't find documentation on the expected schema.