shubhamk0205 opened a new issue, #404:
URL: https://github.com/apache/dubbo-go-hessian2/issues/404

   **What happened**:
   
   If a value has a named basic type, like `type Status string` or `type Code 
int64`, `Encode` writes nothing and returns no error. This happens for the 
string, int, uint and float kinds (named `int32` is fine because it goes 
through `encTypeInt32`).
   
   Inside a struct this is worse: the class definition still lists the field, 
but the instance doesn't write it, so every field after it shifts by one and 
Java reads the wrong values.
   
   A named `bool` (and `**bool`) panics instead. On the decode side, decoding a 
string into a field of a named string type also panics in `SetValue`.
   
   **What you expected to happen**:
   
   A named type should be encoded the same way as its underlying type, so 
`Status("ok")` gives the same bytes as `"ok"`, and nothing should panic.
   
   **How to reproduce it (as minimally and precisely as possible)**:
   
   ```go
   type Status string
   type Flag bool
   
   e := hessian.NewEncoder()
   err := e.Encode(Status("ok"))
   fmt.Println(err, len(e.Buffer())) // <nil> 0  -> nothing written
   
   e = hessian.NewEncoder()
   e.Encode(Flag(true)) // panic: interface conversion: interface {} is 
main.Flag, not *bool
   ```
   
   With `struct{ S Status; Name string }`, the class definition has 2 fields 
but the instance only contains `Name`.
   
   Tested on master (7f32699).
   
   **Anything else we need to know?**:
   
   I think the cause is in the `default:` branch of `Encode` in `encode.go`. 
For the string/int/float kinds it only handles the pointer case, so a plain 
named value falls through without writing anything, and the `reflect.Bool` case 
does `v.(*bool)`, which only works for exactly `*bool`. On decode, `SetValue` 
in `codec.go` has cases for int, uint and float but not for string or bool.
   
   #297 tried to fix part of this some time ago but was closed because it had 
no tests.
   
   I'd like to send a PR for this: a small fix in the encoder and `SetValue`, 
with tests that fail on master today, and a Java-side check like #399 if that's 
useful. Would that be OK?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

  • [I] Named basic types (e.g. ty... shubhamk0205 (via GitHub) via notifications

Reply via email to