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 Javascript object with string array as member are passed empty when invoking java object from javascript #2234

Closed
arnault01 opened this issue Jan 6, 2023 · 9 comments
Labels

Comments

@arnault01
Copy link

I have a test case failing with 1.3.1 karate engine and succeeding with engine 0.9.6

In load-config.js we define a JSON object:

    config.spec = Java.type('examples.AuthenticationKarateClient').spec
    config.test = config.spec({roles: {subscribe: ["value1", "value2"]}})

Which is used to instantiate some Java object with fasterxml JSON annotations.

   public final static ObjectMapper om = new ObjectMapper().findAndRegisterModules();

    public static AuthenticationSpec spec(Map<String, Object> params) {
        return om.convertValue(params, AuthenticationSpec.class);
    }

also

    @JsonCreator
    public static AuthenticationSpec fromJson(
            @JsonProperty("roles") Map<String, Object> roles
    )
    {
        return new AuthenticationSpec(relativizeRoles(roles));
    }

The end result is that the string array is empty when received in the java implementation (println of the parmaters attest that).
I have attached a reproduceable test case with all the source code, after following the note about how to submit an issue.

You can play with the pom.xml property karate.version in the maven file from 1.3.1 to 0.9.6.

myproject.zip

@ptrthomas
Copy link
Member

@arnault01 before I look at this, can you do a quick test with 1.4.0.RC2 ? Graal had a problem with JSON marshalling

@ptrthomas
Copy link
Member

1.4.0 requires Java 11: #2083 (comment)

@ptrthomas ptrthomas assigned ptrthomas and unassigned ptrthomas Jan 6, 2023
@arnault01
Copy link
Author

Hi Peter, thaks for taking a look at this issue.
I just did a quick test with my sample project setting the karate.version to 1.4.0.RC2 and the results are the same with 1.3.1, it fails, here is the output:

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.977 s <<< FAILURE! - in examples.TestPlatformManagement
[ERROR] users[1][1]  Time elapsed: 0.938 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: 
match failed: EQUALS
  $ | not equal | actual array length is not equal to expected - 0:2 (LIST:LIST)
  []
  ["value1","value2"]

@ptrthomas ptrthomas assigned ptrthomas and unassigned ptrthomas Jan 6, 2023
@ptrthomas
Copy link
Member

@arnault01 sorry for the quick negative response, but I'm deeming this sample as "too complicated". I am unable to trace the flow of what is doing what. by the way karate-apache does not exist any more and next time, please follow these instructions exactly to the letter: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

one reason I'm giving up is we spent a lot of time ironing out similar bugs when replacing the JS engine. it was not easy, and we kept releasing RC versions to get feedback from the community. now we have moved on, and you are a little late to the scene with a 0.9.X migration

Karate has pretty good built in routines for json conversions and even java bean conversion, so I'm confident you will be able to avoid depending on any external stuff if you really try

one hint in case it helps: the new JS engine is a little picky, so for best results - convert things to JSON strings when trying to "interop" with Java code, in case the Map and List approach does not work.

and lastly, when you are within a JS function and then call to Java code, you may have to call karate.toJava() to prepare the Map / List for playing well with the Java world, refer this: https://github.com/karatelabs/karate/wiki/1.0-upgrade-guide#java-in-js

all the best ! happy to look again if you succeed in boiling down the repro to 2 files and a few lines of code. we are trying to fix one pending possibly related issue: #2232

@ptrthomas
Copy link
Member

@arnault01 we think we identified the problem. 1.4.0.RC3 has been released, please try it and let us know

@arnault01
Copy link
Author

arnault01 commented Jan 12, 2023

Hi @ptrthomas I just tried this morning with the new version 1.4.0.RC3 with no success, it does not solve the issue.
I've read that the sample project is too complicated so I'm working on simplifying this, and I guess I should create a new issue, once ready.
Thanks for following up on this issue.

Btw I'm aware that karate-apache is no more required for newer version of the engine, it was added only in the case where the sample project was modified to run with the karate engine 0.9.6 in order to make it work chaning the simple pom.xml property karate.version.

@ptrthomas
Copy link
Member

@arnault01 thanks for the update, yes please do submit a simpler example and I will look at it. I'm pretty sure that it is the XML annotations on your side. so if you can eliminate that from your sample I would highly prefer that. do consider my hints on passing "plain old JSON" to karate to avoid any marshalling / unmarshalling weirdness

@arnault01
Copy link
Author

Hi @ptrthomas, I've submitted a simpler example, have you had a chance to look at it ?

@ptrthomas
Copy link
Member

@arnault01 not yet, I'll try over the weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants