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

Protobuf: Initializer, getter and setter for optional fields with message subtype should be nilable #966

Conversation

andrewn617
Copy link
Contributor

@andrewn617 andrewn617 commented Jun 3, 2022

Motivation

In protobuf, when an optional field has a message subtype it is nilable. Suppose I have some protobuf like:

Google::Protobuf::DescriptorPool.generated_pool.build do
  add_file("cart.proto", :syntax => :proto3) do
    add_message "Cart" do
      optional :customer_name, :string
      optional :notes,  :message, 2, "google.protobuf.StringValue"
    end
  end
end

Primitives like :string are not nilable. So the following are invalid:

Cart.new(customer_name: nil) # will error
Cart.new.customer_name = nil # will error
Cart.new.customer_name # returns ""

However, submessages are nilable:

cart = Cart.new(notes: nil)
cart.notes = nil
cart.notes # returns nil

Implementation

When assembling the Field struct, I check if the descriptor is an :optional and it is a :message. If it is, I wrap the type in T.nilable().

Tests

I updated the existing test to reflect this case.

@andrewn617 andrewn617 force-pushed the andrewn617-20220603-protobuf-submessage-setters-and-getters-are-nilable branch 3 times, most recently from 40acb28 to 32842c9 Compare June 3, 2022 18:25
Copy link

@RayLLiu RayLLiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to get ci passed. Note, this might have side effect on other components that using protobuf(a lot of rbi will be updated). I think making the not nil to nilable should be safe, but maybe worth to run some tests and give other team that use protobuf a heads up?

@andrewn617 andrewn617 force-pushed the andrewn617-20220603-protobuf-submessage-setters-and-getters-are-nilable branch from 32842c9 to f0d649e Compare June 21, 2022 17:02
@andrewn617
Copy link
Contributor Author

Sorry have been on a burst and then a vacation.

need to get ci passed.

I believe it was just an issue with the pipeline not the branch. Rerunning now.

I think making the not nil to nilable should be safe, but maybe worth to run some tests and give other team that use protobuf a heads up?

Not sure what the right move is here. The existing sigs are not accurate. But this is a move from a more strict sig to a less strict sig (from non-nilable to nilable) so I don't foresee it breaking anything. But I guess it could be an issue if people are relying on sorbet to make these non-nilable, but in that case they aren't really using protobuf properly so I am not sure what the remediation is. Maybe @paracycle has an idea?

Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't verified nullability myself.

@KaanOzkan KaanOzkan requested a review from a team June 21, 2022 20:34
@slinkp slinkp mentioned this pull request Jun 23, 2022
7 tasks
@andrewn617
Copy link
Contributor Author

@KaanOzkan @paracycle Can we get this merged? This is causing pain for us so we are eager to get this change into a new version of the gem.

@KaanOzkan KaanOzkan merged commit 2808edc into main Jul 5, 2022
@KaanOzkan KaanOzkan deleted the andrewn617-20220603-protobuf-submessage-setters-and-getters-are-nilable branch July 5, 2022 18:04
@KaanOzkan
Copy link
Contributor

Applications that use this DSL generator may now see Method does not exist on NilClass component errors. If you're confident it won't be nil during runtime you can wrap the receiver with T.must to resolve them.

@shopify-shipit shopify-shipit bot temporarily deployed to production July 7, 2022 17:53 Inactive
@shopify-shipit shopify-shipit bot temporarily deployed to 0-9-stable August 19, 2022 20:37 Inactive
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

Successfully merging this pull request may close these issues.

None yet

5 participants