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

Undefined enum number doesn't default to UNKNOWN #1516

Open
Mido-sys opened this issue Jan 17, 2023 · 3 comments
Open

Undefined enum number doesn't default to UNKNOWN #1516

Mido-sys opened this issue Jan 17, 2023 · 3 comments

Comments

@Mido-sys
Copy link

Mido-sys commented Jan 17, 2023

What version of protobuf and what language are you using?

Protobuf v3
protoc-gen-go v1.28.1
protoc v3.15.8
Go 1.18.1

What did you do?
PROTO File

message Product {
   enum STATUS{
      UNKNOWN =0;
      ACTIVE 1;
     DISABLED = 2;
  }
  
  STATUS product_status =1;
}


Golang Client

req := &pb.Product{}
req.ProductStatus = 100;

Golang Server

log.Println(req.ProductStatus == pb.Product_UNKNOWN) // Should be TRUE, but I get `100`


What did you expect to see?
The req.ProductStatus should have a status of pb.Product_UNKNOWN, which is the default value when it can't match the value
What did you see instead?
I get 100 when I do in the server req.GetProductStatus() req.ProductStatus or req.GetProductStatus().String

@Mido-sys Mido-sys changed the title Undefined enum number doesn't default to UNKWON Undefined enum number doesn't default to UNKNOWN Jan 17, 2023
@Mido-sys Mido-sys changed the title Undefined enum number doesn't default to UNKNOWN Undefined enum number doesn't default to UNKNOWN or 0 Jan 17, 2023
@Mido-sys Mido-sys changed the title Undefined enum number doesn't default to UNKNOWN or 0 Undefined enum number doesn't default to UNKNOWN Jan 17, 2023
@puellanivis
Copy link
Collaborator

This is working as intended:

During deserialization, unrecognized enum values will be preserved in the message, though how this is represented when the message is deserialized is language-dependent. In languages that support open enum types with values outside the range of specified symbols, such as C++ and Go, the unknown enum value is simply stored as its underlying integer representation.

https://developers.google.com/protocol-buffers/docs/proto3#enum

@Mido-sys
Copy link
Author

@puellanivis, Thanks for getting back to me. Why can't the enum getter handle this? It already does return the default if x == nil?
So something like this

func (x *Product) GetStatus() Status {
  if x != nil {
    if _, ok := Status_value[int32(x.TransactionType)]; ok {
	return x.TransactionType
     }

   }
  return Product_UNKNOWN
}

@puellanivis
Copy link
Collaborator

It returns the default value if x == nil because the default value of a message is a message wherein all its fields are their default values. Thus the value of (*Product)(nil).GetStatus() would have to be the default value of the enum, which by proto3 is defined as the first entry, which must be = 0. That this is Product_UNKNOWN here, is merely a habit of convenience. But for a common case like enum MobileType { IOS = 0; ANDROID = 1; } the default value would then be IOS and not an undefined value.

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

2 participants