Skip to content

Fix Image.Width/Height DefaultValue (false -> 0) for dotnet/runtime#126265 - #15375

Merged
Shyam-Gupta merged 1 commit into
dotnet:mainfrom
Sudhakar7J:fix-issue-126265-image-default-value
Nov 12, 2026
Merged

Shyam-Gupta merged 1 commit into
dotnet:mainfrom
Sudhakar7J:fix-issue-126265-image-default-value

Conversation

@Sudhakar7J

@Sudhakar7J Sudhakar7J commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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

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
@Sudhakar7J
Sudhakar7J requested a review from a team as a code owner November 6, 2026 19:58
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.79260%. Comparing base (f47a83a) to head (660b4b1).

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           
Flag Coverage Δ
Debug 39.79260% <ø> (ø)
production 42.31695% <ø> (ø)
test 19.92786% <ø> (ø)
unit 42.31695% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 Width and Height defaults from Boolean false to integer 0 in the implementation and netstandard stub.
  • Adds a parameterized TypeDescriptor regression 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

@Shyam-Gupta

Copy link
Copy Markdown
Member

@Sudhakar7J Please complete the Contributor License Agreement(CLA) requirement.

@Sudhakar7J

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@Sudhakar7J

Copy link
Copy Markdown
Contributor Author

@Shyam-Gupta The CLA is now signed and license/cla is green. All dotnet-winforms CI legs pass. Could you take another look when you have a moment? Thanks!

@Shyam-Gupta
Shyam-Gupta merged commit 7dd5f84 into dotnet:main Nov 12, 2026
10 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 13, 2026
@Zheng-Li01

Zheng-Li01 commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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

126265.zip

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants