Skip to content

Fix default value population for renamed constructor parameters - #3120

Open
HengYpinn wants to merge 1 commit into
JamesNK:masterfrom
HengYpinn:fix-3119-renamed-constructor-default
Open

Fix default value population for renamed constructor parameters#3120
HengYpinn wants to merge 1 commit into
JamesNK:masterfrom
HengYpinn:fix-3119-renamed-constructor-default

Conversation

@HengYpinn

@HengYpinn HengYpinn commented Aug 12, 2026

Copy link
Copy Markdown

Fixes #3119.

Related context: #1199.

Summary

  • Resolve an explicitly named constructor parameter with the same naming rules used for properties before matching it to a member.
  • Match missing creator values by resolved JSON name so default-value population reaches the linked constructor parameter.
  • Add regression coverage for get-only properties and for the intentionally unmatched behavior documented in DefaultValueHandling.Populate doesn't set default value when deserializing #1199.

Root cause

Constructor parameters were initially matched to members before the parameter's JsonPropertyAttribute.PropertyName was resolved. Later, missing-value processing compared a resolved constructor JSON name with an underlying CLR member name. Consequently, explicitly assigning the same renamed JSON name to a member and constructor parameter did not reliably link them or inherit the member's default settings.

Compatibility

This preserves the behavior documented in #1199: a constructor parameter named text is not linked to a member serialized as myText unless the parameter explicitly uses [JsonProperty("myText")] (or is itself named myText). The fix does not add a broad CLR-name fallback.

User impact

Immutable models using explicit JsonProperty renames with DefaultValueHandling.Populate now receive their configured constructor default during deserialization instead of null.

Validation

  • dotnet test Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj -f net8.0 (3,519 passed)
  • dotnet test Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj -f net6.0 (3,515 passed)
  • dotnet build Src/Newtonsoft.Json/Newtonsoft.Json.csproj -c Release (all library target frameworks built successfully)

@HengYpinn
HengYpinn force-pushed the fix-3119-renamed-constructor-default branch from a4f9bd3 to 0ea1b0e Compare August 12, 2026 08:47
@HengYpinn
HengYpinn marked this pull request as ready for review August 12, 2026 08:49
@HengYpinn
HengYpinn force-pushed the fix-3119-renamed-constructor-default branch from 0ea1b0e to 04a304a Compare August 12, 2026 09:02
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.

[DefaultValue] applied to a constructor parameter does not work when the parameter has also been renamed by setting JsonPropertyAttribute.PropertyName

1 participant