"Null values preserved in configuration": source generator and in-memory provider statements don't match .NET 9.0.22 / 10.0.14 behavior

Abierto Apto para principiantes
#56,789 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
1/5
Tiempo estimado
Menos de una hora
Aptitud para principiantes
86/100
Tipo de issue
Documentación
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp

Línea de trabajo

Lee la página de compatibilidad enlazada, especialmente la sección «Non-nullable value types», y compara esa afirmación con la reproducción del problema y el código generado que aparecen en el issue. Corrige la afirmación para que describa cuándo el binder de reflection y el source generator conservan un valor inicial ante valores de configuración nulos o vacíos. La tarea estará terminada cuando la página explique correctamente el comportamiento reportado en .NET 9 y .NET 10.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

:watch: Not Triaged

Page: https://learn.microsoft.com/dotnet/core/compatibility/extensions/10.0/configuration-null-values-preserved (read 2026-11-08)

Statement on the page (section "Non-nullable value types"):

Providers that store a real null value, such as the in-memory provider, already bound null to non-nullable value-type properties by setting the default value, even before .NET 10, and the configuration source generator also matches this behavior.

What we observe: neither part holds when the property has an initial value.

Repro (Microsoft.NET.Sdk.Web, TargetFrameworks net9.0;net10.0, built once with EnableConfigurationBindingGenerator false and once with true):

using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    .AddInMemoryCollection(new Dictionary<string, string?>
    {
        ["Settings:Name"] = "x",
        ["Settings:TimeoutSeconds"] = null,
    })
    .Build();

Settings settings = config.GetSection("Settings").Get<Settings>()!;
Console.WriteLine($"{Environment.Version} TimeoutSeconds = {settings.TimeoutSeconds}");

public sealed class Settings
{
    public string? Name { get; set; }
    public int TimeoutSeconds { get; set; } = 30;
}

Output (SDK 10.0.403, Windows):

Binder .NET 9.0.22 .NET 10.0.14
Reflection 30 0
Source generator 30 30
  • With the in-memory provider, the reflection binder on .NET 9 kept the initial value (30); it did not set default(int).
  • The source generator keeps the initial value on both versions. The generated code assigns a non-nullable value-type property only when the value is neither null nor empty, for example (.NET 10.0.14 generator output):
if (TryGetConfigurationValue(configuration, key: "TimeoutSeconds", out string? value6))
{
    if (!string.IsNullOrEmpty(value6))
    {
        instance.TimeoutSeconds = ParseInt(value6, configuration.GetSection("TimeoutSeconds").Path);
    }
}

The same holds for a JSON null (AddJsonFile): reflection binder 0 on .NET 10, source generator 30 on .NET 9 and .NET 10. So on .NET 10 the same configuration produces different values depending on whether the generator is enabled, which matters for apps where it is on by default (AOT web apps, PublishTrimmed).

Suggested fix: remove or correct the sentence, and mention that the source generator does not bind null (or "") to non-nullable value-type properties, so the initial value is kept.

Lenguaje dominante
Sin datos de lenguaje
Estrellas
4.8k
Forks
6.1k
Merge medio
1 d 4 h
PR fusionados (30 d)
261

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/docs

Todos los issues de dotnet/docs

Issues similares

Más issues de Documentation

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.