utrack / clay

Proto-first minimal server platform for gRPС+REST+Swagger APIs
MIT License
290 stars 37 forks source link

Fix unmarshal http request body into specified field #10

Closed zyukin closed 6 years ago

zyukin commented 6 years ago

Fix for issue #9

utrack commented 6 years ago

There's a panic when processing the request - check branch pr-10 for breakage example (try to regenerate proto for /doc/example/pb and run /doc/example).

Struct fields should be initiated before unmarhalling into them - in.B = &BStruct{} would fix the panic.

Also, code doesn't compile if body tag points to a primitive type, i.e. int64 - it does not implement proto.Message interface.

utrack commented 6 years ago

Thank you! Primitive types still don't work, but we can file it as a separate bug. Can you fix the HTTPClient vs new rules, please? https://github.com/zyukin/clay/blob/e0cd213da9bf9653f5a97641611d69774146f760/cmd/protoc-gen-goclay/genhandler/template.go#L357

zyukin commented 6 years ago

Primitive types works fine for this example:

service Summator {
  rpc Sum(SumRequest) returns (SumResponse) {
    option (google.api.http) = {
                                post: "/v1/example/sum/{a}"
                                body: "b"
    };
  }
}
message SumRequest {
  int64 a = 1;
  int64 b = 2;
}
zyukin commented 6 years ago

What's wrong with HTTPClient?

utrack commented 6 years ago

I see, thank you!

At the moment, HTTPClient marshals the whole struct to the Body (like body: '*') even if only one field is specified in proto.

utrack commented 6 years ago

https://github.com/utrack/clay/tree/zyukin-fix-unmarshal

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '33' 'http://localhost:12345/v1/example/sum/22'

{
  "error": "couldn't parse request: couldn't read request JSON: json: cannot unmarshal number into Go value of type map[string]json.RawMessage"
}
zyukin commented 6 years ago

What proto?

utrack commented 6 years ago

Are you from Ozon? It would be much faster if you'd ping me on Slack if so. Proto in /doc/example/pb at branch https://github.com/utrack/clay/tree/zyukin-fix-unmarshal