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

Rest Assured 5.4.0 does not handle valid Json #1766

Open
genekostruba opened this issue Jan 22, 2024 · 3 comments
Open

Rest Assured 5.4.0 does not handle valid Json #1766

genekostruba opened this issue Jan 22, 2024 · 3 comments

Comments

@genekostruba
Copy link

I encountered a queryable Json object on the web similar to:

{"message":{"ErrorCode:":0,"ErrorMsg:":"Success"}}

Note the extra colons after ErrorCode and ErrorMsg. These extra colons are valid Json, as they are simply part of their strings, and any prettyprinter on the web will render it correctly as:

{
  "message":  {
    "ErrorCode:":  0,
    "ErrorMsg:":  "Success"
  }
}

However, after querying this object with restassured and extracting the response body to jsonString, the following code:

JsonPath jsonPath = new JsonPath(jsonString);
Assert.assertEquals(jsonPath.getInt("message.ErrorCode:"), 0);

throws error in the Assert statement:

java.lang.IllegalArgumentException: Invalid JSON expression

Removing the colon from the Assert statement:

Assert.assertEquals(jsonPath.getInt("message.ErrorCode"), 0);

and rerunning then throws error:

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of
"io.restassured.internal.common.path.ObjectConverter.convertObjectTo(Object, java.lang.Class)" is null

Removing the extra colons from jsonString itself clears the error.

Not sure why restassured has a problem with this valid Json.

@lucas-nguyen-17
Copy link
Contributor

An easy fix is using bracket, not dot.

getInt("message['ErrorCode:']")

@genekostruba
Copy link
Author

Thanks Lucas, but you shouldn't have to use array notation to get around a path qualification problem.

@lucas-nguyen-17
Copy link
Contributor

lucas-nguyen-17 commented Jan 24, 2024

Rest-Assured uses GPath groovy internally to extract value and this error is not only for Rest-Assured. It happens with Javascript as well, example https://stackoverflow.com/questions/4925760/selecting-a-json-object-with-a-colon-in-the-key

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

No branches or pull requests

2 participants