Giter Site home page Giter Site logo

Comments (10)

andrebonna avatar andrebonna commented on May 25, 2024

JSON is just a way to represent the object being tested with snapshot. If the objects compared are deeply equal, default GSON serializer will serialize them equally every time. If you provide a custom JSON serializer function, you must ensure the same behavior.

from json-snapshot.github.io.

tbo avatar tbo commented on May 25, 2024

default GSON serializer will serialize them equally every time

@andrebonna This doesn't match with my observations. The way some HashMaps are being serialized in my test cases seems to be non-deterministic. I get errors like this one all the time:

Changed content at line 22:
expecting:
  ["        "LFDNR": "1",",
   "        "KTOIBAN": "DE56940596610000008888""]
but was:
  ["        "KTOIBAN": "DE56940596610000008888",",
   "        "LFDNR": "1""]

Is there a way to avoid this behavior?

from json-snapshot.github.io.

andrebonna avatar andrebonna commented on May 25, 2024

You are right, I totally forgot HashMaps serialization. Fortunatelly, Jackson serializer has a config property to solve this ordering issue, SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS. I just changed the default JSON serializer library from GSON to Jackson and used the config property to ensure always the same key ordering of a map. That should be fixed in version 1.0.14. I'm reopening the issue until you confirm the bug has been solved on your side.

from json-snapshot.github.io.

tbo avatar tbo commented on May 25, 2024

@andrebonna It seems to be fixed. Thanks!

from json-snapshot.github.io.

andrebonna avatar andrebonna commented on May 25, 2024

I'm generation a new version compiled to Java 8. Version 1.0.15

from json-snapshot.github.io.

metas-ts avatar metas-ts commented on May 25, 2024

Hi @andrebonna,

it still doesn't work for me, unless I also add

objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);

in the method io.github.jsonSnapshot.SnapshotMatcher.buildObjectMapper()

Further reading: https://www.stubbornjava.com/posts/creating-a-somewhat-deterministic-jackson-objectmapper

Pls let me know if I can help with this issue.

from json-snapshot.github.io.

andrebonna avatar andrebonna commented on May 25, 2024

Please, provide us and example. JSON properties should be in the same order as Class properties definition.

from json-snapshot.github.io.

metas-ts avatar metas-ts commented on May 25, 2024

Please, provide us an example. JSON properties should be in the same order as Class properties definition.

Thx for the reply. I'm sorry, but I lack the time to look into trying to extract the case into an example. However, as we are open source, if you want you can

from json-snapshot.github.io.

metas-ts avatar metas-ts commented on May 25, 2024

Turned out that for me even objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); didn't really help to create the same snapshot on my local machine and our CI.
I suspect different java versions to be part of the problem

  • Note: jackson was the same (tried both 2.8.8and 2.9.8), and we also use maven-enforcer to keep the dependency tree clean of duplicate classes and different versions of the same artifact)

The problem occurred with an interface that had default methods/properties:

  • on my local machine the properties were still ordered alphabetically "globally"
  • on jenkins I got the interface's properties (ordered alphabetically) followed by the implementing class's properties (also ordered alphabetically)

Here is my failed test: https://jenkins.metasfresh.com/job/metasfresh-webui/job/gh4959-app/12/testReport/de.metas.ui.web.dataentry.window.descriptor.factory/DataEntryTabLoaderTest/createGroupEntityDescriptors_verify_DocumentEntityDescriptor/ ..just for completeness..i'm not sure if it helps 😊

I eventually got around this problem by explicitly setting the order (i.e. @JsonPropertyOrder(value = { "columnName", "defaultOrderBy", "defaultOrderByAscending", "mandatory" }) ), but to me that's just a workaround. I want my collegues to love your tool and use it broadly, and I think for that, can'T tell them "yeah, it's great but you need to instrument your production code like this and that etc etc".

So, I'm going to check out https://github.com/skyscreamer/JSONassert as documented here: https://www.baeldung.com/jsonassert .
I plan to extend SnapshotConfig to allow a more lenient comparison instead of the current if (!rawSnapshot.trim().equals(currentObject.trim())).

from json-snapshot.github.io.

grimsa avatar grimsa commented on May 25, 2024

Maybe it would be possible to compare expected and actual JSON trees for equality using JsonNode#equals?
I'd expect Jackson to return true for two nodes that have the same fields and values, regardless of field order, but I have not checked.

from json-snapshot.github.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.