Skip to content

MockMvcResultMatchers.jsonPath(String).value() should have a matching method to declare the expected type [SPR-14498] #19067

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

Closed
spring-projects-issues opened this issue Jul 20, 2016 · 9 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 20, 2016

Chris Mercer opened SPR-14498 and commented

Java Double instances will sometimes be converted from "12.0" to "12" in JSON based on rules/Jackson configuration.

While reading the results back in testing, JsonPath will convert the "JSON number" to an Integer.

String json = "{ \"value\" : 12}";
Double expectedValue = 12.0;
MockMvcResultMatchers.jsonPath("$.value").value(expectedValue) 
...
...
... expected:<java.lang.Double> but was:<java.lang.Integer>

There should be an additional "value" method that accepts the target type for JsonPath to convert to.

Double expectedValue = 12.0;
MockMvcResultMatchers.jsonPath("$.value").value(expectedValue, Double.class) 

Affects: 4.2.5

Issue Links:

Referenced from: commits 2c2ce55, 7fdb892, 68463e2, d09b0fe

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

FYI: This is very closely related (identical?) to a discussion on Stack Overflow.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Currently we use JsonPath.compile(expression) to obtain a pre-compiled JsonPath and then call read(json) on it, which doesn't seem to have an option to specify the target class. Looking at the JsonPath documentation it shows examples where JsonPath.parse(json) is used first to obtain a DocumentContext on which one can then call read(expression, targetClass) with a target class. Seems like we should switch to the latter.

Sam Brannen what do you think? JsonPathExpectationsHelper currently accepts the expression as a constructor arg but whatever benefit there is, it makes no difference when used through MockMvcResultMatchers which creates a new JsonPathExpectationsHelper instance every time. So in JsonPathExpectationsHelper we could simply cache the expression and then call JsonPath.parse(json).read(expression, targetClass).

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Rossen Stoyanchev,

I agree with your proposal. That sounds like the best course of action.

By default, JsonPath will use the JsonSmartMappingProvider which has its own mapping (i.e., conversion) infrastructure based on the json-smart project. So hopefully that will suffice for most common mapping scenarios, and if it doesn't the user should be able to configure a different MappingProvider (such as the JacksonMappingProvider) as I explained in that Stack Overflow discussion.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

See commit 7fdb89 for the resulting fix.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Great work! (y)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Looks good to me. I'll backport it right away since I'm wrapping up other stuff on 4.3.x at the moment anyway :-)

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Thanks, Juergen Hoeller

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Thanks to a comment on Stack Overflow, I just noticed that...

The explicit targetType variant was added to JsonPathRequestMatchers but not to JsonPathResultMatchers.

Oops!

I'll address that in a new issue.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 13, 2018

Sam Brannen commented

See #21129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants