Skip to content

Commit

Permalink
fix issue #340 (#341)
Browse files Browse the repository at this point in the history
* fix issue #340

* format code

Co-authored-by: wongoo <wongoo@apache.org>
  • Loading branch information
tiltwind and wongoo committed Dec 6, 2022
1 parent a6671d5 commit 6455362
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
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))
}

0 comments on commit 6455362

Please sign in to comment.