Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty slice encode/decode error #340 #341

Merged
merged 2 commits into from Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 15 additions & 10 deletions ref.go
Expand Up @@ -26,6 +26,9 @@ import (
perrors "github.com/pkg/errors"
)

// Empty slice is not nil, but the addresses of all empty slice are the same.
var _emptySliceAddr = unsafe.Pointer(reflect.ValueOf([]interface{}{}).Pointer())

// used to ref object,list,map
type _refElem struct {
// record the kind of target, objects are the same only if the address and kind are the same
Expand Down Expand Up @@ -124,18 +127,20 @@ func (e *Encoder) checkRefMap(v reflect.Value) (int, bool) {
}
}

if elem, ok := e.refMap[addr]; ok {
if elem.kind == kind {
// If kind is not struct, just return the index. Otherwise,
// check whether the types are same, because the different
// empty struct may share the same address and kind.
if elem.kind != reflect.Struct {
return elem.index, ok
} else if elem.tp == tp {
return elem.index, ok
if addr != _emptySliceAddr {
if elem, ok := e.refMap[addr]; ok {
if elem.kind == kind {
// If kind is not struct, just return the index. Otherwise,
// check whether the types are same, because the different
// empty struct may share the same address and kind.
if elem.kind != reflect.Struct {
return elem.index, ok
} else if elem.tp == tp {
return elem.index, ok
}
}
return 0, false
}
return 0, false
}

n := len(e.refMap)
Expand Down
89 changes: 89 additions & 0 deletions testcases/issue340/issue340_test.go
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package issue340

import (
"reflect"
"testing"
)

import (
hessian "github.com/apache/dubbo-go-hessian2"
)

import (
"github.com/stretchr/testify/assert"
)

type Point struct {
X int
Y int
}

func (Point) JavaClassName() string {
return "com.test.Point"
}

type SpPoint struct {
X int
Y int
Sp int
}

func (SpPoint) JavaClassName() string {
return "com.test.SpPoint"
}

type ReqInfo struct {
Name string
Points []*Point
SpPoints []*SpPoint
}

func (ReqInfo) JavaClassName() string {
return "com.test.ReqInfo"
}

func TestIssue340Case(t *testing.T) {
req := &ReqInfo{
Name: "test",
Points: []*Point{},
SpPoints: []*SpPoint{},
}

hessian.RegisterPOJO(&Point{})
hessian.RegisterPOJO(&SpPoint{})
hessian.RegisterPOJO(req)

encoder := hessian.NewEncoder()
err := encoder.Encode(req)
if err != nil {
t.Error(err)
return
}

enBuf := encoder.Buffer()

decoder := hessian.NewDecoder(enBuf)
deReq, err := decoder.Decode()
assert.Nil(t, err)

t.Log(deReq)

assert.True(t, reflect.DeepEqual(req, deReq))
}