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

$id and $ref with same value will lead to StackOverFlowError when visit combined schema. #464

Open
counter2015 opened this issue Aug 16, 2022 · 2 comments

Comments

@counter2015
Copy link

The schema file

{
  "$id": "https://raw.githubusercontent.com/cloudevents/spec/main/cloudevents/formats/cloudevents.json",
  "allOf": [
    {
      "$ref": "https://raw.githubusercontent.com/cloudevents/spec/main/cloudevents/formats/cloudevents.json"
    }
  ]
}

The code

import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;

public class Bug {

    static void check() {
        try (var inputStream = Bug.class.getResourceAsStream("schema.json")) {
            var rawSchema = new JSONObject(new JSONTokener(inputStream));
            var schema = SchemaLoader.builder().schemaJson(rawSchema).draftV7Support().build().load().build();
            schema.validate(new JSONObject("{\"hello\" : \"world\"}"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        check();
    }
}

It will raise error like following

Exception in thread "main" java.lang.StackOverflowError
	at org.everit.json.schema.ValidatingVisitor.visitCombinedSchema(ValidatingVisitor.java:160)
	at org.everit.json.schema.CombinedSchema.accept(CombinedSchema.java:200)
	at org.everit.json.schema.Visitor.visit(Visitor.java:48)
	at org.everit.json.schema.ValidatingVisitor.visit(ValidatingVisitor.java:59)
	at org.everit.json.schema.ValidatingVisitor.lambda$getFailureOfSchema$2(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidationFailureReporter.inContextOfSchema(ValidationFailureReporter.java:34)
	at org.everit.json.schema.CollectingFailureReporter.inContextOfSchema(CollectingFailureReporter.java:25)
	at org.everit.json.schema.ValidatingVisitor.getFailureOfSchema(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidatingVisitor.visitReferenceSchema(ValidatingVisitor.java:139)
	at org.everit.json.schema.ReferenceSchema.accept(ReferenceSchema.java:168)
	at org.everit.json.schema.Visitor.visit(Visitor.java:48)
	at org.everit.json.schema.ValidatingVisitor.visit(ValidatingVisitor.java:59)
	at org.everit.json.schema.ValidatingVisitor.lambda$getFailureOfSchema$2(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidationFailureReporter.inContextOfSchema(ValidationFailureReporter.java:34)
	at org.everit.json.schema.CollectingFailureReporter.inContextOfSchema(CollectingFailureReporter.java:25)
	at org.everit.json.schema.ValidatingVisitor.getFailureOfSchema(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidatingVisitor.visitCombinedSchema(ValidatingVisitor.java:164)
	at org.everit.json.schema.CombinedSchema.accept(CombinedSchema.java:200)
	at org.everit.json.schema.Visitor.visit(Visitor.java:48)
	at org.everit.json.schema.ValidatingVisitor.visit(ValidatingVisitor.java:59)
	at org.everit.json.schema.ValidatingVisitor.lambda$getFailureOfSchema$2(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidationFailureReporter.inContextOfSchema(ValidationFailureReporter.java:34)
	at org.everit.json.schema.CollectingFailureReporter.inContextOfSchema(CollectingFailureReporter.java:25)
	at org.everit.json.schema.ValidatingVisitor.getFailureOfSchema(ValidatingVisitor.java:199)
	at org.everit.json.schema.ValidatingVisitor.visitReferenceSchema(ValidatingVisitor.java:139)
	at org.everit.json.schema.ReferenceSchema.accept(ReferenceSchema.java:168)
	at org.everit.json.schema.Visitor.visit(Visitor.java:48)

lib version: 1.14.1

@erosb
Copy link
Contributor

erosb commented Aug 18, 2022

Hi! As a quick workaround I suggest using "$ref": "#".

@counter2015
Copy link
Author

counter2015 commented Aug 18, 2022

My original schema is a very large schema, it will refer to another schema by allOf and $ref, and for browser viewing reason, we add $id(as absolute-URI) field on sub-schema.

My workaournd is remove $id fiield after parse it to json object since I only use it to valid data,

IMO, $id property here should not be visited since in spec

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

2 participants