Skip to content

System.Drawing.Image Width and Height declare bool default values #126265

Closed

Description

@sduquette-devolutions

Description

The properties Width and Height of System.Drawing.Image have [DefaultValue(false)], while their type is int (https://github.com/dotnet/dotnet/blob/b0f34d51fccc69fd334253924abd8d6853fad7aa/src/winforms/src/System.Drawing.Common/src/System/Drawing/Image.cs#L426).

I encountered this while trying to add openapi generation to an API. Image shouldn't be serialized in our API definition in the first place, but it's probably worth fixing so that it doesn't cause problems somewhere else.

Reproduction Steps

I'm able to trigger the behavior in Microsoft.AspNetCore.OpenApi, I have tried creating the smallest web api that reproduces the behavior.

using Microsoft.AspNetCore.Mvc;
using System.Drawing;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddOpenApi();

var app = builder.Build();
    
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

public class WeatherForecast
{
    public Image? Image { get; set; } = null;
}

[ApiController]
[Route("test")]
public class WeatherForecastController : ControllerBase
{
    public IEnumerable<WeatherForecast> Get()
    {
        return [];
    }
}

Expected behavior

Width and Height declare a numerical default value: ``[DefaultValue(0)]`

Actual behavior

When visiting http://localhost:5108/openapi/v1.json, OpenApi throws an invalid cast exception.

InvalidCastException: Unable to cast object of type 'System.Boolean' to type 'System.Int32'.

System.Text.Json.JsonSerializer.UnboxOnWrite(object value)
System.Text.Json.Serialization.Metadata.JsonTypeInfo.SerializeAsObject(Utf8JsonWriter writer, object rootValue)
System.Text.Json.JsonSerializer.WriteNodeAsObject(object value, JsonTypeInfo jsonTypeInfo)
Microsoft.AspNetCore.OpenApi.JsonNodeSchemaExtensions.ApplyDefaultValue(JsonNode schema, object defaultValue, JsonTypeInfo jsonTypeInfo)
Microsoft.AspNetCore.OpenApi.OpenApiSchemaService.<.ctor>b__0_2(JsonSchemaExporterContext context, JsonNode schema)
System.Text.Json.Schema.JsonSchema.g__CompleteSchema|100_0(JsonNode schema, ref <>c__DisplayClass100_0 )
[...]

Regression?

I don't know.

Known Workarounds

No response

Configuration

.net 10, on Windows, x64. The platform probably does not matter.

Other information

No response

Activity

  1. added
    untriaged
    on Mar 28, 2026
  2. dotnet-policy-service commented on Mar 28, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  3. dotnet-policy-service commented on Mar 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-drawing
    See info in area-owners.md if you want to be subscribed.

  4. dotnet-policy-service commented on Mar 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  5. dotnet-policy-service commented on Mar 30, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-drawing
    See info in area-owners.md if you want to be subscribed.

  6. added
    help wanted
    and removed
    untriaged
    on Aug 15, 2026
  7. added this to the Future milestone on Aug 15, 2026
  8. added 2 commits that reference this issue on Nov 12, 2026
    660b4b1
    7dd5f84
  9. Zheng-Li01 commented on Nov 19, 2026

    @Zheng-Li01
    Member

    Verified the issue with .NET 12.0.0-alpha.1.26568.110, the issue has been fixed as below screenshot.

    image
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-System.Drawinghelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions