Skip to content

Commit

Permalink
fix #796 where order of object nodes attributes is not kept
Browse files Browse the repository at this point in the history
  • Loading branch information
glhez authored and lukas-krecan committed Jul 16, 2024
1 parent ba02fdf commit 673fd45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
*/
package net.javacrumbs.jsonunit.core.internal;

import org.jetbrains.annotations.NotNull;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint;

import java.math.BigDecimal;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.Collections.unmodifiableSet;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint;

import org.jetbrains.annotations.NotNull;

/**
* For internal use only!!! Abstract node representation.
Expand Down Expand Up @@ -235,10 +235,9 @@ class JsonMap extends AbstractMap<String, Object> implements NodeWrapper {
public Set<Entry<String, Object>> entrySet() {
if (entrySet == null) {
Iterator<KeyValue> fields = wrappedNode.fields();
entrySet = unmodifiableSet(StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false)
.map(keyValue -> new SimpleEntry<>(
keyValue.getKey(), keyValue.getValue().getValue()))
.collect(Collectors.toSet()));
entrySet = StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, java.util.Spliterator.ORDERED), false)
.map(keyValue -> new SimpleEntry<>(keyValue.getKey(), keyValue.getValue().getValue()))
.collect(collectingAndThen(toCollection(LinkedHashSet::new), Collections::unmodifiableSet));
}
return entrySet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ void objectShouldContainComplexValueError() {
" {\"c\":5}");
}

@Test
void objectFieldsShouldBeKeptInOrder() {
assertThatJson("{\"root\":{\"key3\": 3, \"key2\": 2, \"key1\": 1 }}").node("root")
.isObject()
.containsExactly(entry("key3", 3),
entry("key2", 2),
entry("key1", 1));
}

@Test
void objectDoesContainComplexValue() {
assertThatJson("{\"a\":1, \"b\": {\"c\" :3}}").isObject().doesNotContainValue(json("{\"c\" :\"${json-unit.any-string}\"}"));
Expand Down

0 comments on commit 673fd45

Please sign in to comment.