3

I have a thrift struct

struct Message {
    1: optional int userID;
    ...
} 

Is it a safe operation to change it to default require-ness?

struct Message {
    1: int userID;
    ...
} 

If I know it's always set? What about "required"?

WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30

1 Answers1

3

As outlined in this answer, there are three degrees of (so-called) requiredness in Thrift:

  • required: must exist on read, must be set on write
  • optional: may or may not be set, entirely optional
  • "default": may not exist on read, always written (unless it is a null pointer )

To answer the question(s) asked:

  1. It is safe to change optional to default (i.e. remove the optional keyword).

  2. Changing optional to required may break compatibility. Unless you make sure all clients/servers are updated accordingly, it may happen that the older side does not supply a value for such a field. In that case, the other end will reject the incoming request or response as incomplete, because that required field is missing from the received data.

For further reading on the subject you may want to consult Diwaker Gupta's highly recommendable "Missing Guide".

JensG
  • 13,148
  • 4
  • 45
  • 55
  • Some very helpful context on default vs optional: https://grokbase.com/t/thrift/user/08azr2zpzv/default-required-optional – nerdherd Jan 15 '19 at 23:00