Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to tell protoc to remove omitempty field from json tags when generating .go files #1560

Open
GooDeeJAY opened this issue Sep 3, 2023 · 3 comments

Comments

@GooDeeJAY
Copy link

Problem

When I generate .pb.go files using protoc, its generating structs with json tags where there are omitempty option in each of the fields:

Proto3:

message MyMessage{
    string id = 1;
    MyType type = 2;
    bool isFoo = 3;
}

Generated go struct:

type MyMessage struct {
    ...    
    Id       string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    Type     MyType   `protobuf:"varint,2,opt,name=type,proto3,enum=some_service.MyType" json:"type,omitempty"`
    IsFoo    bool     `protobuf:"varint,3,opt,name=onboarded,proto3" json:"onboarded,omitempty"`
}

Solutions

Option to tell protoc compiler to not generate structs with omitempty json tags (For example some arg like protoc --json-emitall)


Protobuf to support json-tag related options:

For example, on the top of the .proto file:

option json-emitall="true";

Or, support for some per message option like:

message Foo {
    string id = 1;
    
    option json-tags = "";
}

Desired Result

So that I can get:

type MyMessage struct {
    ...    
    Id       string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id"`
    Type     MyType   `protobuf:"varint,2,opt,name=type,proto3,enum=some_service.MyType" json:"type"`
    IsFoo    bool     `protobuf:"varint,3,opt,name=onboarded,proto3" json:"onboarded"`
}
@puellanivis
Copy link
Collaborator

The JSON tags are considered a deprecated feature of protobuf generation, as one should be using the protojson package, instead of the standard library’s encoding/json which has several caveats to its usage that mean that the official proto3 JSON mapping could not be implemented properly with it.

@noxsyc
Copy link

noxsyc commented Mar 19, 2024

The JSON tags are considered a deprecated feature of protobuf generation, as one should be using the protojson package, instead of the standard library’s encoding/json which has several caveats to its usage that mean that the official proto3 JSON mapping could not be implemented properly with it.

What caveats is there? protojson has a lot unexpected behaviour i prefer avoid using it replacing with JSONBuiltin. The only trouble i faced - hardcoded omitempty by the compiler.

@puellanivis
Copy link
Collaborator

What caveats is there?

The caveats that it cannot correctly implement the proto3 JSON mapping standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants