{"record":{"id":"d7f4e663713ee519","repo":"apache/pulsar","slug":"size-of-data-received-by-booleanschema-is-not-1","errorCode":null,"errorMessage":"Size of data received by BooleanSchema is not 1","messagePattern":"Size of data received by BooleanSchema is not 1","errorType":"exception","errorClass":"SchemaSerializationException","httpStatus":null,"severity":"error","filePath":"pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/BooleanSchema.java","lineNumber":49,"sourceCode":"    private static final BooleanSchema INSTANCE;\n    private static final SchemaInfo SCHEMA_INFO;\n\n    static {\n        SCHEMA_INFO = SchemaInfoImpl.builder()\n                .name(\"Boolean\")\n                .type(SchemaType.BOOLEAN)\n                .schema(new byte[0]).build();\n        INSTANCE = new BooleanSchema();\n    }\n\n    public static BooleanSchema of() {\n        return INSTANCE;\n    }\n\n    @Override\n    public void validate(byte[] message) {\n        if (message.length != 1) {\n            throw new SchemaSerializationException(\"Size of data received by BooleanSchema is not 1\");\n        }\n    }\n\n    @Override\n    public byte[] encode(Boolean message) {\n        if (null == message) {\n            return null;\n        } else {\n            return new byte[]{(byte) (message ? 1 : 0)};\n        }\n    }\n\n    @Override\n    public Boolean decode(byte[] bytes) {\n        if (null == bytes) {\n            return null;\n        }\n        validate(bytes);","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/BooleanSchema.java#L31-L67","documentation":"BooleanSchema encodes booleans as exactly one byte. validate() rejects any payload whose length differs from 1 with SchemaSerializationException, so decoding/validating a message of the wrong size fails fast instead of producing garbage values.","triggerScenarios":"Passing a byte[] of length 0 or >1 into Schema.BOOLEAN().validate(...) or a corrupted/wrongly-encoded payload into the decode path of a BOOLEAN schema topic.","commonSituations":"Publishing to a boolean-schema topic with a mismatched schema (e.g. sending strings or multi-byte data); hand-crafted payloads in tests or bridges from other systems producing non-1-byte booleans.","solutions":["Ensure producers use Schema.BOOLEAN so values are encoded as exactly 1 byte","Check the payload length is 1 before calling validate/decode","Verify the topic's schema matches what producers actually publish (use broker schema validation enforce)."],"exampleFix":"// before\nbooleanSchema.validate(payload); // throws if payload.length != 1\n// after\nif (payload != null && payload.length == 1) {\n    booleanSchema.validate(payload);\n}","handlingStrategy":"validation","validationCode":"if (message == null || message.length != 1) {\n    throw new IllegalArgumentException(\"Boolean payload must be exactly 1 byte\");\n}","typeGuard":"boolean isValidBooleanPayload(byte[] msg) {\n    return msg != null && msg.length == 1;\n}","tryCatchPattern":"try {\n    schema.validate(message);\n} catch (SchemaSerializationException e) {\n    log.warn(\"Skipping malformed boolean payload of length {}\", message == null ? -1 : message.length);\n}","preventionTips":["Always publish boolean topics with Schema.BOOLEAN","Enable broker schema validation enforcement so mismatched producers fail at publish time","Sanitize payloads coming from external bridges before decoding"],"tags":["pulsar","schema","boolean","validation","serialization"],"backgroundTag":"schema-payload-size-mismatch","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"}