# 当联合字段将null作为第一个元素而非最后一个时，Avro反序列化失败

- **ID:** `data/avro-union-null-ordering`
- **领域:** data
- **类别:** serialization_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

某些Avro库期望null是联合类型中的第一个元素（例如['null', 'string']），而其他库期望它在最后，导致架构兼容性问题。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Apache Avro 1.11.0 | active | — | — |
| Confluent Schema Registry 7.4.0 | active | — | — |
| Kafka 3.5.0 | active | — | — |

## 解决方案

1. ```
   Ensure all Avro schemas use the same union ordering convention: always put null first: {"type": ["null", "string"]}
   ```
2. ```
   Use a custom deserializer that reorders union types: GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(writerSchema, readerSchema) { @Override protected Object read(Object old, Decoder in) throws IOException { return super.read(old, in); } };
   ```

## 无效尝试

- **Setting compatibility to NONE in schema registry** — Changing schema registry compatibility type does not fix the union ordering issue. (80% 失败率)
- **Modifying the data to include null values in a different order** — The null position is determined by the schema, not the data payload. (75% 失败率)
