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

ISSUE#31 Fixed panic on nil ptr #32

Merged
merged 1 commit into from
Aug 30, 2021

Conversation

lombare
Copy link

@lombare lombare commented Aug 25, 2021

Pull request related to #31

@mweibel mweibel merged commit bfb5dba into liip:master Aug 30, 2021
@mweibel
Copy link
Collaborator

mweibel commented Aug 30, 2021

thanks!

@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

I think the slice case should be removed because a nil slice is effectively an empty slice.
This caused a regression in our codebase. Instead of [] being in the response nownull is which causes an error in the frontend.

@mweibel
Copy link
Collaborator

mweibel commented Sep 1, 2021

hmm, you're right. same for the map actually.
would you mind creating a PR fixing this?

@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

Sure, I'll do that right away.

simaotwx added a commit to simaotwx/sheriff that referenced this pull request Sep 1, 2021
Maps and slices that are nil are empty and valid.
The change introduced by liip#32 which fixes liip#31 has changed how
nil slices and maps are marshaled (`null` instead of `[]`).

Fix this regression by removing maps and slices from the check.
@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

While writing tests, I'm getting a fail on the map part.
On v0.10.0 maps were already being marshaled as null.

Here is the test result:

--- FAIL: TestMarshal_NilMap (0.00s)
    sheriff_test.go:762: 
        	Error Trace:	sheriff_test.go:762
        	Error:      	Not equal: 
        	            	expected: "{}"
        	            	actual  : "null"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{}
        	            	+null
        	Test:       	TestMarshal_NilMap
FAIL
exit status 1
FAIL	github.com/liip/sheriff	0.005s

There is a test that expects null for maps:

https://github.com/liip/sheriff/blob/master/sheriff_test.go#L493

simaotwx added a commit to simaotwx/sheriff that referenced this pull request Sep 1, 2021
Maps and slices that are nil are empty and valid.
The change introduced by liip#32 which fixes liip#31 has changed how
nil slices and maps are marshaled (`null` instead of `[]`).

Fix this regression by removing maps and slices from the check.
@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

If keeping existing behavior is desired, I would suggest only removing reflect.Slice from the switch. This way the tests still work and the new one testing nil slices does, too.

simaotwx added a commit to simaotwx/sheriff that referenced this pull request Sep 1, 2021
Slices that are nil are empty and valid.
The change introduced by liip#32 which fixes liip#31 has changed how
nil slices are marshaled (`null` instead of `[]`).

Fix this regression by removing slices from the check.
@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

The interesting thing about this is that json.Marshal also marshals a nil slice to null so it seems like this library has always deviated from that behavior. In case this is supposed to mimic how json.Marshal would handle this case, the current implementation is correct, but breaks existing codebases that falsely relied on this behavior (although since semver allows breaking changes on 0.x I don't see a big issue here).

@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

In codebases using this library, this can be fixed/worked around by changing

var slice []T

to

slice := []T{}

@mweibel
Copy link
Collaborator

mweibel commented Sep 1, 2021

hmm not sure what to do here TBH.

var vs := does have a slight difference (var does not allocate memory IIRC) but otherwise that should be ok.

@mweibel
Copy link
Collaborator

mweibel commented Sep 1, 2021

The initial issue was only for nil pointer and then the PR extended into other types too.
Let's merge your PR, but fix the failing test first?

@simaotwx
Copy link
Contributor

simaotwx commented Sep 1, 2021

var vs := does have a slight difference (var does not allocate memory IIRC) but otherwise that should be ok.

The difference between the two is that slice := []T{} creates an empty slice whereas var slice []T creates a nil slice (so there's no underlying storage for the slice and the value is nil).

The initial issue was only for nil pointer and then the PR extended into other types too. Let's merge your PR, but fix the failing test first?

The failing test is not fixable on its own, so I created another branch with only reflect.Slice removed which only requires one test and it doesn't fail in that case: simaotwx@0b23eee

Should I open a PR for that one?

@mweibel
Copy link
Collaborator

mweibel commented Sep 1, 2021

yeah, let's do that.

mweibel pushed a commit that referenced this pull request Sep 1, 2021
Slices that are nil are empty and valid.
The change introduced by #32 which fixes #31 has changed how
nil slices are marshaled (`null` instead of `[]`).

Fix this regression by removing slices from the check.
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 this pull request may close these issues.

3 participants