Skip to content

[BUG] useJsonName=true introduce error code for oneof #1227

@coreyzzp

Description

@coreyzzp

when I attach the option useJsonName=true, I found generated code will be error for oneof field

like this:

message RoomMessage {
    oneof action {
        CreateRoomAction create = 1;
        JoinRoomAction join = 2;
        LeaveRoomAction leave = 3;
    }
}

when useJsonName=true option is attached, the generated code will be: the field should be action instead of create



function createBaseRoomMessage(): RoomMessage {
  return { create: undefined };
}

export const RoomMessage: MessageFns<RoomMessage> = {
  encode(message: RoomMessage, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    switch (message.create?.$case) {
      case "create":
        CreateRoomAction.encode(message.create.value, writer.uint32(10).fork()).join();
        break;
      case "join":
        JoinRoomAction.encode(message.create.value, writer.uint32(18).fork()).join();
        break;
      case "leave":
        LeaveRoomAction.encode(message.create.value, writer.uint32(26).fork()).join();
        break;
    }
    return writer;
  },

when I drop the option the code works well:


function createBaseRoomMessage(): RoomMessage {
  return { action: undefined };
}

export const RoomMessage: MessageFns<RoomMessage> = {
  encode(message: RoomMessage, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    switch (message.action?.$case) {
      case "create":
        CreateRoomAction.encode(message.action.value, writer.uint32(10).fork()).join();
        break;
      case "join":
        JoinRoomAction.encode(message.action.value, writer.uint32(18).fork()).join();
        break;
      case "leave":
        LeaveRoomAction.encode(message.action.value, writer.uint32(26).fork()).join();
        break;
    }
    return writer;
  },

full command:

npx protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=codegen --ts_proto_opt=useJsonWireFormat=true,printObject=true,esModuleInterop=true,useJsonName=true,useNumericEnumForJson=true,forceLong=bigint,oneof=unions-value --proto_path=protos protos/oneof 

npx protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=codegen --ts_proto_opt=useJsonWireFormat=true,printObject=true,esModuleInterop=true,useNumericEnumForJson=true,forceLong=bigint,oneof=unions-value --proto_path=protos protos/oneof 

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