Skip to content

Stack overflow with recursive JSON schema #3779

Open
@Julien-Hanin

Description

@Julien-Hanin

Calling the validator on (some) recursive JSON schemas throws a StackOverflowError. I've tested this code

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import io.confluent.kafka.schemaregistry.SchemaValidatorBuilder;
import io.confluent.kafka.schemaregistry.SimpleParsedSchemaHolder;
import io.confluent.kafka.schemaregistry.json.JsonSchema;

class Main {
  public static void main(String[] args) throws Exception {
    JsonSchema schema = new JsonSchema(new String(Files.readAllBytes(Path.of("SERVICE.json"))));
    new SchemaValidatorBuilder().canReadStrategy().validateLatest().validate(schema, List.of(new SimpleParsedSchemaHolder(schema)));
  }
}

with SERVICE.json being either

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "$defs" : {
    "Permission" : {
      "type" : "object",
      "properties" : {
        "service" : {
          "$ref" : "#"
        }
      }
    }
  },
  "type" : "object",
  "properties" : {
    "permission" : {
      "$ref" : "#/$defs/Permission"
    }
  }
}

or even

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "$defs" : {
    "Permission" : {
      "type" : "object",
      "properties" : {
        "service" : {
          "$ref" : "#"
        }
      }
    }
  },
  "type" : "object",
  "properties" : {
    "permission" : {
      "type" : "string"
    }
  }
}

This results in

Exception in thread "main" java.lang.IllegalArgumentException: Invalid JSON Schema
	at io.confluent.kafka.schemaregistry.json.JsonSchema.rawSchema(JsonSchema.java:399)
	at io.confluent.kafka.schemaregistry.json.JsonSchema.isBackwardCompatible(JsonSchema.java:671)
	at io.confluent.kafka.schemaregistry.SchemaValidatorBuilder.lambda$canReadStrategy$0(SchemaValidatorBuilder.java:51)
	at io.confluent.kafka.schemaregistry.SchemaValidatorBuilder.lambda$validateLatest$3(SchemaValidatorBuilder.java:92)
	at test.Main.main(Main.java:36)
Caused by: java.lang.StackOverflowError
	at java.base/java.util.LinkedHashMap$LinkedKeyIterator.<init>(LinkedHashMap.java:1045)
	at java.base/java.util.LinkedHashMap$LinkedKeySet.iterator(LinkedHashMap.java:700)
	at java.base/java.util.HashSet.iterator(HashSet.java:182)
	at com.github.erosb.jsonsKema.SchemaVisitor.visitChildren(SchemaVisitor.kt:142)
	at io.confluent.kafka.schemaregistry.json.schema.SchemaTranslator.visitChildren(SchemaTranslator.java:176)
	at io.confluent.kafka.schemaregistry.json.schema.SchemaTranslator.visitChildren(SchemaTranslator.java:105)
	at com.github.erosb.jsonsKema.SchemaVisitor.visitCompositeSchema(SchemaVisitor.kt:66)
	at io.confluent.kafka.schemaregistry.json.schema.SchemaTranslator.visitCompositeSchema(SchemaTranslator.java:185)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions