Skip to content

Proto2 required fields are not serialized when they are the default value #612

Open
@TheLostTree

Description

@TheLostTree
syntax = "proto2";
message SCCharacterLoginResult {
    required int32 resultCode = 1;
    optional int32 loginType = 2;
}
interface SCCharacterLoginResult {
    /**
     * @generated from protobuf field: int32 resultCode = 1;
     */
    resultCode: number;
    /**
     * @generated from protobuf field: optional ELoginType loginType = 2;
     */
    loginType?: number;
}
/*

*/
let obj = {
    resultCode: 0,
    loginType: 0
}
{
    let p : SCCharacterLoginResult = SCCharacterLoginResult.fromJson(obj)
    let r = Buffer.from(SCCharacterLoginResult.toBinary(p))
    console.log(r.toString("hex"))
    //1000
}

import * as pbjs from "protobufjs"

{
    let root = new pbjs.Root();
    let x = root.loadSync("./src/proto/out.proto")
    let pType = x.lookupType("SCCharacterLoginResult")
    let r = Buffer.from(pType.encode(obj).finish())
    console.log(r.toString("hex"))
    //08001000
}

Protobuf-ts doesn't serialize the resultCode field even though it is set. Would it be possible for protobuf-ts to emit code that handels the required in proto2 by removing the check for resultCode == 0?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions