Skip to content
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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public boolean equals(Object o) {
return that.canEqual(this) &&
Objects.equals(refValue, that.refValue) &&
Objects.equals(unprocessedProperties, that.unprocessedProperties) &&
Objects.equals(referredSchema, that.referredSchema) &&
Copy link
Contributor

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.

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.

Copy link
Author

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.

Objects.equals(title, that.title) &&
Objects.equals(description, that.description) &&
super.equals(that);
Expand All @@ -156,7 +155,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), referredSchema, refValue, unprocessedProperties, title, description);
return Objects.hash(super.hashCode(), refValue, unprocessedProperties, title, description);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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;

public class HashCodeRecursionTest
{
@Test
public void hashCodeShouldNotProduceStackoverflowOnCyclicSchema() throws IOException
{
JSONObject schemaJson;
try (InputStream inStream = getClass().getResourceAsStream("/org/everit/jsonvalidator/cyclic.json")) {
schemaJson = new JSONObject(new JSONTokener(inStream));
}

Schema schema = new SchemaLoader.SchemaLoaderBuilder()
.schemaJson(schemaJson)
.build()
.load()
.build();

schema.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void definesPropertyThrowsExc_IfNoReferredSchemaIsSet() {
public void equalsVerifier() {
EqualsVerifier.forClass(ReferenceSchema.class)
.withRedefinedSuperclass()
.withIgnoredFields("schemaLocation", "location")
.withIgnoredFields("schemaLocation", "location", "referredSchema")
//there are specifically some non final fields for loading of recursive schemas
.suppress(Warning.NONFINAL_FIELDS)
.suppress(Warning.STRICT_INHERITANCE)
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/resources/org/everit/jsonvalidator/cyclic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"title": "Foo Schema",
"allOf": [
{
"$ref": "#/definitions/Foo"
}
],
"definitions": {
"Bar": {
"type": "object",
"properties": {
"foo": {
"$ref": "#/definitions/Foo"
}
}
},
"Foo": {
"type": "object",
"properties": {
"bar": {
"$ref": "#/definitions/Bar"
}
}
}
}
}