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

[Ruby] unable to read custom options from descriptor #16834

Open
cocoahero opened this issue May 13, 2024 · 0 comments
Open

[Ruby] unable to read custom options from descriptor #16834

cocoahero opened this issue May 13, 2024 · 0 comments
Labels
untriaged auto added to all issues by default when created.

Comments

@cocoahero
Copy link

It appears that #1198 has been closed as fixed. However, I am unable to figure out how to actually access any custom options on a message's descriptor. Calling to_h on the MessageOptions object returns {}, but to_json returns a promising value.

Below are some things I have tried, but have not found an access pattern that works.

Example

sandbox.proto

syntax = "proto3";

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions {
  string my_custom_option = 50001;
}

message MyMessage {
  option (my_custom_option) = "hello";
}

sandbox_pb.rb (generated using protoc -I . --ruby_out=. sandbox.proto)

# frozen_string_literal: true
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: sandbox.proto

require 'google/protobuf'

require 'google/protobuf/descriptor_pb'


descriptor_data = "\n\rsandbox.proto\x1a google/protobuf/descriptor.proto\"\x16\n\tMyMessage:\t\x8a\xb5\x18\x05hello:;\n\x10my_custom_option\x12\x1f.google.protobuf.MessageOptions\x18\xd1\x86\x03 \x01(\tb\x06proto3"

pool = Google::Protobuf::DescriptorPool.generated_pool
pool.add_serialized_file(descriptor_data)

MyMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("MyMessage").msgclass
require_relative "sandbox_pb" 
# => true

MyMessage.descriptor.options 
# => <Google::Protobuf::MessageOptions: uninterpreted_option: []>

MyMessage.descriptor.options.to_h
# => {}

MyMessage.descriptor.options.to_json
# => "{\"[my_custom_option]\":\"hello\"}"

MyMessage.descriptor.options["my_custom_option"]
# => nil

MyMessage.descriptor.options["[my_custom_option]"]
# => nil

MyMessage.descriptor.options["(my_custom_option)"]
# => nil

MyMessage.descriptor.options.my_custom_option
# `method_missing': undefined method `my_custom_option' for an instance of Google::Protobuf::MessageOptions (NoMethodError)

Versions:

> ruby --version
ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]

> bundle show google-protobuf
~/.gem/ruby/3.3.1/gems/google-protobuf-4.26.1-arm64-darwin

> protoc --version
libprotoc 26.1
@cocoahero cocoahero added the untriaged auto added to all issues by default when created. label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged auto added to all issues by default when created.
Projects
None yet
Development

No branches or pull requests

1 participant