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

[C#] Update GetExtension to support getting typed value #9655

Merged
merged 6 commits into from
Mar 22, 2022

Conversation

JamesNK
Copy link
Contributor

@JamesNK JamesNK commented Mar 21, 2022

Fixes #9626

  • Improves GetExtension support for unexpected extension values. The error message for getting an extension value with the wrong type is improved.
  • Adds GetExtension support for Extensions that get the value typed as a base type.

@jskeet

@@ -6,7 +6,7 @@
and without the internal visibility from the test project (all of which have caused issues in the past).
-->
<PropertyGroup>
<TargetFrameworks>net45;netstandard1.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard1.1;netstandard2.0</TargetFrameworks>
Copy link
Contributor Author

@JamesNK JamesNK Mar 21, 2022

Choose a reason for hiding this comment

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

Updated to net462 because VS2022 doesn't support net45.

net462 is the oldest supported netfx version in April 2022.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... I haven't had that problem. I've been able to run tests from VS2022 without issues, as far as I'm aware. I'm okay with it though, given that .NET 4.5 (as opposed to 4.5.2) is long out of support, and even 4.5.2 isn't supported for much longer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not a problem if you install VS2022 and VS2019 side-by-side. VS2019 lets you install the bits and VS2022 can then use them.

@JamesNK
Copy link
Contributor Author

JamesNK commented Mar 21, 2022

Test GeneratedCodeCompilesWithOldCsharpCompiler failed on my machine. Not sure why.

Copy link
Contributor

@jskeet jskeet left a comment

Choose a reason for hiding this comment

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

I'm okay with this barring a small matter of assuming generic-ness... but I'm going to ask for another pair of eyes on it before merging.

@@ -6,7 +6,7 @@
and without the internal visibility from the test project (all of which have caused issues in the past).
-->
<PropertyGroup>
<TargetFrameworks>net45;netstandard1.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard1.1;netstandard2.0</TargetFrameworks>
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... I haven't had that problem. I've been able to run tests from VS2022 without issues, as far as I'm aware. I'm okay with it though, given that .NET 4.5 (as opposed to 4.5.2) is long out of support, and even 4.5.2 isn't supported for much longer.

}
else
{
var storedType = value.GetType().GetTypeInfo().GenericTypeArguments[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be worth leaving a comment here explaining why we believe all IExtensionValue values will have generic type arguments - and add a comment to IExtensionValue to advise that anyone creating a non-generic IExtensionValue implementation should change this code. Alternatively, make this code more defensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made it more defensive.

@jskeet
Copy link
Contributor

jskeet commented Mar 21, 2022

Test GeneratedCodeCompilesWithOldCsharpCompiler failed on my machine. Not sure why.

Hmm... I won't be able to look at that right now, but I'll check it tomorrow.

@jskeet
Copy link
Contributor

jskeet commented Mar 21, 2022

Please could you add some more details in the PR description that's suitable for release notes?

@JamesNK
Copy link
Contributor Author

JamesNK commented Mar 21, 2022

Please could you add some more details in the PR description that's suitable for release notes?

Done

Test GeneratedCodeCompilesWithOldCsharpCompiler failed on my machine. Not sure why.

Hmm... I won't be able to look at that right now, but I'll check it tomorrow.

It also fails without any changes (other than updating net451 to net462). Perhaps it is because I only have VS2022 on this machine.

Copy link

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

For completeness maybe improve the method for repeated extensions as well?

  1. At least the error message can be improved in a similar fashion.
  2. Unless I'm missing something, which very well may be: Maybe even check that the actual type of the elements on the RepeatedField is assignable to TValue and return a new RepeatedField<TValue>. This will allow users to get the RepeatedField<object> and do the parsing from bytes here as well.

@JamesNK
Copy link
Contributor Author

JamesNK commented Mar 21, 2022

I updated the get method for repeated values with the improved error message. Creating a new collection doesn't seem right. For example, if a new collection was returned then items added to it wouldn't be in the real collection.

If someone wants to work around this issue they can use the extension method for non-repeated values with a type of IList, then process values as desired.

Copy link

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@jskeet
Copy link
Contributor

jskeet commented Mar 21, 2022

Interestingly, Kokoro is failing the "GeneratedCodeCompilesWithOldCsharpCompiler" test as well - I'll have a look at that tomorrow. But this PR LGTM.

@amanda-tarafa
Copy link

Kokoro is broken in Windows.
There are some specific tests for old compilers: Google.Protobuf.RefStructCompatibilityTest.RunOldCsharpCompilerAndCheckSuccess

and also error CS0006: Metadata file 'bin\Debug\net45\Google.Protobuf.dll' could not be found

Guessing, I think the net45 target being on the tests projects was forcing the Google.Protobuf.dll to be build in net45, and when you removed that, it's not beeing build anymore so the test is failing?

@jskeet or @JamesNK let me know if you want me to look at it.

@JamesNK
Copy link
Contributor Author

JamesNK commented Mar 22, 2022

Thanks for looking at this. Test fixed by changing the path to net462.

@jskeet
Copy link
Contributor

jskeet commented Mar 22, 2022

Aha, well spotted. Right, given that everything's passing, I'll squash and merge this now.

@jskeet jskeet merged commit e5ae3bb into protocolbuffers:master Mar 22, 2022
@jskeet
Copy link
Contributor

jskeet commented Mar 22, 2022

Done - thanks @JamesNK!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C#] MethodOptions.GetExtension cast exception
4 participants