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

add on null does not work anymore ? #200

Closed
eddycharly opened this issue Jan 17, 2024 · 10 comments · Fixed by #201
Closed

add on null does not work anymore ? #200

eddycharly opened this issue Jan 17, 2024 · 10 comments · Fixed by #201

Comments

@eddycharly
Copy link

Looks like applying an add patch to null now panics: https://go.dev/play/p/qo2WNSqwpYZ

package main

import (
	"fmt"

	jsonpatch "github.com/evanphx/json-patch/v5"
)

func main() {
	original := []byte(`null`)
	patchJSON := []byte(`[{"op": "add", "path": "/version", "value": "0.2.0"}]`)
	patch, err := jsonpatch.DecodePatch(patchJSON)
	if err != nil {
		panic(err)
	}
	options := &jsonpatch.ApplyOptions{EnsurePathExistsOnAdd: true}
	modified, err := patch.ApplyWithOptions(original, options)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Original document:\n%s\n", original)
	fmt.Printf("Modified document:\n%s\n", modified)
}
panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/evanphx/json-patch/v5.(*partialDoc).set(...)
	/tmp/gopath2668474575/pkg/mod/github.com/evanphx/json-patch/[email protected]/patch.go:570
github.com/evanphx/json-patch/v5.(*partialDoc).add(0xc0000161b0, {0xc000012120, 0x7}, 0xc0000600e0, 0x496328?)
	/tmp/gopath2668474575/pkg/mod/github.com/evanphx/json-patch/[email protected]/patch.go:575 +0x176
github.com/evanphx/json-patch/v5.Patch.add({0xc000012028?, 0x4?, 0x4?}, 0xc000104e60, 0xc0000161b0?, 0xc00001a078)
	/tmp/gopath2668474575/pkg/mod/github.com/evanphx/json-patch/[email protected]/patch.go:781 +0x25e
github.com/evanphx/json-patch/v5.Patch.ApplyIndentWithOptions({0xc000060040?, 0x1, 0x4}, {0xc000012028, 0x4, 0x4}, {0x0, 0x0}, 0x60?)
	/tmp/gopath2668474575/pkg/mod/github.com/evanphx/json-patch/[email protected]/patch.go:1221 +0x2d9
github.com/evanphx/json-patch/v5.Patch.ApplyWithOptions(...)
	/tmp/gopath2668474575/pkg/mod/github.com/evanphx/json-patch/[email protected]/patch.go:1174
main.main()
	/tmp/sandbox724842672/prog.go:17 +0x105

Program exited.
@evanphx
Copy link
Owner

evanphx commented Jan 17, 2024

Shouldn't panic, that's a bug I'll fix now. But I don't think it's valid either.

@eddycharly
Copy link
Author

Thanks, it used to work in 5.7.0 and below (at least with EnsurePathExistsOnAdd: true)

@evanphx
Copy link
Owner

evanphx commented Jan 17, 2024

@eddycharly what did it do? Why were you using it?

@evanphx
Copy link
Owner

evanphx commented Jan 17, 2024

Looks like in v5.7.0, it returned an error about the json document...

@eddycharly
Copy link
Author

Yes, in v5.7.0 it returned an error failed to patch resource: unexpected JSON token in document node: <nil>.

@evanphx
Copy link
Owner

evanphx commented Jan 17, 2024

kk, I'll have a 5.8.2 out shortly that fixes this.

@eddycharly
Copy link
Author

@evanphx are you still planing a fix release soon ?

shall I give it a try and open a PR ? would that help ?

@evanphx
Copy link
Owner

evanphx commented Jan 27, 2024

Sorry for the delay, dealing with this today.

@evanphx
Copy link
Owner

evanphx commented Jan 28, 2024

5.9.0 is out now with the fix!

@eddycharly
Copy link
Author

Thanks, trying it out !

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