Repository navigation
Fix Image dimension DefaultValue metadata - #14895
Draft
breadtitor wants to merge 2 commits into
Draft
breadtitor wants to merge 2 commits into
breadtitor wants to merge 2 commits into
Conversation
The Width and Height properties are integers, but their DefaultValue attributes used false. Use zero and cover both properties through TypeDescriptor metadata. Fix dotnet/runtime#126265
Author
|
@dotnet-policy-service agree |
Shyam-Gupta
added a commit
that referenced
this pull request
Nov 12, 2026
…26265 (#15375) Fixes dotnet/runtime#126265 ## Proposed changes **What:** Change `DefaultValue` metadata for `System.Drawing.Image.Width` and `System.Drawing.Image.Height` from `DefaultValue(false)` to `DefaultValue(0)`. **Why:** `Width`/`Height` are `int` properties but declared `[DefaultValue(false)]`. Metadata consumers that read the default via `TypeDescriptor`/`PropertyDescriptor.Attributes` receive a `Boolean` value. When `Microsoft.AspNetCore.OpenApi` (or `System.Text.Json.Schema.JsonSchema`) generates a schema from a model containing an `Image?` property, it does `UnboxOnWrite<int>(defaultValue)` and throws: ``` InvalidCastException: Unable to cast object of type 'System.Boolean' to type 'System.Int32'. at System.Text.Json.JsonSerializer.UnboxOnWrite<T>(object value) at Microsoft.AspNetCore.OpenApi.JsonNodeSchemaExtensions.ApplyDefaultValue(...) ``` 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`: add `ImageDimensionProperty_DefaultValueIsZero` theory asserting `TypeDescriptor` default is `(int)0` for both properties. - `src/System.Drawing.Common/src/CompatibilitySuppressions.xml`: intentional `CP0015` suppressions for both properties across `net10.0`/`netstandard2.0` baselines (`[T:System.ComponentModel.DefaultValueAttribute]` target). > **Duplicate note:** Draft PR #14895 (`Fix Image dimension DefaultValue metadata`, #14895) covers the same issue and was verified present before opening this PR. This PR is opened per contributor request as a ready-for-review submission of commit `660b4b1` (fix: Correct Image.Width/Height DefaultValue metadata). Please close one as duplicate. ## Customer Impact - Metadata consumers that read `Image.Width`/`Height` defaults via `TypeDescriptor`/`PropertyDescriptor.Attributes` no longer receive a Boolean default for `int` properties. - Fixes `InvalidCastException` in metadata/schema consumers such as OpenAPI generation (`Microsoft.AspNetCore.OpenApi` `ApplyDefaultValue` path). - No runtime behavior change: property values, API surface unchanged; only design-time metadata corrected. ## Regression? - No. This corrects invalid design-time metadata; property values and API surface are unchanged. ## Risk - Low. Production change is limited to two `DefaultValue` attributes (+ netstandard stub parity) and is covered by a focused `TypeDescriptor` regression test. ApiCompat `CP0015` suppressions added intentionally; no API signature change. ## Test methodology - Built `System.Drawing.Common` and `System.Drawing.Common.Tests` with the repo-pinned .NET 11 toolchain. - New regression test `ImageTests.ImageDimensionProperty_DefaultValueIsZero` (2 cases, Width + Height): 2 passed, 0 failed. - Full `System.Drawing.Common.Tests` suite: **5292 passed, 0 failed** (xUnit, Windows x64). - Verified fix direction: `TypeDescriptor.GetProperties(typeof(Image))["Width"/"Height"]` default is now `System.Int32 0`, not `System.Boolean false`. ## Edge cases - `TypeDescriptor` on abstract `Image` (property not abstract): `PropertyDescriptor.Attributes[typeof(DefaultValueAttribute)]` returns `DefaultValueAttribute(0)`; test asserts both `IsType` and value `0` to guard against future type drift. - netstandard stub (`NotSupported.cs`): patched identically so non-Windows stub contract matches Windows implementation; ApiCompat baselines suppressed intentionally (3 suppression sets: `net10.0` baseline, `netstandard2.0` vs `net462`, `netstandard2.0` baseline). - Web API/OpenAPI repro shape (`WeatherForecast.Image`): default lookup now succeeds without cast; consumer unaffected beyond fix. ## Test environment(s) - Windows x64 - .NET SDK `11.0.100-preview.6.26359.118` - Test runtime `11.0.0-rc.1.26411.119` AI assistance: AI (Codex) was used to prepare this change; the patch was reviewed by the submitter. ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15375)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes dotnet/runtime#126265
Proposed changes
DefaultValuemetadata forImage.WidthandImage.Heightfromfalseto integer0.TypeDescriptorregression test for both properties.Customer Impact
Regression?
Risk
DefaultValueattributes and is covered by a focused regression test.Test methodology
System.Drawing.CommonandSystem.Drawing.Common.Testswith the repository-pinned .NET 11 toolchain.System.Drawing.Tests.ImageTests.ImageDimensionProperty_DefaultValueIsZero: 2 passed, 0 failed.Test environment(s)
11.0.100-preview.6.26359.11811.0.0-rc.1.26411.119AI assistance: Codex was used to prepare this change; the patch was reviewed by the submitter.
Microsoft Reviewers: Open in CodeFlow