Repository navigation
Fix Image.Width/Height DefaultValue (false -> 0) for dotnet/runtime#126265 - #15375
Conversation
Width and Height are integers but declared DefaultValue(false), causing InvalidCastException in metadata consumers such as OpenAPI schema generation (Boolean to Int32). Change DefaultValue(false) to DefaultValue(0) in Image.cs and netstandard NotSupported.cs stub; add TypeDescriptor regression test; add intentional ApiCompat CP0015 suppressions. Fix dotnet/runtime#126265
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15375 +/- ##
=============================================
Coverage 39.79260% 39.79260%
=============================================
Files 246 246
Lines 9836 9836
Branches 1036 1036
=============================================
Hits 3914 3914
Misses 5741 5741
Partials 181 181
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the image-dimension metadata correction.
No open issues 🎉
What changed in this PR
Corrects System.Drawing.Image dimension metadata to address the invalid-cast failure reported in dotnet/runtime#126265, without changing property values or API signatures.
Changes:
- Changes
WidthandHeightdefaults from Booleanfalseto integer0in the implementation and netstandard stub. - Adds a parameterized
TypeDescriptorregression test for both properties. - Adds targeted API compatibility suppressions for the intentional metadata changes.
| File | Description |
|---|---|
src/System.Drawing.Common/tests/System/Drawing/ImageTests.cs |
Tests default-value metadata for both dimensions. |
src/System.Drawing.Common/src/System/Drawing/Image.cs |
Corrects dimension defaults to integer zero. |
src/System.Drawing.Common/src/Special/NotSupported.cs |
Applies matching metadata to the netstandard stub. |
src/System.Drawing.Common/src/CompatibilitySuppressions.xml |
Suppresses expected compatibility diagnostics for the metadata corrections. |
Review effort: Balanced
|
@Sudhakar7J Please complete the Contributor License Agreement(CLA) requirement. |
|
@dotnet-policy-service agree |
|
@Shyam-Gupta The CLA is now signed and |

Fixes dotnet/runtime#126265
Proposed changes
What: Change
DefaultValuemetadata forSystem.Drawing.Image.WidthandSystem.Drawing.Image.HeightfromDefaultValue(false)toDefaultValue(0).Why:
Width/Heightareintproperties but declared[DefaultValue(false)]. Metadata consumers that read the default viaTypeDescriptor/PropertyDescriptor.Attributesreceive aBooleanvalue. WhenMicrosoft.AspNetCore.OpenApi(orSystem.Text.Json.Schema.JsonSchema) generates a schema from a model containing anImage?property, it doesUnboxOnWrite<int>(defaultValue)and throws:This is the exact failure reported in dotnet/runtime#126265.
How:
src/System.Drawing.Common/src/System/Drawing/Image.cs:Width/Height[DefaultValue(false)]→[DefaultValue(0)].src/System.Drawing.Common/src/Special/NotSupported.cs(netstandard stub): same fix for the two stub properties.src/System.Drawing.Common/tests/System/Drawing/ImageTests.cs: addImageDimensionProperty_DefaultValueIsZerotheory assertingTypeDescriptordefault is(int)0for both properties.src/System.Drawing.Common/src/CompatibilitySuppressions.xml: intentionalCP0015suppressions for both properties acrossnet10.0/netstandard2.0baselines ([T:System.ComponentModel.DefaultValueAttribute]target).Customer Impact
Image.Width/Heightdefaults viaTypeDescriptor/PropertyDescriptor.Attributesno longer receive a Boolean default forintproperties.InvalidCastExceptionin metadata/schema consumers such as OpenAPI generation (Microsoft.AspNetCore.OpenApiApplyDefaultValuepath).Regression?
Risk
DefaultValueattributes (+ netstandard stub parity) and is covered by a focusedTypeDescriptorregression test. ApiCompatCP0015suppressions added intentionally; no API signature change.Test methodology
System.Drawing.CommonandSystem.Drawing.Common.Testswith the repo-pinned .NET 11 toolchain.ImageTests.ImageDimensionProperty_DefaultValueIsZero(2 cases, Width + Height): 2 passed, 0 failed.System.Drawing.Common.Testssuite: 5292 passed, 0 failed (xUnit, Windows x64).TypeDescriptor.GetProperties(typeof(Image))["Width"/"Height"]default is nowSystem.Int32 0, notSystem.Boolean false.Edge cases
TypeDescriptoron abstractImage(property not abstract):PropertyDescriptor.Attributes[typeof(DefaultValueAttribute)]returnsDefaultValueAttribute(0); test asserts bothIsTypeand value0to guard against future type drift.NotSupported.cs): patched identically so non-Windows stub contract matches Windows implementation; ApiCompat baselines suppressed intentionally (3 suppression sets:net10.0baseline,netstandard2.0vsnet462,netstandard2.0baseline).WeatherForecast.Image): default lookup now succeeds without cast; consumer unaffected beyond fix.Test environment(s)
11.0.100-preview.6.26359.11811.0.0-rc.1.26411.119AI assistance: AI (Codex) was used to prepare this change; the patch was reviewed by the submitter.
Microsoft Reviewers: Open in CodeFlow