Skip to content

Nullable annotations metadata on non-public members #32804

Closed
@JamesNK

Description

@JamesNK

Description

In ASP.NET Core we want to use nullable annotations metadata to make decisions about how to call user-defined methods and local-methods/delegates. For more information, see #32375

Today that metadata is removed from private members and compiler-generated methods: dotnet/runtime#29723 (comment)

Also we set the flag for the compiler to not emit nullable metadata for non visible outside the assembly APIs; I don't know how interesting it would be for apps to figure out nullability for internal/private APIs of the framework, I guess that is a non-goal for this API but something to add on the docs?

I'm guessing this was done to reduce metadata in assemblies.

Unfortunately, it means that today we can't use that information when calling compiler-generated methods and private methods.

// Nullable IService means if IService is not resolved from DI
// then the method will be called will a null value instead of throwing an error
app.MapPost("/todos", (IService? service) => {
    // stuff
});

Activity

ghost

ghost commented on May 18, 2021

@ghost

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

JamesNK

JamesNK commented on May 18, 2021

@JamesNK
MemberAuthor
jkotas

jkotas commented on May 18, 2021

@jkotas
Member

This option is called nullablePublicOnly and it is set here: https://github.com/dotnet/runtime/blob/main/Directory.Build.props#L268

This option is right for dotnet/runtime since we generally try to avoid reflection and we do not support 3rd party code using reflection against internal implementation details.

If you would like to not set this for ASP.NET and take the size hit, that's your call to make on whether it is the right tradeoff.

transferred this issue fromdotnet/runtimeon May 18, 2021
JamesNK

JamesNK commented on May 18, 2021

@JamesNK
MemberAuthor

The private and compiler-generated methods in this situation aren't written by us. They're from user's applications and we're reflecting on them.

Would that mean the nullablePublicOnly setting must be not set in the ASP.NET Core SDK?

jkotas

jkotas commented on May 18, 2021

@jkotas
Member

I do not see anyting setting nullablePublicOnly in any SDK. Do you have a repro for the problem that you are trying to solve? Maybe you are getting confused by nullablePublicOnly being set for ASP.NET Core repo here

<Features>strict;nullablePublicOnly</Features>
?

JamesNK

JamesNK commented on May 18, 2021

@JamesNK
MemberAuthor

Ahh, I get what is going on now. This is something that we've opted into for assemblies produced by runtime and aspnetcore.

In user app assemblies this flag isn't on so the nullable metadata will always be available.

My confusion was because I was testing this in an aspnetcore test assembly, which has the nullablePublicOnly flag applied to it.

I think the only change here will be to turn nullablePublicOnly on in just the assemblies that are deployed in the SDK, and leave it off in test assemblies. That will allow test assemblies to simulate user's apps more closely.

added
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
on May 18, 2021
removed
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
on May 18, 2021
ghost

ghost commented on May 19, 2021

@ghost

Thanks for contacting us.

We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

halter73

halter73 commented on Jul 8, 2021

@halter73
Member

Closing but the context might be relevant for #32375

ghost locked as resolved and limited conversation to collaborators on Aug 7, 2021
added
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
and removed on Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routing

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @halter73@davidfowl@JamesNK@jkotas@javiercn

        Issue actions

          Nullable annotations metadata on non-public members · Issue #32804 · dotnet/aspnetcore