{"record":{"id":"80a754ca70ebe5a6","repo":"apache/pulsar","slug":"invalid-java-object-for-schema-type-type","errorCode":null,"errorMessage":"Invalid Java object for schema type + type +  :  + val.getClass() +  for field : \" + name + \"","messagePattern":"Invalid Java object for schema type \\+ type \\+  :  \\+ val\\.getClass\\(\\) \\+  for field : \" \\+ name \\+ \"","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/client/impl/schema/SchemaUtils.java","lineNumber":126,"sourceCode":"        SCHEMA_TYPE_CLASSES.put(\n                SchemaType.BYTES,\n                Arrays.asList(byte[].class, ByteBuffer.class, ByteBuf.class));\n        // build the reverse mapping\n        SCHEMA_TYPE_CLASSES\n                .forEach((type, classes) -> classes.forEach(clz -> JAVA_CLASS_SCHEMA_TYPES.put(clz, type)));\n    }\n\n    public static void validateFieldSchema(String name,\n                                           SchemaType type,\n                                           Object val) {\n        if (null == val) {\n            return;\n        }\n\n        List<Class<?>> expectedClasses = SCHEMA_TYPE_CLASSES.get(type);\n\n        if (null == expectedClasses) {\n            throw new RuntimeException(\"Invalid Java object for schema type \" + type\n                    + \" : \" + val.getClass()\n                    + \" for field : \\\"\" + name + \"\\\"\");\n        }\n\n        boolean foundMatch = false;\n        for (Class<?> expectedCls : expectedClasses) {\n            if (expectedCls.isInstance(val)) {\n                foundMatch = true;\n                break;\n            }\n        }\n\n        if (!foundMatch) {\n            throw new RuntimeException(\"Invalid Java object for schema type \" + type\n                    + \" : \" + val.getClass()\n                    + \" for field : \\\"\" + name + \"\\\"\");\n        }\n","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/client/impl/schema/SchemaUtils.java#L108-L144","documentation":"SchemaUtils.validateFieldSchema checks that the Java value supplied for a field matches the expected Java classes for the field's SchemaType. When no class list is registered for that schema type at all, it throws RuntimeException 'Invalid Java object for schema type ... for field ...', meaning the type cannot accept any Java object in this validation path.","triggerScenarios":"Calling schema validation (e.g. Avro-based record schema building with SchemaUtils.validateFieldSchema) with a SchemaType that has no entry in SCHEMA_TYPE_CLASSES (such as special/auto types or unsupported types) while passing a non-null value.","commonSituations":"Using AUTO_, KEY_VALUE, JSON, or similar schema types in a manual field-validation flow; building schemas programmatically from POJOs where field type annotation maps to an unsupported SchemaType enum.","solutions":["Use a concrete supported SchemaType (INT8..STRING, BOOLEAN, BYTES, DATE, TIME, TIMESTAMP, etc.) for the field.","Remove manual calls to validateFieldSchema for special types (AUTO/AUTO_CONSUME/AUTO_PUBLISH/KEY_VALUE) that are not meant to validate plain Java objects.","Wrap validation in try/catch for RuntimeException to surface which field/type combination is unsupported, then correct the schema definition."],"exampleFix":"// before\nSchemaUtils.validateFieldSchema(\"f\", val, SchemaType.AUTO_CONSUME, false);\n// after\nSchemaUtils.validateFieldSchema(\"f\", val, SchemaType.INT32, false); // concrete type with registered classes","handlingStrategy":"try-catch","validationCode":"if (val != null && Set.of(SchemaType.AUTO, SchemaType.AUTO_CONSUME, SchemaType.AUTO_PUBLISH,\n        SchemaType.KEY_VALUE, SchemaType.JSON, SchemaType.NONE).contains(type)) {\n    throw new IllegalArgumentException(\"schema type \" + type + \" cannot validate plain Java objects\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    SchemaUtils.validateFieldSchema(name, val, type, required);\n} catch (RuntimeException e) {\n    throw new SchemaException(\"field '\" + name + \"' uses unsupported schema type \" + type, e);\n}","preventionTips":["Use concrete SchemaTypes with registered Java class mappings for field validation.","Never run plain-object field validation for AUTO*/KEY_VALUE/JSON/NONE types.","Catch RuntimeException and surface the field name for faster diagnosis."],"tags":["schema","validation","avro","type-mismatch"],"backgroundTag":"schema-validation-failed","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}