ApprovalTests.Java icon indicating copy to clipboard operation
ApprovalTests.Java copied to clipboard

JsonJacksonApprovals should accept instance of ObjectMapper

Open olsavmic opened this issue 1 year ago • 2 comments

The org.approvaltests.JsonJacksonApprovals class should accept ObjectMapper as an optional parameter, similarly to the org.approvaltests.JsonApprovals accepting a Function1<GsonBuilder, GsonBuilder> gsonBuilder.

The main motivation is to have consistent serialization with the rest of the application, allowing to perform pretty-printing without changing the semantics of the output (typically NULL values present vs missing).

I don't think there is any reason to modify the mapper during verification, therefore I find it safe to pass the actual instance instead of some ObjectMapper factory/builder.


Thank you for the tooling! I really appreciate such a zero-config snapshotting functionality :)

olsavmic avatar Mar 09 '24 18:03 olsavmic

Since Jackson supports ordering of properties and map entries out of the box (ORDER_MAP_ENTRIES_BY_KEYS, SORT_PROPERTIES_ALPHABETICALLY options), allowing to pass an instance of ObjectMapper will make the JsonJacksonApprovals implementation on par with JsonApprovals in terms of features.

olsavmic avatar Mar 09 '24 19:03 olsavmic

A simple solution would is to simply define the ObjectMapper as a public static field in the class. That on each JsonJacksonApprovals.asJson a new ObjectMapper instance is created is wasteful anyway.

A more elegant approach would be to use the approach by java-snapshot-testing

haisi avatar Aug 07 '24 20:08 haisi