| title | Schema Evolution |
|---|---|
| sidebar_position | 7 |
| id | schema_evolution |
| license | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
Apache Fory™ C# supports schema evolution in Compatible(true) mode.
Fory fory = Fory.Builder()
.Compatible(true)
.Build();Compatible mode writes type metadata that allows readers and writers with different struct definitions to interoperate.
using Apache.Fory;
[ForyObject]
public sealed class OneStringField
{
public string? F1 { get; set; }
}
[ForyObject]
public sealed class TwoStringField
{
public string F1 { get; set; } = string.Empty;
public string F2 { get; set; } = string.Empty;
}
Fory fory1 = Fory.Builder().Compatible(true).Build();
fory1.Register<OneStringField>(200);
Fory fory2 = Fory.Builder().Compatible(true).Build();
fory2.Register<TwoStringField>(200);
byte[] payload = fory1.Serialize(new OneStringField { F1 = "hello" });
TwoStringField evolved = fory2.Deserialize<TwoStringField>(payload);
// F2 falls back to default value on reader side.
System.Diagnostics.Debug.Assert(evolved.F1 == "hello");
System.Diagnostics.Debug.Assert(evolved.F2 == string.Empty);If you want strict schema identity checks instead of evolution behavior:
Fory strict = Fory.Builder()
.Compatible(false)
.CheckStructVersion(true)
.Build();This mode throws on schema hash mismatches.
- Use
Compatible(true)for independently deployed services. - Keep stable type IDs across versions.
- Add new fields with safe defaults.
- Use
CheckStructVersion(true)when strict matching is required.