{"id":"032f9c5007f1077a","repo":"apache/kafka","slug":"is-not-a-long","errorCode":null,"errorMessage":"{} is not a long","messagePattern":"(.+?) is not a long","errorType":"exception","errorClass":"SchemaException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java","lineNumber":1138,"sourceCode":"        }\n    };\n\n    public static final DocumentedType VARLONG = new DocumentedType() {\n        @Override\n        public void write(ByteBuffer buffer, Object o) {\n            ByteUtils.writeVarlong((Long) o, buffer);\n        }\n\n        @Override\n        public Long read(ByteBuffer buffer) {\n            return ByteUtils.readVarlong(buffer);\n        }\n\n        @Override\n        public Long validate(Object item) {\n            if (item instanceof Long)\n                return (Long) item;\n            throw new SchemaException(item + \" is not a long\");\n        }\n\n        public String typeName() {\n            return \"VARLONG\";\n        }\n\n        @Override\n        public int sizeOf(Object o) {\n            return ByteUtils.sizeOfVarlong((Long) o);\n        }\n\n        @Override\n        public String documentation() {\n            return \"Represents an integer between -2<sup>63</sup> and 2<sup>63</sup>-1 inclusive. \" +\n                    \"Encoding follows the variable-length zig-zag encoding from \" +\n                    \" <a href=\\\"https://code.google.com/apis/protocolbuffers/docs/encoding.html\\\"> Google Protocol Buffers</a>.\";\n        }\n    };","sourceCodeStart":1120,"sourceCodeEnd":1156,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java#L1120-L1156","documentation":"Thrown by VARLONG.validate() when the object passed to the schema validator is not a java.lang.Long. VARLONG is the zig-zag variable-length 64-bit integer type used in modern Kafka APIs; validate() is the type-coercion gate invoked during Struct/Schema validation to ensure the value matches the declared type before serialization.","triggerScenarios":"Calling VARLONG.validate(item) (directly or via Schema.validate on a struct containing a VARLONG field) where item is an Integer, BigInteger, String, or any non-Long. Commonly reached when populating a Struct with a primitive int or a boxed Integer for a field declared as VARLONG.","commonSituations":"Passing an int literal into a Struct for a VARLONG field (autoboxes to Integer, not Long). Cross-system data mapping (Avro/Protobuf -> Kafka Struct) that yields BigInteger or String. Unit tests that hand-build Structs without casting offsets/timestamps to long.","solutions":["Ensure the value is a long: cast or wrap explicitly with Long.valueOf(...) / suffix literals with L before placing into the Struct.","When mapping from another schema (Avro, JSON), coerce the source type to java.lang.Long before validation.","For offset/timestamp fields, use the API's typed setters that take long primitives rather than Object.","Add an assertion in your builder code that the field is instanceof Long before calling the schema."],"exampleFix":"// before: int autoboxes to Integer, validate fails\nstruct.set(\"OFFSET\", 5); // VARLONG.validate throws \"5 is not a long\"\n\n// after: explicit long\nstruct.set(\"OFFSET\", 5L);","handlingStrategy":"type-guard","validationCode":"// Narrow before calling VARLONG.validate so the error never fires.\nif (!(item instanceof Long)) {\n    throw new IllegalArgumentException(\n        \"Expected Long, got \" + (item == null ? \"null\" : item.getClass().getName()));\n}\nLong value = (Long) item; // safe\norg.apache.kafka.common.protocol.types.Type.VARLONG.validate(item);","typeGuard":"// Java type guard / narrowing predicate.\nstatic boolean isLong(Object o) {\n    return o instanceof Long;\n}\n\n// Usage:\nif (isLong(item)) {\n    Long v = (Long) item;\n    org.apache.kafka.common.protocol.types.Type.VARLONG.validate(v);\n} else {\n    throw new IllegalArgumentException(\"VARLONG requires Long, got \" + (item == null ? \"null\" : item.getClass()));\n}","tryCatchPattern":null,"preventionTips":["VARLONG.validate only accepts java.lang.Long — an Integer or long primitive boxed differently will be rejected; box with Long.valueOf or cast to (Long).","If you are reading from a map/config source, coerce with Long.parseLong / ((Number) x).longValue() and pass the Long-typed result.","Keep values Long-typed end-to-end in your pipeline; widening from Integer at the boundary is the usual root cause."],"tags":["protocol","serialization","schema","varlong","type-validation"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}