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

json.Marshal() and sheriff.Marshal() gives different results when empty map present in struct #37

Closed
mandeepji opened this issue Apr 25, 2023 · 2 comments · Fixed by #38

Comments

@mandeepji
Copy link
Contributor

When empty map is present in Structure sheriff.Marshal() gives nil but json.Marshal() gives empty json {}

I used existing test TestMarshal_EmptyMap and Marshal same object using json & sheriff and got different results.

Modified Test func:

func TestMarshal_EmptyMap(t *testing.T) {                   
        emp := EmptyMapTest{                                
                AMap: make(map[string]string),              
        }                                                   
        o := &Options{                                      
                Groups: []string{"test"},                   
        }                                                   
                                                            
        actualMap, err := Marshal(o, emp)                   
        assert.NoError(t, err)                              
                                                            
        actual, err := json.Marshal(actualMap)              
        assert.NoError(t, err)                              
                                                            
        expected, err := json.Marshal(emp)                  
        assert.NoError(t, err)                              
                                                            
        assert.Equal(t, string(expected), string(actual))   
}

Test result:

--- FAIL: TestMarshal_EmptyMap (0.00s)
    sheriff_test.go:510:
                Error Trace:    sheriff_test.go:510
                Error:          Not equal:
                                expected: "{\"a_map\":{}}"
                                actual  : "{\"a_map\":null}"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -{"a_map":{}}
                                +{"a_map":null}
                Test:           TestMarshal_EmptyMap
FAIL
@mweibel
Copy link
Collaborator

mweibel commented Apr 25, 2023

oh yeah, that looks like a bug. Wondering if that breaks existing users if we change it.. but would you be open to create a PR for it?

@mandeepji
Copy link
Contributor Author

@mweibel created a PR. Can you review & let me know how can I trigger lint & test?

@mweibel mweibel mentioned this issue May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants