GoogleCloudPlatform / go-endpoints

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

Path parameters inconsistently handle JSON name overrides, "req[uired]" tag. #132

Open danjacques opened 8 years ago

danjacques commented 8 years ago

Path parameter names for fields with JSON name overrides are inconsistently handled by go-endpoints. In apiconfig.go, fieldNames will honor JSON name overrides, including ignoring fields with JSON name -. This affects the exported parameters field for a given function. Additionally, parseTag only recognizes the endpoints tag, "req".

On the other hand, when exporting path parameters from a service struct, service.go's requiredParamNames directly exports the field name as the path parameter and only recognizes the endpoints tag, "required".

Example code follows:

package frontend

import (
        "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
        "golang.org/x/net/context"
)

type Request struct {
        Path string `json:"myPath" endpoints:"required"`
}

type Service struct{}

func (s *Service) DoThing(c context.Context, r *Request) error { return nil }

func init() {
        _, err := endpoints.RegisterService(&Service{}, "service", "v1", "Test Service", true)
        if err != nil {
                panic(err)
        }

        endpoints.HandleHTTP()
}

The resulting backend JSON:

...
"service.dothing": {
     "httpMethod": "GET", 
     "path": "dothing/{Path}", 
     "request": {
      "parameters": {
       "myPath": {
        "type": "string"
       }
     },...

Which generates the invalid discovery JSON:

"dothing": {
   "id": "service.dothing",
   "path": "{Path}",
   "httpMethod": "GET",
   "parameters": {
    "myPath": {
     "type": "string",
     "location": "query"
    }
   },
   "parameterOrder": [
    "Path"
   ]

Note that the parameter name is "Path", but the parameter specification is for "myPath". Additionally, "myPath" is exported as a query parameter b/c of the inconsistent "req[uired]" handling.