Use Copy Constructor for JsonSerializerSettings#61251
Use Copy Constructor for JsonSerializerSettings#61251BrennanConroy merged 2 commits intodotnet:mainfrom
Conversation
|
@pranavkm the bot hasn't worked out who needs to review this and you were involved in the linked issue. Any chance you could nudge it in the right direction? |
halter73
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
Head branch was pushed to by a user without write access
fc4e126 to
7bac082
Compare
|
I can't work out where the latest lot of build failures are coming from, so I'm hoping it's not me...! |
Fixes dotnet#29532 Fixes an issue where JsonSerializerSettings.DateFormatString overwrites DateFormatHandling. See dotnet#29532 for details
a91eda9 to
037406f
Compare
|
@halter73 I still can't work out why the test is failing but I'm relatively sure it's nothing I'm doing. Do you have any pointers for how to proceed from here? |
There was a problem hiding this comment.
Pull request overview
Updates MVC’s Newtonsoft.Json output formatter to clone JsonSerializerSettings via Json.NET’s copy constructor, avoiding the DateFormatString setter side-effect that can cause DateFormatHandling to be ignored (per #29532).
Changes:
- Replace the formatter’s custom
ShallowCopylogic withnew JsonSerializerSettings(SerializerSettings). - Remove the now-unneeded
ShallowCopyhelper. - Add a regression test covering the new copy-constructor-based behavior (but see comment about strengthening the assertion).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs | Switches settings cloning to Json.NET’s copy constructor and removes ShallowCopy. |
| src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonOutputFormatterTest.cs | Adds a test intended to validate that the copy constructor doesn’t override date formatting behavior. |
|
|
||
| // Act | ||
| var copiedSettings = new JsonSerializerSettings(originalSettings); | ||
|
|
||
| // Assert | ||
| Assert.Equal(originalSettings.DateFormatHandling, copiedSettings.DateFormatHandling); | ||
| Assert.Equal(originalSettings.DateFormatString, copiedSettings.DateFormatString); |
There was a problem hiding this comment.
This test only compares DateFormatHandling/DateFormatString property values. The original bug is caused by a hidden side effect of setting DateFormatString (even to null) which flips an internal "DateFormatString has been set" flag; property equality will still pass even when the bug is present. Please assert the observable behavior instead (e.g., serialize a DateTime using the copied settings or via NewtonsoftJsonOutputFormatter and verify the payload uses MicrosoftDateFormat rather than the default ISO date string).
| // Act | |
| var copiedSettings = new JsonSerializerSettings(originalSettings); | |
| // Assert | |
| Assert.Equal(originalSettings.DateFormatHandling, copiedSettings.DateFormatHandling); | |
| Assert.Equal(originalSettings.DateFormatString, copiedSettings.DateFormatString); | |
| var value = DateTime.UnixEpoch; | |
| // Act | |
| var copiedSettings = new JsonSerializerSettings(originalSettings); | |
| var originalJson = JsonConvert.SerializeObject(value, originalSettings); | |
| var copiedJson = JsonConvert.SerializeObject(value, copiedSettings); | |
| // Assert | |
| Assert.Equal(originalJson, copiedJson); | |
| Assert.Equal(@"""\/Date(0)\/""", copiedJson); |
|
/ba-g unrelated hang |
Fixes #29532
Fixes an issue where JsonSerializerSettings.DateFormatString overwrites DateFormatHandling. See #29532 for details
For more details, open the Copilot Workspace session.