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

StackOverflowError when calling hashCode on mutually recursive schemas #302

Open
cpchen opened this issue May 22, 2019 · 6 comments
Open

Comments

@cpchen
Copy link

cpchen commented May 22, 2019

Example schema to reproduce:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "id": "foo",
  "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"
        }
      }
    }
  }
}

Calling hashCode on the corresponding schema object yields the following (truncated) SO:

java.lang.StackOverflowError
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.Schema.hashCode(Schema.java:224)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ReferenceSchema.hashCode(ReferenceSchema.java:102)
	at java.base/java.util.Objects.hashCode(Objects.java:116)
	at java.base/java.util.HashMap$Node.hashCode(HashMap.java:297)
	at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
	at java.base/java.util.Collections$UnmodifiableMap.hashCode(Collections.java:1488)
	at java.base/java.util.Arrays.hashCode(Arrays.java:4684)
	at java.base/java.util.Objects.hash(Objects.java:146)
	at org.everit.json.schema.ObjectSchema.hashCode(ObjectSchema.java:358)
@tmarsteel
Copy link

Would it suffice to have ReferenceSchema not include the referred schema into the hashCode? Actually, that would make a lot of sense to me. The referred schema depends on the schema loader pretty much and the correctness of ReferenceSchemas equals+hashCode shouldn't depend on the schema loader.

@erosb
Copy link
Contributor

erosb commented Jul 17, 2020

Hello @tmarsteel , you can give it a go, but in that case I'm afraid the equals()will still run into an infinite recursion, so not sure if that helps a lot. Feel free to raise a PR with the fix and a passing test, if you want. Thanks for your idea anyway.

tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 17, 2020
tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 17, 2020
tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 17, 2020
@rayokota
Copy link

We use this library in Confluent Schema Registry, and are seeing this error as well when equals or hashCode is called on a recursive schema. confluentinc/schema-registry#1549

tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 24, 2020
tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 24, 2020
tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 24, 2020
tmarsteel pushed a commit to rebuy-de/json-schema that referenced this issue Jul 24, 2020
@shionefaye
Copy link

@rayokota Conflunece Schema Registry issue on recursive schema has been fixed and merged recently. What is needed to be done in this package?

@rayokota
Copy link

rayokota commented Jun 8, 2021

@shionefaye , the issue still exists in this library. For Schema Registry the code has been changed to compare instances of JsonNode from Jackson rather than instances of Schema from this library.

@shionefaye
Copy link

@shionefaye , the issue still exists in this library. For Schema Registry the code has been changed to compare instances of JsonNode from Jackson rather than instances of Schema from this library.

Got you. I misread your early message and thought this package depends on Conflunece Schema Registry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants