{"id":"a7654e3cb2013a5f","repo":"apache/kafka","slug":"missing-value-for-field-name-which-has-no-def","errorCode":null,"errorMessage":"Missing value for field '${name}' which has no default value.","messagePattern":"Missing value for field '(.+?)' which has no default value\\.","errorType":"exception","errorClass":"SchemaException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/types/Schema.java","lineNumber":113,"sourceCode":"     * values; otherwise, if the schema does not tolerate missing fields, or if missing fields\n     * don't have a default value, a {@code SchemaException} is thrown to signify that mandatory\n     * fields are missing.\n     */\n    @Override\n    public Struct read(ByteBuffer buffer) {\n        if (cachedStruct != null) {\n            return cachedStruct;\n        }\n        Object[] objects = new Object[fields.length];\n        for (int i = 0; i < fields.length; i++) {\n            try {\n                if (tolerateMissingFieldsWithDefaults) {\n                    if (buffer.hasRemaining()) {\n                        objects[i] = fields[i].def.type.read(buffer);\n                    } else if (fields[i].def.hasDefaultValue) {\n                        objects[i] = fields[i].def.defaultValue;\n                    } else {\n                        throw new SchemaException(\"Missing value for field '\" + fields[i].def.name +\n                                \"' which has no default value.\");\n                    }\n                } else {\n                    objects[i] = fields[i].def.type.read(buffer);\n                }\n            } catch (Exception e) {\n                throw new SchemaException(\"Error reading field '\" + fields[i].def.name + \"': \" +\n                                          (e.getMessage() == null ? e.getClass().getName() : e.getMessage()));\n            }\n        }\n        return new Struct(this, objects);\n    }\n\n    /**\n     * The size of the given record\n     */\n    @Override\n    public int sizeOf(Object o) {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/types/Schema.java#L95-L131","documentation":"Thrown by Schema.read when the schema was constructed with tolerateMissingFieldsWithDefaults=true, the ByteBuffer is exhausted before all fields are read, and the next field has no default value. This lenient mode lets readers accept older payloads that omit trailing optional fields, but a missing mandatory field is still fatal.","triggerScenarios":"Schema.read loops fields; when tolerateMissingFieldsWithDefaults is set, !buffer.hasRemaining() && !field.def.hasDefaultValue triggers the exception. Hit when a producer serializes fewer trailing fields than the reader's schema declares and one of the omitted fields lacks a default.","commonSituations":"Forward-incompatible schema evolution: a new mandatory field was added without a default while older clients still send the prior layout; an internal caller constructing a Schema(true, ...) for v0/v1 feature gating forgot to set defaults; test fixtures serialized with an older code path.","solutions":["Add a default value to the declared Field so omitted payloads decode (Field.for(...).withDefault(...)).","Ensure producers and consumers are on compatible versions; if a mandatory field was added, roll out producers before consumers.","If reading an older persisted payload, pin the reader's Schema to the version that wrote it instead of the latest.","Audit the message JSON specs and regenerate schemas (./gradlew processMessages) so nullable/optional fields carry defaults."],"exampleFix":"// before - mandatory field added without default\nnew Schema(true,\n    new Field(\"replica_id\", INT32),\n    new Field(\"new_mandatory\", INT32))\n\n// after - give it a default\nnew Schema(true,\n    new Field(\"replica_id\", INT32),\n    new Field(\"new_mandatory\", INT32).withDefault(0))","handlingStrategy":"validation","validationCode":"// You control the Schema definition. If a field may be absent on the wire,\n// give it a default so read() falls back instead of throwing.\nSchema schema = new Schema(\n    Field.forInt32(\"required_field\"),                       // mandatory\n    Field.forInt32(\"optional_field\").withDefault(0)         // absent-tolerant\n);\n// OR, if you cannot change the schema, ensure the buffer carries every field:\nif (!buffer.hasRemaining()) {\n    throw new EOFException(\"buffer ended before mandatory field\");\n}\n// Prefer new Schema(true, fields...) so trailing optional fields with defaults\n// are tolerated automatically:\nSchema tolerant = new Schema(true, /* fields with defaults */ );","typeGuard":null,"tryCatchPattern":"try {\n    Struct s = (Struct) schema.read(buffer);\n} catch (org.apache.kafka.common.protocol.types.SchemaException e) {\n    if (e.getMessage().contains(\"has no default value\")) {\n        // buffer was shorter than the schema -> version/encoding mismatch.\n        // Re-read with a tolerant schema or reject the frame.\n        throw new IncompleteFrameException(e);\n    }\n    throw e;\n}","preventionTips":["Define defaults on any field that older peers may omit; trailing fields without defaults cause exactly this error.","Use Schema(true, fields...) when reading from sources known to drop trailing optional fields.","Match the reader schema's field count to the version advertised by the sender.","Test deserialization against the minimum-length byte stream from the oldest supported peer."],"tags":["protocol","serialization","schema-evolution","defaults","kafka-clients"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}