Repository navigation
Description
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
dotnet-policy-service commentedon Mar 28, 2026
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Mar 29, 2026
Tagging subscribers to this area: @dotnet/area-system-drawing
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Mar 29, 2026
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Mar 30, 2026
Tagging subscribers to this area: @dotnet/area-system-drawing
See info in area-owners.md if you want to be subscribed.
fix: Correct Image.Width/Height DefaultValue metadata
Fix Image.Width/Height DefaultValue (false -> 0) for dotnet/runtime#1…
Zheng-Li01 commentedon Nov 19, 2026
Verified the issue with .NET 12.0.0-alpha.1.26568.110, the issue has been fixed as below screenshot.