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

Enum class struct props serialized differently to enum values #5084

Open
qaisjp opened this issue Jan 11, 2022 · 1 comment
Open

Enum class struct props serialized differently to enum values #5084

qaisjp opened this issue Jan 11, 2022 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@qaisjp
Copy link
Collaborator

qaisjp commented Jan 11, 2022

Input

→ View on sorbet.run

# typed: true
extend T::Sig

class Kind < T::Enum
  enums do
    X = new
    Y = new
  end
end

class Foo < T::Struct
  prop :type, Kind
end

class Bar < T::Struct
  prop :type, Kind::X, default: Kind::X
end

a = Foo.new(type: Kind::X).serialize # {"type"=>"x"}
b = Bar.new.serialize # {"type"=>#<Foo::X>}

# $ a == b
# true
#
# Which makes sense, because Kind::X == "x"

Observed output

a = Foo.new(type: Kind::X).serialize # {"type"=>"x"}
b = Bar.new.serialize # {"type"=>#<Foo::X>}

Expected behavior

Bar should be serialized identically to Foo. The only difference that Bar has to Foo, is that Bar only allows Kind::X to be passed, whereas Foo allows any value in Kind.

a = Foo.new(type: Kind::X).serialize # {"type"=>"x"}
b = Bar.new.serialize # {"type"=>"x"}
@qaisjp qaisjp added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels Jan 11, 2022
@qaisjp qaisjp changed the title Enum class struct props serialized differently enum members themselves Enum class struct props serialized differently to enum members themselves Jan 11, 2022
@qaisjp qaisjp changed the title Enum class struct props serialized differently to enum members themselves Enum class struct props serialized differently to enum values Jan 11, 2022
@jez
Copy link
Collaborator

jez commented Jan 11, 2022

It looks like we only do handle_custom_type for the T::Types::Simple case. Sorbet stores the Kind::X types as T::Types::TEnum types, which are not T::Types::Simple

https://github.com/sorbet/sorbet/blob/master/gems/sorbet-runtime/lib/types/props/private/serde_transform.rb#L64-L79

So we need to add a case for T::Types::TEnum, and call handle_custom_type in it.

Also worth noting: I'm not 100% sure that the above file is the only place that the serde code lives in sorbet-runtime right now—it might be duplicated in both the dynamically-generated serde methods as well as another, equivalent place that doesn't use eval, so we would want to double check that.

@jez jez added good first issue Good for newcomers and removed unconfirmed This issue has not yet been confirmed by the Sorbet team labels Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants