Skip to content

Commit 64fb0c5

Browse files
committed
fix json omitempty case
1 parent 2e713f6 commit 64fb0c5

3 files changed

Lines changed: 44 additions & 132 deletions

File tree

helper/jsonMap.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helper
33
import (
44
"fmt"
55
"reflect"
6+
"strings"
67
)
78

89
func GetValidMap(in any) (map[string]any, error) {
@@ -66,7 +67,11 @@ func MapJsonMapToStruct(jsonMapInput map[string]any, structToUpdate any) error {
6667
fieldKey := fieldType.Name
6768
jsonKey := fieldType.Tag.Get("json")
6869
if len(jsonKey) > 0 {
69-
fieldKey = jsonKey
70+
// Split on comma to handle omitempty and other options
71+
jsonKey = strings.Split(jsonKey, ",")[0]
72+
if jsonKey != "-" {
73+
fieldKey = jsonKey
74+
}
7075
}
7176

7277
if jsonValue, ok := jsonMapInput[fieldKey]; ok {

helper/jsonMap_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package helper
22

33
import (
4+
"encoding/json"
45
"reflect"
56
"testing"
67

@@ -415,3 +416,40 @@ func TestMapJsonMapToStructWithUUID(t *testing.T) {
415416
assert.Equal(t, expectedUUID, result.ID)
416417
})
417418
}
419+
420+
func TestMapJsonMapToStructWithOmitEmpty(t *testing.T) {
421+
t.Run("Map field with omitempty tag", func(t *testing.T) {
422+
type TestStruct struct {
423+
Name string `json:"name"`
424+
Config map[string]any `json:"config,omitempty"`
425+
}
426+
427+
original := TestStruct{
428+
Name: "test",
429+
Config: map[string]any{
430+
"key1": "value1",
431+
"key2": 42,
432+
},
433+
}
434+
435+
// Marshal and unmarshal through JSON
436+
jsonBytes, err := json.Marshal(original)
437+
assert.NoError(t, err)
438+
439+
var jsonMap map[string]any
440+
err = json.Unmarshal(jsonBytes, &jsonMap)
441+
assert.NoError(t, err)
442+
443+
// Convert using MapJsonMapToStruct
444+
var result TestStruct
445+
err = MapJsonMapToStruct(jsonMap, &result)
446+
assert.NoError(t, err)
447+
448+
assert.Equal(t, "test", result.Name)
449+
assert.NotNil(t, result.Config)
450+
assert.Len(t, result.Config, 2)
451+
assert.Equal(t, "value1", result.Config["key1"])
452+
// JSON unmarshals numbers as float64
453+
assert.Equal(t, float64(42), result.Config["key2"])
454+
})
455+
}

model/validation_test.go

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package model
22

33
import (
4-
"encoding/json"
54
"testing"
65

7-
"github.com/siherrmann/validator/helper"
86
"github.com/stretchr/testify/assert"
97
)
108

@@ -180,132 +178,3 @@ func TestGetValidationsFromStruct(t *testing.T) {
180178
})
181179
}
182180
}
183-
184-
func TestValidationJSONMarshaling(t *testing.T) {
185-
t.Run("Marshal and unmarshal Validation with nil Groups", func(t *testing.T) {
186-
v := Validation{
187-
Key: "TestField",
188-
Type: String,
189-
Requirement: "min5",
190-
Groups: nil,
191-
Default: "",
192-
}
193-
194-
// Marshal to JSON
195-
jsonBytes, err := json.Marshal(v)
196-
assert.NoError(t, err, "Expected no error marshaling")
197-
198-
// Unmarshal back
199-
var result Validation
200-
err = json.Unmarshal(jsonBytes, &result)
201-
assert.NoError(t, err, "Expected no error unmarshaling")
202-
assert.Equal(t, v, result, "Expected validation to match after round-trip")
203-
})
204-
205-
t.Run("Marshal and unmarshal Validation with Groups", func(t *testing.T) {
206-
v := Validation{
207-
Key: "TestField",
208-
Type: String,
209-
Requirement: "min5",
210-
Groups: []*Group{{Name: "gr1", ConditionType: "min", ConditionValue: "1"}},
211-
Default: "",
212-
}
213-
214-
// Marshal to JSON
215-
jsonBytes, err := json.Marshal(v)
216-
assert.NoError(t, err, "Expected no error marshaling")
217-
218-
// Unmarshal back
219-
var result Validation
220-
err = json.Unmarshal(jsonBytes, &result)
221-
assert.NoError(t, err, "Expected no error unmarshaling")
222-
assert.Equal(t, v, result, "Expected validation to match after round-trip")
223-
})
224-
225-
t.Run("Unmarshal Validation from map[string]any", func(t *testing.T) {
226-
jsonMap := map[string]any{
227-
"Key": "TestField",
228-
"Type": String,
229-
"Requirement": "min5",
230-
"Groups": nil,
231-
"Default": "",
232-
}
233-
234-
var result Validation
235-
err := helper.MapJsonMapToStruct(jsonMap, &result)
236-
assert.NoError(t, err, "Expected no error mapping to struct")
237-
assert.Equal(t, "TestField", result.Key)
238-
assert.Equal(t, String, result.Type)
239-
assert.Equal(t, "min5", result.Requirement)
240-
assert.Nil(t, result.Groups)
241-
})
242-
243-
t.Run("Unmarshal Validation with Groups from map[string]any", func(t *testing.T) {
244-
jsonMap := map[string]any{
245-
"Key": "TestField",
246-
"Type": String,
247-
"Requirement": "min5",
248-
"Groups": []any{
249-
map[string]any{"Name": "gr1", "ConditionType": "min", "ConditionValue": "1"},
250-
},
251-
"Default": "",
252-
}
253-
254-
var result Validation
255-
err := helper.MapJsonMapToStruct(jsonMap, &result)
256-
assert.NoError(t, err, "Expected no error mapping to struct")
257-
assert.Equal(t, "TestField", result.Key)
258-
assert.Equal(t, String, result.Type)
259-
assert.Equal(t, "min5", result.Requirement)
260-
assert.NotNil(t, result.Groups)
261-
assert.Len(t, result.Groups, 1)
262-
assert.Equal(t, "gr1", result.Groups[0].Name)
263-
})
264-
265-
t.Run("Unmarshal Validation with empty Groups from map[string]any", func(t *testing.T) {
266-
jsonMap := map[string]any{
267-
"Key": "TestField",
268-
"Type": String,
269-
"Requirement": "min5",
270-
"Groups": []any{},
271-
"Default": "",
272-
}
273-
274-
var result Validation
275-
err := helper.MapJsonMapToStruct(jsonMap, &result)
276-
assert.NoError(t, err, "Expected no error mapping to struct with empty Groups")
277-
assert.Equal(t, "TestField", result.Key)
278-
assert.NotNil(t, result.Groups)
279-
assert.Len(t, result.Groups, 0, "Expected Groups to be empty slice")
280-
})
281-
282-
t.Run("Unmarshal Validation with InnerValidation", func(t *testing.T) {
283-
jsonMap := map[string]any{
284-
"Key": "TestField",
285-
"Type": Struct,
286-
"Requirement": "-",
287-
"Groups": nil,
288-
"Default": "",
289-
"InnerValidation": []any{
290-
map[string]any{
291-
"Key": "InnerField",
292-
"Type": String,
293-
"Requirement": "min3",
294-
"Groups": nil,
295-
"Default": "",
296-
},
297-
},
298-
}
299-
300-
var result Validation
301-
err := helper.MapJsonMapToStruct(jsonMap, &result)
302-
assert.NoError(t, err, "Expected no error mapping to struct with InnerValidation")
303-
assert.Equal(t, "TestField", result.Key)
304-
assert.Equal(t, Struct, result.Type)
305-
assert.NotNil(t, result.InnerValidation)
306-
assert.Len(t, result.InnerValidation, 1)
307-
assert.Equal(t, "InnerField", result.InnerValidation[0].Key)
308-
assert.Equal(t, String, result.InnerValidation[0].Type)
309-
assert.Equal(t, "min3", result.InnerValidation[0].Requirement)
310-
})
311-
}

0 commit comments

Comments
 (0)