Skip to content

Fix Image dimension DefaultValue metadata - #14895

Draft
breadtitor wants to merge 2 commits into
dotnet:mainfrom
breadtitor:agent/fix-image-default-values
Draft

breadtitor wants to merge 2 commits into
dotnet:mainfrom
breadtitor:agent/fix-image-default-values

Conversation

@breadtitor

@breadtitor breadtitor commented Aug 16, 2026 •

Copy link
Copy Markdown

Fixes dotnet/runtime#126265

Proposed changes

  • Change the DefaultValue metadata for Image.Width and Image.Height from false to integer 0.
  • Add a parameterized TypeDescriptor regression test for both properties.

Customer Impact

  • Metadata consumers no longer receive a Boolean default for integer properties.
  • Prevents invalid-cast failures in consumers such as OpenAPI schema generation.

Regression?

  • No. This corrects invalid design-time metadata; the property values and API surface are unchanged.

Risk

  • Low. The production change is limited to two DefaultValue attributes and is covered by a focused regression test.

Test methodology

  • Built System.Drawing.Common and System.Drawing.Common.Tests with the repository-pinned .NET 11 toolchain.
  • Ran System.Drawing.Tests.ImageTests.ImageDimensionProperty_DefaultValueIsZero: 2 passed, 0 failed.

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: Codex was used to prepare this change; the patch was reviewed by the submitter.

Microsoft Reviewers: Open in CodeFlow

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
@breadtitor

Copy link
Copy Markdown
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

draft draft PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant