-
Notifications
You must be signed in to change notification settings - Fork 281
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
#302: do not use ReferenceSchema#referredSchema for equals+hashCode #378
Open
tmarsteel
wants to merge
7
commits into
everit-org:master
Choose a base branch
from
rebuy-de:cyclic-schema
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0851cc3
Issue #302: do not use ReferenceSchema#referredSchema for equals+hash…
14ae9d2
#302: fix unit test name naming
2834a61
#302: remove accidentially added jenv config
844d515
Issue #302: include ReferenceSchema#referredSchema in equals
fe1a51c
Issue #302: improve stability of HashCodeRecursionTest
13438c9
Issue #302: DRAFT: support ReferenceSchema#equals with cyclic schemas
631d08d
Issue #302: javadocs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
core/src/main/java/org/everit/json/schema/internal/EqualsCycleBreaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.everit.json.schema.internal; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
import java.util.function.BiFunction; | ||
|
||
public final class EqualsCycleBreaker | ||
{ | ||
private EqualsCycleBreaker() | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* ThreadLocal because this class doesn't bother with stack overflows across multiple threads, if that | ||
* is even possible. | ||
* <br> | ||
* A <i>weak</i> map so that this consumes less memory. | ||
* <br> | ||
* For each ongoing equality check via {@link #equalsWithoutCycle(Object, Object, boolean, BiFunction)}, | ||
* maps the <code>this</code> pointer of the <code>equals</code> invocation to all of the objects it is | ||
* being compared against. Each mapping is removed when `equals` returns. | ||
* <br> | ||
* This way, when {@link Object#equals(Object)} is called with the same parameters (this and the other reference) | ||
* a second time before the first invocation has returned (= cyclic!), it can be detected and handled. | ||
*/ | ||
private static final ThreadLocal<WeakHashMap<Identity, Set>> ongoingEqualityChecks = ThreadLocal.withInitial(WeakHashMap::new); | ||
|
||
/** | ||
* Use to break cycles in equality checks. For example: | ||
* | ||
* <pre> | ||
* class A { | ||
* B b; | ||
* | ||
* public boolean equals(Object o) { | ||
* if (!(o instanceof A)) { | ||
* return false; | ||
* } | ||
* | ||
* return this.b.equals(((A) o).b); | ||
* } | ||
* } | ||
* class B { | ||
* int i; | ||
* A a; | ||
* | ||
* public boolean equals(Object o) { | ||
* if (!(o instanceof B)) { | ||
* return false; | ||
* } | ||
* | ||
* B that = (B) o; | ||
* if (i != that.i) { | ||
* return false; | ||
* } | ||
* | ||
* return EqualsCycleBreaker.equalsWithoutCycle(this, that, true, B::equalsPossiblyCyclic); | ||
* } | ||
* | ||
* private boolean equalsPossiblyCyclic(B that) { | ||
* return this.a.equals(that.a); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* If you now construct a cyclic object tree and call equals on it, it will not explode with a stack overflow: | ||
* <pre> | ||
* A a = new A(); | ||
* B b = new B(); | ||
* b.i = 10; | ||
* b.a = a; | ||
* a.b = b; | ||
* | ||
* b.equals(b); // returns true | ||
* </pre> | ||
* | ||
* @param self The receiver of an invocation to {@link Object#equals(Object)}. E.g. in <code>a.equals(b)</code>, this | ||
* parameter is <code>a</code>. | ||
* @param other The parameter of an invocation to {@link Object#equals(Object)}. E.g. in <code>a.equals(b)</code>, this | ||
* parameter is <code>b</code>. | ||
* @param equalsOnCycle What this method should return when it detects a cycle | ||
* @param equalityFunction The part of the equality check that can cause cyclic invocations / stack overflows. | ||
* @return If this method is called in a cycle, returns <code>equalsOnCycle</code>. Otherwise defers to <code>equalityFunction</code>. | ||
*/ | ||
public static <T> boolean equalsWithoutCycle(T self, T other, boolean equalsOnCycle, BiFunction<T, T, Boolean> equalityFunction) { | ||
Set<T> localOngoingEqualityChecks = ongoingEqualityChecks.get() | ||
.computeIfAbsent(new Identity<>(self), (_k) -> new HashSet<>()); | ||
if (localOngoingEqualityChecks.add(other)) { | ||
try { | ||
return equalityFunction.apply(self, other); | ||
} | ||
finally { | ||
localOngoingEqualityChecks.remove(other); | ||
if (localOngoingEqualityChecks.isEmpty()) { | ||
ongoingEqualityChecks.remove(); | ||
} | ||
} | ||
} else { | ||
return equalsOnCycle; | ||
} | ||
} | ||
|
||
private static class Identity<E> { | ||
private final E e; | ||
|
||
public Identity(E e) | ||
{ | ||
this.e = e; | ||
} | ||
|
||
public int hashCode() { | ||
return System.identityHashCode(e); | ||
} | ||
|
||
public boolean equals(Object o) { | ||
if (!(o instanceof Identity)) { | ||
return false; | ||
} | ||
return ((Identity<?>) o).e == e; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
core/src/test/java/org/everit/json/schema/HashCodeRecursionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.everit.json.schema; | ||
|
||
import org.everit.json.schema.loader.SchemaLoader; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class HashCodeRecursionTest | ||
{ | ||
@Test | ||
public void hashCodeShouldNotProduceStackoverflowOnCyclicSchema() throws IOException | ||
{ | ||
loadSelfCyclic().hashCode(); | ||
} | ||
|
||
@Test | ||
public void equalsShouldNotProduceStackoverflowOnCyclicSchema() throws IOException | ||
{ | ||
CombinedSchema cyclic = (CombinedSchema) loadSelfCyclic(); | ||
CombinedSchema cyclicCopy = (CombinedSchema) loadSelfCyclic(); | ||
|
||
assertEquals(cyclic, cyclicCopy); | ||
} | ||
|
||
private Schema loadSelfCyclic() throws IOException | ||
{ | ||
JSONObject schemaJson; | ||
try (InputStream inStream = getClass().getResourceAsStream("/org/everit/jsonvalidator/cyclic.json")) { | ||
schemaJson = new JSONObject(new JSONTokener(inStream)); | ||
} | ||
|
||
return new SchemaLoader.SchemaLoaderBuilder() | ||
.schemaJson(schemaJson) | ||
.build() | ||
.load() | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/test/resources/org/everit/jsonvalidator/cyclic.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"title": "Foo Schema", | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/Foo" | ||
} | ||
], | ||
"definitions": { | ||
"Bar": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { | ||
"$ref": "#/definitions/Foo" | ||
} | ||
} | ||
}, | ||
"Foo": { | ||
"type": "object", | ||
"properties": { | ||
"bar": { | ||
"$ref": "#/definitions/Bar" | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @tmarsteel , do you think we loose anything if we keep this line? So my concern is that, with this change, there can be (rare) cases when two schemas are considered equal when they are not (I mean eg. two references pointing to
#
, but this denotes the roots of two different schema documents).What do you think about keeping this line? I see that it might run into infinite
equals()
recursions, but that's probably a very rare case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of Confluent Schema Registry, we call
equals
quite often on schema. So we would prefer the fix above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a very good point. The testcase and my actual cyclic schemas both work with
referredSchema
being included in equals.