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

asp.net's configuration parsing puts null into non-nullable variables #45045

Open
Trolldemorted opened this issue Nov 21, 2020 · 8 comments
Open

Comments

@Trolldemorted
Copy link

Describe the bug

If I make my code parse a custom configuration section in a <Nullable>enable</Nullable> project, it fills non-nullable references with null values instead of throwing an exception.

To Reproduce

public Startup(IConfiguration configuration)
{
    this.Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MySettings>(this.Configuration.GetSection("MySettings"));
    var mySettings= this.Configuration
        .GetSection("MySettings")
        .Get<LandingPageSettings>();

Add any reference type to MySettings and observe that the parsing does not fail and fills the non-nullable variable with null.

I didn't find anything related in dotnet/aspnetcore#5680 so I created this Issue. This might be caused by #1256, if you are using their parser.

Does asp.net have a way of parsing configs that honors the not-null safety?

Further technical details

  • ASP.NET Core version 5.0
  • Include the output of dotnet --info
.NET SDK (gemäß "global.json"):
 Version:   5.0.100
 Commit:    5044b93829

Laufzeitumgebung:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.2.20475.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
@Tratcher
Copy link
Member

This is expected. Nullable is a compile-time only aid, it does not affect the application at runtime. Configuration is bound dynamically at runtime.

Note the configuration and options APIs live in the runtime repo. I'll transfer this.

@Tratcher Tratcher transferred this issue from dotnet/aspnetcore Nov 21, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Nov 21, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

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.

@ghost
Copy link

ghost commented Nov 21, 2020

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Describe the bug

If I make my code parse a custom configuration section in a <Nullable>enable</Nullable> project, it fills non-nullable references with null values instead of throwing an exception.

To Reproduce

public Startup(IConfiguration configuration)
{
    this.Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MySettings>(this.Configuration.GetSection("MySettings"));
    var mySettings= this.Configuration
        .GetSection("MySettings")
        .Get<LandingPageSettings>();

Add any reference type to MySettings and observe that the parsing does not fail and fills the non-nullable variable with null.

I didn't find anything related in dotnet/aspnetcore#5680 so I created this Issue. This might be caused by #1256, if you are using their parser.

Does asp.net have a way of parsing configs that honors the not-null safety?

Further technical details

  • ASP.NET Core version 5.0
  • Include the output of dotnet --info
.NET SDK (gemäß "global.json"):
 Version:   5.0.100
 Commit:    5044b93829

Laufzeitumgebung:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.2.20475.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
Author: Trolldemorted
Assignees: -
Labels:

area-Extensions-Options, untriaged

Milestone: -

@Trolldemorted
Copy link
Author

@Tratcher as mentioned in #1256, entity framework's runtime behaviour is affected by nullable, so there must be some way to do different things depending on whether a type is nullable or not.

Is there any [Required]-like attribute I can attach to my non-nullable type references which will be respected when parsing an asp.net config?

@wzchua
Copy link
Contributor

wzchua commented Nov 21, 2020

ASP Options pattern has a means to configure validation https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0#options-validation

@maryamariyan
Copy link
Member

cc: @safern

@safern
Copy link
Member

safern commented Nov 24, 2020

Sorry for the delay on responding. Unfortunately, at the moment we don't have an API that can tell us whether a member is a nullable reference type as the underlying type at runtime is the same for non-nullable as for nullable types, the only thing that changes is that the compiler emits metadata in order to capture the nullability. However this metadata is hard to read as it has a lot of edge cases, so in order to support this we would need a proper API that exposes this information via Reflection for example: #29723 -- until we have that API in place, we can't support this without a huge investment on implementing logic to read this metadata, which just seems overkill as the compiler could change the way metadata is represented at any time.

However, I think that if we end up using source generators at compile time, we could get information about nullable reference types for the POCO an app is trying to bind to, and with that info, roslyn could tell us if the type has nullable annotations or not. We currently have this to investigate how source generators could help the binding story: #44493

@safern safern removed the untriaged New issue has not been triaged by the area owner label Nov 24, 2020
@safern safern added this to the Future milestone Nov 24, 2020
@archaeron
Copy link

Hi

it looks to me like this API is now included in dotnet #29723

is there a way to turn this check on?

Thank you :)

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

No branches or pull requests

7 participants