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

Don't overstep on IMessage serialization #8363

Merged
merged 1 commit into from
Apr 7, 2023

Conversation

jsteinich
Copy link
Contributor

@jsteinich jsteinich commented Apr 3, 2023

The ProtobufCodec will try to claim ownership of IMessage serialization. The result is an exception when trying to access the parser as it isn't a concrete class.

This PR will allow the IMessage interface serialization to instead be handled by another serializer (AbstractTypeSerializer by default).
It also adds a case IMessage<T> since T is the concrete type.

Microsoft Reviewers: Open in CodeFlow

if (type == MessageType)
{
// Not a concrete implementation, so not directly serializable
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe just returning true here and not changing IsTypeAllowed would make more sense.

// Not a concrete implementation, but the generic type does give the concrete type
type = type.GenericTypeArguments[0];
}

if (!MessageParsers.ContainsKey(type.TypeHandle))
{
if (Activator.CreateInstance(type) is not IMessage protobufMessageInstance)
Copy link
Member

Choose a reason for hiding this comment

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

It seems that this check could be performed via type.IsAssignableTo(typeof(IMessage)) without invoking the ctor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method is really making sure that a message parser can be obtained (and caching that value) as opposed to just verifying the interface. The only other way to obtain the parser is via reflection.

@ReubenBond ReubenBond merged commit 5ea07d6 into dotnet:main Apr 7, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants