The template code of DerivedClassWithOptionalFields.cs is invalid since in the code from Line 60 to Line 82 the EncodingMask is not correctly taken into account. In case the base class has fields that need to be serialized or deserialized the EncodingMask has to be calculated and serialized/deserialized before any fields a serialized/deserialized. With the current template setup this is not possible.
The order of the fields and encoding mask needs to be in valid order when generating code for derived classes with optional fields.