Repository: zeromicro/go-zero
Pull request #3286 (2023-05-26 10:08:15)

chore: add more tests

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3286?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3286](https://app.codecov.io/gh/zeromicro/go-zero/pull/3286?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (c15703e) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/fa33329a449a79e6d74dea583de2811cb7f005b8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (fa33329) will **decrease** coverage by `0.02%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3286?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/mapping/unmarshaler.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3286?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9tYXBwaW5nL3VubWFyc2hhbGVyLmdv) | `96.33% <100.00%> (+0.01%)` | :arrow_up: |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3286/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go
index 5d315085..895f288f 100644
--- a/core/mapping/unmarshaler.go
+++ b/core/mapping/unmarshaler.go
@@ -350,7 +350,12 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any)
 					return emptyValue, errTypeMismatch
 				}
 
-				targetValue.SetMapIndex(key, reflect.ValueOf(v))
+				val := reflect.ValueOf(v)
+				if !val.Type().AssignableTo(dereffedElemType) {
+					return emptyValue, errTypeMismatch
+				}
+
+				targetValue.SetMapIndex(key, val)
 			case json.Number:
 				target := reflect.New(dereffedElemType)
 				if err := setValueFromString(dereffedElemKind, target.Elem(), v.String()); err != nil {


Test patch:
diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go
index 425dc80c..399b2b4e 100644
--- a/core/mapping/unmarshaler_test.go
+++ b/core/mapping/unmarshaler_test.go
@@ -4400,6 +4400,16 @@ func TestUnmarshalJsonReaderWithTypeString(t *testing.T) {
 		body := `{"params":{"a":{"a":123}}}`
 		assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
 	})
+
+	t.Run("customized string type", func(t *testing.T) {
+		type myString string
+
+		var req struct {
+			Params map[string]myString `json:"params"`
+		}
+		body := `{"params":{"a":"b"}}`
+		assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
+	})
 }
 
 func TestUnmarshalJsonReaderWithMismatchType(t *testing.T) {
@@ -4862,19 +4872,6 @@ func Test_UnmarshalMap(t *testing.T) {
 	})
 }
 
-// func TestUnmarshalWithFillPrimitives(t *testing.T) {
-// 	t.Run("fill primitives", func(t *testing.T) {
-// 		type St struct {
-// 			A int `key:"a,string,range=[5,10]"`
-// 		}
-// 		var st St
-// 		err := UnmarshalKey(map[string]any{
-// 			"a": "1",
-// 		}, &st)
-// 		assert.ErrorIs(t, err, errNumberRange)
-// 	})
-// }
-
 func TestUnmarshaler_Unmarshal(t *testing.T) {
 	t.Run("not struct", func(t *testing.T) {
 		var i int