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

Add support for "optional" field for proto3 files #476

Open
Puneethgr opened this issue May 24, 2021 · 7 comments
Open

Add support for "optional" field for proto3 files #476

Puneethgr opened this issue May 24, 2021 · 7 comments

Comments

@Puneethgr
Copy link

When there is a proto3 file which uses optional field, then protoc command throws an error.

example.proto:

syntax = "proto3";

message Example {
  string name = 1;
  optional int32 age = 2; // optional field is used here
}

Command:

protoc --c_out=. example.proto

Error:

example.proto: is a proto3 file that contains optional fields, 
but code generator protoc-gen-c hasn't been updated to support optional fields in proto3. 
Please ask the owner of this code generator to support proto3 optional.

Solution:

Open /protobuf-c-1.4.0/protoc-c/c_generator.h and add the following lines inside CGenerator class: (Refer this https://chromium.googlesource.com/external/github.com/protocolbuffers/protobuf/+/refs/heads/master/docs/implementing_proto3_presence.md)

  uint64_t GetSupportedFeatures() const override {
    // Indicate that this code generator supports proto3 optional fields.
    return FEATURE_PROTO3_OPTIONAL;
  }

Finally,

the CGenerator class inside c_generator.h should look like this:

class PROTOC_C_EXPORT CGenerator : public CodeGenerator {
 public:
  CGenerator();
  ~CGenerator();

  // implements CodeGenerator ----------------------------------------
  bool Generate(const FileDescriptor* file,
                const std::string& parameter,
                OutputDirectory* output_directory,
                std::string* error) const;
 
  uint64_t GetSupportedFeatures() const override {
    // Indicate that this code generator supports proto3 optional fields.
    return FEATURE_PROTO3_OPTIONAL;
  }

 private:
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CGenerator);
};

At last,

you need to re-install protobuf-c,
go to protobuf-c-1.4.0 directory and run the installation command:
./autogen.sh && ./configure && make && make install

Please add this support into the source code.

@acozzette
Copy link
Contributor

Before updating the generator to have GetSupportedFeatures() return FEATURE_PROTO3_OPTIONAL, it is a good idea to make sure the generator has proper support for handling proto3 optional fields. Otherwise, protoc will run successfully but proto3 optional fields will be treated like single-element oneofs, which probably wouldn't be the ideal API. The oneof trick is a backward-compatibility measure but it's better to add proper support if possible.

cosmo0920 added a commit to cosmo0920/protobuf-c that referenced this issue Sep 30, 2022
This patch is obtained from the issue
protobuf-c#476.

The official documentation is:
https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md

Signed-off-by: Hiroshi Hatake <hatake@calyptia.com>
edsiper pushed a commit to fluent/protobuf-c that referenced this issue Oct 1, 2022
This patch is obtained from the issue
protobuf-c#476.

The official documentation is:
https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md

Signed-off-by: Hiroshi Hatake <hatake@calyptia.com>
@devangvyas90
Copy link

devangvyas90 commented Nov 20, 2022

Hi,
Any progress/update on this?

  1. Is above advised solution good to go or will it have long term compatibility issues?

  2. I am observing below issue while building following the suggested solution. (Note that i am building against protobuf version 3.6.1-4)

In file included from protoc-c/c_generator.cc:63:0:
./protoc-c/c_generator.h:97:12: error: ‘uint64_t google::protobuf::compiler::c::CGenerator::GetSupportedFeatures() const’ marked override, but does not override
uint64_t GetSupportedFeatures() const override {
^
./protoc-c/c_generator.h: In member function ‘uint64_t google::protobuf::compiler::c::CGenerator::GetSupportedFeatures() const’:
./protoc-c/c_generator.h:99:12: error: ‘FEATURE_PROTO3_OPTIONAL’ was not declared in this scope
return FEATURE_PROTO3_OPTIONAL;
^
make: *** [Makefile:1609: protoc-c/protoc_c_protoc_gen_c-c_generator.o] Error 1

@samjgarnham
Copy link

Any progress?
It seems others have attempted this:
master...fluent:protobuf-c:master

@gsstark
Copy link

gsstark commented Feb 17, 2023

Hi, I just want to mention I ran into this problem trying to compile the opentelemetry protobufs. Specifically it seems it's the metrics protobufs that trigger this:

I assume it's this section:

  // min is the minimum value over (start_time, end_time].
  optional double min = 11;

  // max is the maximum value over (start_time, end_time].
  optional double max = 12;

@sudhank29
Copy link

Any progress? It seems others have attempted this: master...fluent:protobuf-c:master

Incorporating this change fixes the issue of not being able to use "optional" with "proto3" syntax but the generated C structs don't have the "has_field" members.

@AxelLin
Copy link

AxelLin commented Dec 5, 2023

Hi @edmonds

Looks like v1.5.0 does not support "optional" field for proto3 files
This issue has been reported for more than 2 years, just wondering if
you have plan to fix this issue?

@luisGallet
Copy link

Any progress on adding support for optional fields for proto3?

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

8 participants