karate icon indicating copy to clipboard operation
karate copied to clipboard

nested array JSON building fails edge case

Open ptrthomas opened this issue 2 years ago • 2 comments

as reported here: https://stackoverflow.com/a/69536418/143475

to replicate the issue:

* def foo = {}
* set foo.a[0].b[0].c = 'hello'
* print foo

error:

* set foo.a[0].b[0].c = 'hello'
class java.util.LinkedHashMap cannot be cast to class java.util.List (java.util.LinkedHashMap and java.util.List are in module java.base of loader 'bootstrap')

workaround:

* def foo = {}
* set foo.a[0].b = [{ c: 'hello' }]
* print foo

Karate tries to auto-create missing parents and the logic is a bit tricky. you can find the code here: https://github.com/karatelabs/karate/blob/v1.1.0/karate-core/src/main/java/com/intuit/karate/Json.java#L141

ptrthomas avatar Oct 12 '21 07:10 ptrthomas

@ptrthomas Can i work on this issue??

sc2776 avatar Oct 14 '21 20:10 sc2776

@sc2776 yes, go ahead

ptrthomas avatar Oct 15 '21 04:10 ptrthomas

Hi @ptrthomas , Requesting more info on this. Can I get some sample java test cases for this issue if possible?

I did some analysis and came up with the sample request. Confirm if the below test case is one of the error scenarios.

Json json = Json.object(); json.set("first",""); json.set("first.second[0].third[0].fourth","hello");

Thanks

captainbkarthick avatar Oct 17 '22 17:10 captainbkarthick

@captainbkarthick that looks correct, if you get the same error mentioned in the original post, you are on the right track

ptrthomas avatar Oct 18 '22 02:10 ptrthomas

@ptrthomas, I am trying to solve this issue. Will keep you posted on this. Thanks

captainbkarthick avatar Oct 18 '22 06:10 captainbkarthick

@ptrthomas , I have fixed the issue. Requesting to check the PR.

captainbkarthick avatar Oct 18 '22 09:10 captainbkarthick

1.3.0 released

ptrthomas avatar Nov 02 '22 17:11 ptrthomas