-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Verify JsonSerializer support for init-only properties #40150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
94 changes: 94 additions & 0 deletions
94
src/libraries/System.Text.Json/tests/Serialization/PropertyVisiblityTests.InitOnly.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Text.Json.Serialization.Tests | ||
{ | ||
public static partial class PropertyVisibilityTests | ||
{ | ||
[Theory] | ||
[InlineData(typeof(ClassWithInitOnlyProperty))] | ||
[InlineData(typeof(StructWithInitOnlyProperty))] | ||
public static void InitOnlyProperties_Work(Type type) | ||
{ | ||
// Init-only property included by default. | ||
object obj = JsonSerializer.Deserialize(@"{""MyInt"":1}", type); | ||
Assert.Equal(1, (int)type.GetProperty("MyInt").GetValue(obj)); | ||
|
||
// Init-only properties can be serialized. | ||
Assert.Equal(@"{""MyInt"":1}", JsonSerializer.Serialize(obj)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(Class_PropertyWith_PrivateInitOnlySetter))] | ||
[InlineData(typeof(Class_PropertyWith_InternalInitOnlySetter))] | ||
[InlineData(typeof(Class_PropertyWith_ProtectedInitOnlySetter))] | ||
public static void NonPublicInitOnlySetter_Without_JsonInclude_Fails(Type type) | ||
{ | ||
// Non-public init-only property setter ignored. | ||
object obj = JsonSerializer.Deserialize(@"{""MyInt"":1}", type); | ||
Assert.Equal(0, (int)type.GetProperty("MyInt").GetValue(obj)); | ||
|
||
// Public getter can be used for serialization. | ||
Assert.Equal(@"{""MyInt"":0}", JsonSerializer.Serialize(obj)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(Class_PropertyWith_PrivateInitOnlySetter_WithAttribute))] | ||
[InlineData(typeof(Class_PropertyWith_InternalInitOnlySetter_WithAttribute))] | ||
[InlineData(typeof(Class_PropertyWith_ProtectedInitOnlySetter_WithAttribute))] | ||
public static void NonPublicInitOnlySetter_With_JsonInclude_Works(Type type) | ||
{ | ||
// Non-public init-only property setter included with [JsonInclude]. | ||
object obj = JsonSerializer.Deserialize(@"{""MyInt"":1}", type); | ||
Assert.Equal(1, (int)type.GetProperty("MyInt").GetValue(obj)); | ||
|
||
// Init-only properties can be serialized. | ||
Assert.Equal(@"{""MyInt"":1}", JsonSerializer.Serialize(obj)); | ||
} | ||
|
||
public class ClassWithInitOnlyProperty | ||
{ | ||
public int MyInt { get; init; } | ||
} | ||
|
||
public struct StructWithInitOnlyProperty | ||
{ | ||
public int MyInt { get; init; } | ||
} | ||
|
||
public class Class_PropertyWith_PrivateInitOnlySetter | ||
{ | ||
public int MyInt { get; private init; } | ||
} | ||
|
||
public class Class_PropertyWith_InternalInitOnlySetter | ||
{ | ||
public int MyInt { get; internal init; } | ||
} | ||
|
||
public class Class_PropertyWith_ProtectedInitOnlySetter | ||
{ | ||
public int MyInt { get; protected init; } | ||
} | ||
|
||
public class Class_PropertyWith_PrivateInitOnlySetter_WithAttribute | ||
{ | ||
[JsonInclude] | ||
public int MyInt { get; private init; } | ||
} | ||
|
||
public class Class_PropertyWith_InternalInitOnlySetter_WithAttribute | ||
{ | ||
[JsonInclude] | ||
public int MyInt { get; internal init; } | ||
} | ||
|
||
public class Class_PropertyWith_ProtectedInitOnlySetter_WithAttribute | ||
{ | ||
[JsonInclude] | ||
public int MyInt { get; protected init; } | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.