apache/pulsar · error · RuntimeException

Cannot disable validation of default values

Error message

Cannot disable validation of default values

What it means

When building an Avro schema via createAvroSchema, Pulsar reflectively accesses the internal static field Schema.VALIDATE_DEFAULTS (a ThreadLocal<Boolean>) from the Avro library so it can disable validation of default values for compatibility. If that field cannot be found or accessed, a RuntimeException is thrown. This typically means the Avro version on the classpath differs from the one Pulsar was compiled against.

Source

Thrown at pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/util/SchemaUtil.java:82

                .name("")
                .type(schemaType).build();
    }

    @SuppressWarnings("unchecked")
    public static Schema createAvroSchema(SchemaDefinition schemaDefinition) {
        Class pojo = schemaDefinition.getPojo();

        if (StringUtils.isNotBlank(schemaDefinition.getJsonDef())) {
            return parseAvroSchema(schemaDefinition.getJsonDef());
        } else if (pojo != null) {
            ThreadLocal<Boolean> validateDefaults = null;

            try {
                Field validateDefaultsField = Schema.class.getDeclaredField("VALIDATE_DEFAULTS");
                validateDefaultsField.setAccessible(true);
                validateDefaults = (ThreadLocal<Boolean>) validateDefaultsField.get(null);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException("Cannot disable validation of default values", e);
            }

            final boolean savedValidateDefaults = validateDefaults.get();

            try {
                // Disable validation of default values for compatibility
                validateDefaults.set(false);
                return extractAvroSchema(schemaDefinition, pojo);
            } finally {
                validateDefaults.set(savedValidateDefaults);
            }
        } else {
            throw new RuntimeException("Schema definition must specify pojo class or schema json definition");
        }
    }

    public static Schema extractAvroSchema(SchemaDefinition schemaDefinition, Class pojo) {
        try {

View on GitHub (pinned to 820761864e)

Solutions

  1. Align the Avro dependency version with the one required by your Pulsar client version (check pulsar-client's avro dependency in its pom).
  2. Add JVM args like --add-opens org.apache.avro:org.apache.avro.schema if reflective access is blocked by modules.
  3. Check for conflicting Avro jars on the classpath (mvn dependency:tree / shading conflicts) and exclude duplicates.
  4. As a workaround, define the schema from an explicit JSON schema string (Schema ParseSchemaInfo path) instead of the pojo reflection path.

Example fix

// before (pom.xml)
<dependency><groupId>org.apache.avro</groupId><artifactId>avro</artifactId><version>1.11.0</version></dependency>
// after
<dependency><groupId>org.apache.avro</groupId><artifactId>avro</artifactId><version>1.11.3</version></dependency> <!-- version matching pulsar-client -->
Defensive patterns

Strategy: try-catch

Validate before calling

try {
    Class.forName("org.apache.avro.Schema")
         .getDeclaredField("VALIDATE_DEFAULTS");
} catch (NoSuchFieldException e) {
    throw new IllegalStateException("Avro on classpath lacks VALIDATE_DEFAULTS; fix Avro version");
}

Try / catch

try {
    Schema<Event> s = Schema.AVRO(Event.class);
} catch (RuntimeException e) {
    if (e.getMessage().contains("Cannot disable validation of default values")) {
        log.error("Avro/Pulsar version mismatch", e); // fix avro dependency
    }
}

Prevention

When it happens

Trigger: Calling Schema.AVRO(SomePojo.class) (or JSONSchema/other createAvroSchema paths) when the runtime Avro library lacks the VALIDATE_DEFAULTS field or blocks reflective access (Java module strong encapsulation / SecurityManager).

Common situations: Pulsar client and Avro version mismatch after upgrading Avro (field renamed/removed in Avro 1.12+); shaded/uber jars bundling the wrong Avro; running on a JDK with strict module access blocking setAccessible.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/ea59a1fe9c4cd9b5. Report an issue: GitHub.