EasyP
CLIBreaking changesRules

Field Same Cardinality

Категории:

  • WIRE+

Правило проверяет, что поля сообщений сохраняют ту же кардинальность (обязательность / optional). Изменение кардинальности поля ломает совместимость по wire‑формату и сгенерированный код: семантика присутствия и ожидания клиентского кода отличаются для optional и required (implicit) полей.

Примеры

Плохой (Breaking)

До:

syntax = "proto3";

package myapi.v1;

message User {
  string name = 1;
  string email = 2;
  int32 age = 3;
}

message CreateUserRequest {
  string name = 1;
  optional string email = 2;
  string phone = 3;
}

После:

syntax = "proto3";

package myapi.v1;

message User {
  string name = 1;
  optional string email = 2; // Было required (implicit), стало optional
  int32 age = 3;
}

message CreateUserRequest {
  string name = 1;
  string email = 2; // Было optional, стало требуемым (implicit required)
  string phone = 3;
}

Ошибка:

user.proto:6:3: Field "2" with name "email" on message "User" became optional. (BREAKING_CHECK)
request.proto:7:3: Field "2" with name "email" on message "CreateUserRequest" became not optional. (BREAKING_CHECK)

Дополнительные примеры

Проблемы миграции Proto2 → Proto3:

// Before (proto2)
syntax = "proto2";

message Order {
  required string id = 1;
  optional string notes = 2;
  repeated string tags = 3;
}

// After (proto3) - BREAKING CHANGES!
syntax = "proto3";

message Order {
  string id = 1;           // BREAKING: required -> implicit optional
  string notes = 2;        // BREAKING: explicit optional -> implicit optional
  repeated string tags = 3; // OK
}

Хороший (Безопасный)

Вместо изменения кардинальности — добавьте новое поле:

syntax = "proto3";

package myapi.v1;

message User {
  string name = 1;
  string email = 2 [deprecated = true]; // Сохраняем старую кардинальность
  int32 age = 3;
  optional string email_optional = 4; // Новое optional поле
}

message CreateUserRequest {
  string name = 1;
  optional string email = 2 [deprecated = true];
  string phone = 3;
  string email_required = 4; // Новое требуемое поле
}

Или создайте новую версию сообщения:

syntax = "proto3";

package myapi.v2;

message User {
  string name = 1;
  optional string email = 2; // Чистая модель в v2
  int32 age = 3;
}

message CreateUserRequest {
  string name = 1;
  string email = 2; // Требуемое в v2
  string phone = 3;
}

Влияние

  • Wire Format: Меняется семантика присутствия — клиенты неправильно ожидают поля
  • Generated Code: Меняются методы доступа / проверки (has, clear)
  • Validation: Правила проверки обязательности переписываются
  • Default Values: Optional и required по‑разному обрабатывают отсутствие значения

Реальный пример

Код клиента ломается при смене кардинальности:

// Before - поле фактически требуется (implicit)
user := &myapi.User{
    Name:  "John",
    Email: "john@example.com",
    Age:   30,
}

if user.Email != "" {
    // Есть значение
}

// After - поле стало optional (pointer)
user := &myapi.User{
    Name: "John",
    Age:  30,
    // Email может отсутствовать
}

if user.Email != nil && *user.Email != "" {
    // Есть значение (новый способ)
}

user.Email = &emailValue // Присваивание указателя вместо строки

Валидация на сервере:

// Before
func validateUser(user *User) error {
    if user.Email == "" {
        return errors.New("email is required")
    }
    return nil
}

// After
func validateUser(user *User) error {
    if user.Email == nil {
        return errors.New("email is required")
    }
    if *user.Email == "" {
        return errors.New("email cannot be empty")
    }
    return nil
}

Стратегия миграции

  1. Добавьте новое поле с нужной кардинальностью:

    string old_email = 2 [deprecated = true];
    optional string new_email = 5;
  2. Период двойной записи — заполняйте оба поля:

    user := &User{
        Name:     "John",
        OldEmail: email,
        NewEmail: &email,
    }
  3. Клиенты постепенно переходят:

    email := user.NewEmail
    if email == nil && user.OldEmail != "" {
        v := user.OldEmail
        email = &v
    }
  4. Удаление старого поля в следующей major‑версии:

    reserved 2, "old_email";
    optional string new_email = 5;

Типовые сценарии

Миграция Proto2 → Proto3

syntax = "proto3";
message Order {
  string id = 1; // BREAKING если был required
}

// Сохранить семантику:
syntax = "proto3";
message Order {
  string id = 1;
  optional string notes = 2;
}

Делать optional поле required

message CreateUserRequest {
  string name = 1;
  string email = 2; // BREAKING (раньше optional)
}

message CreateUserRequest {
  string name = 1;
  optional string email = 2 [deprecated = true];
  string required_email = 3;
}

Делать required поле optional

message User {
  optional string phone = 3; // BREAKING (было implicit required)
}

message User {
  string phone = 3 [deprecated = true];
  optional string phone_optional = 4;
}

Типы кардинальности в Protobuf

Proto3

  • Implicit optional: string name = 1;
  • Explicit optional: optional string email = 2;
  • Repeated: repeated string tags = 3;
  • Map: map<string, string> metadata = 4;

Proto2

  • Required: required string id = 1;
  • Optional: optional string email = 2;
  • Repeated: repeated string tags = 3;

Матрица Breaking изменений

FromToResult
RequiredOptional❌ BREAKING
OptionalRequired❌ BREAKING
ImplicitExplicit❌ BREAKING
ExplicitImplicit❌ BREAKING
SingleRepeated❌ BREAKING
RepeatedSingle❌ BREAKING
FieldMap❌ BREAKING
MapField❌ BREAKING

On this page