{"record":{"id":"c415569eddbb3383","repo":"apache/beam","slug":"incorrectly-sized-byte-array","errorCode":null,"errorMessage":"Incorrectly sized byte array.","messagePattern":"Incorrectly sized byte array\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java","lineNumber":1342,"sourceCode":"        } else if (typeWithNullability.type.getType() == org.apache.avro.Schema.Type.LONG) {\n          ReadableInstant instant = (ReadableInstant) value;\n          return (long) instant.getMillis();\n        } else {\n          throw new IllegalArgumentException(\n              \"Can't represent \" + fieldType + \" as \" + typeWithNullability.type.getType());\n        }\n\n      case BYTES:\n        return ByteBuffer.wrap((byte[]) value);\n\n      case LOGICAL_TYPE:\n        String identifier = checkNotNull(fieldType.getLogicalType()).getIdentifier();\n        if (FixedBytes.IDENTIFIER.equals(identifier)) {\n          FixedBytesField fixedBytesField =\n              checkNotNull(FixedBytesField.fromBeamFieldType(fieldType));\n          byte[] byteArray = (byte[]) value;\n          if (byteArray.length != fixedBytesField.getSize()) {\n            throw new IllegalArgumentException(\"Incorrectly sized byte array.\");\n          }\n          return NullnessCheckerWorkarounds.createFixed(\n              null, (byte[]) value, typeWithNullability.type);\n        } else if (VariableBytes.IDENTIFIER.equals(identifier)) {\n          return NullnessCheckerWorkarounds.createFixed(\n              null, (byte[]) value, typeWithNullability.type);\n        } else if (FixedString.IDENTIFIER.equals(identifier)\n            || \"CHAR\".equals(identifier)\n            || \"NCHAR\".equals(identifier)) {\n          return new Utf8((String) value);\n        } else if (VariableString.IDENTIFIER.equals(identifier)\n            || \"NVARCHAR\".equals(identifier)\n            || \"VARCHAR\".equals(identifier)\n            || \"LONGNVARCHAR\".equals(identifier)\n            || \"LONGVARCHAR\".equals(identifier)) {\n          return new Utf8((String) value);\n        } else if (EnumerationType.IDENTIFIER.equals(identifier)) {\n          EnumerationType enumerationType = fieldType.getLogicalType(EnumerationType.class);","sourceCodeStart":1324,"sourceCodeEnd":1360,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java#L1324-L1360","documentation":"Thrown by AvroUtils.genericFromBeamField when converting a Beam FixedBytes logical-type field to an Avro fixed schema: the byte[] value's length does not equal the declared fixed size (FixedBytesField.getSize()). Avro 'fixed' values must be exactly the declared size, so short or long arrays are rejected.","triggerScenarios":"Converting a Beam Row whose field is a FixedBytes logical type to a GenericRecord where value is a byte[] with length != fixed size — e.g. writing a 5-byte array into a fixed(8) Avro field, or padding/truncation was skipped upstream.","commonSituations":"Reading fixed-size values from a database or file into byte[] and forgetting to pad to the declared length; changing the fixed size in the Beam schema (FixedBytes.of(n)) without updating producer code; deserializing data produced under an older, different fixed size.","solutions":["Pad or truncate the byte[] to the exact fixed size before conversion (e.g. Arrays.copyOf(byteArray, fixedSize))","Verify the producer writes arrays of exactly FixedBytesField.getSize() bytes","Update the Beam FixedBytes.of(n) size (and Avro fixed size) to match the actual data length","Switch to the VariableBytes logical type if lengths genuinely vary"],"exampleFix":"// before\nbyte[] value = Arrays.copyOfRange(raw, 0, 5); // fixed size is 8\nrow = row.withValue(\"digest\", value);\n// after\nbyte[] value = Arrays.copyOf(raw, 8); // pad to fixed size 8\nrow = row.withValue(\"digest\", value);","handlingStrategy":"validation","validationCode":"boolean hasFixedSize(byte[] value, Schema.FieldType fieldType) {\n  FixedBytesField f = FixedBytesField.fromBeamFieldType(fieldType);\n  return f != null && value.length == f.getSize();\n}","typeGuard":null,"tryCatchPattern":"try {\n  GenericRecord record = AvroUtils.toGenericRecord(row, avroSchema);\n} catch (IllegalArgumentException e) {\n  if (\"Incorrectly sized byte array.\".equals(e.getMessage())) {\n    throw new IllegalStateException(\"Pad/trim byte arrays to the declared fixed size before conversion\", e);\n  } else { throw e; }\n}","preventionTips":["Validate byte[] lengths at Row-construction time, not at Avro conversion time","Keep FixedBytes.of(n) sizes and Avro fixed(n) sizes defined from one shared constant","If lengths genuinely vary, use VariableBytes instead of FixedBytes"],"tags":["java","avro","beam-schema","fixed-bytes","conversion"],"backgroundTag":"shape-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}