Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,14 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe
? @$"jsonPropertyName: ""{memberMetadata.JsonPropertyName}"""
: "jsonPropertyName: null";

string getterNamedArg = memberMetadata.CanUseGetter
string getterNamedArg = memberMetadata.CanUseGetter &&
memberMetadata.DefaultIgnoreCondition != JsonIgnoreCondition.Always
Copy link
Member

Choose a reason for hiding this comment

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

Curious, how does the presence of ObsoleteAttribute influence the value of memberMetadata.DefaultIgnoreCondition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the issue this generated code is shown:

    getter: static (obj) => ((global::MyLibrary.Models.ClassA)obj).Foo,
    setter: static (obj, value) => ((global::MyLibrary.Models.ClassA)obj).Foo = value!,

Since the property Foo is used but obsolete it generates a compiler warning.

? $"getter: static (obj) => (({declaringTypeCompilableName})obj).{clrPropertyName}"
: "getter: null";

string setterNamedArg;
if (memberMetadata.CanUseSetter)
if (memberMetadata.CanUseSetter &&
memberMetadata.DefaultIgnoreCondition != JsonIgnoreCondition.Always)
{
string propMutation = typeGenerationSpec.IsValueType
? @$"{UnsafeTypeRef}.Unbox<{declaringTypeCompilableName}>(obj).{clrPropertyName} = value!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ public async Task Ignore_BasePublicPropertyIgnored_ConflictWithDerivedPrivate()
Assert.Equal("NewDefaultValue", ((ClassWithIgnoredPublicPropertyAndNewSlotPrivate)obj).MyString);
}

[Fact]
public async Task Ignore_VerifyNoReferenceToGetterAndSetter()
{
// Serialize
var obj = new ClassWithObsoleteAndIgnoredProperty();
string json = await JsonSerializerWrapperForString.SerializeWrapper(obj);

Assert.Equal(@"{}", json);

// Deserialize
json = @"{""MyString_Obsolete"":""NewValue""}";
obj = await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithObsoleteAndIgnoredProperty>(json);

#pragma warning disable CS0618 // Type or member is obsolete
Assert.Equal("DefaultValue", obj.MyString_Obsolete);
#pragma warning restore CS0618 // Type or member is obsolete
}

[Fact]
public async Task Ignore_PublicProperty_ConflictWithPrivateDueAttributes()
{
Expand Down Expand Up @@ -782,6 +800,13 @@ public class ClassWithIgnoredPublicProperty
public string MyString { get; set; } = "DefaultValue";
}

public class ClassWithObsoleteAndIgnoredProperty
{
[Obsolete("Src gen should not generate reference to getter or setter")]
[JsonIgnore]
public string MyString_Obsolete { get; set; } = "DefaultValue";
}

public class ClassWithIgnoredPublicPropertyAndNewSlotPrivate : ClassWithIgnoredPublicProperty
{
internal new string MyString { get; set; } = "NewDefaultValue";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ public override async Task HonorJsonPropertyName()
[JsonSerializable(typeof(StructWithBadIgnoreAttribute))]
[JsonSerializable(typeof(Class_PropertyWith_InternalInitOnlySetter))]
[JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
[JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
[JsonSerializable(typeof(ClassWithObsoleteAndIgnoredProperty))]
[JsonSerializable(typeof(ClassWithPublicGetterAndPrivateSetter))]
[JsonSerializable(typeof(ClassWithInitializedProps))]
[JsonSerializable(typeof(ClassWithNewSlotInternalProperty))]
Expand Down Expand Up @@ -324,10 +326,12 @@ public override async Task JsonIgnoreCondition_WhenWritingNull_OnValueType_Fail_
[JsonSerializable(typeof(StructWithBadIgnoreAttribute))]
[JsonSerializable(typeof(Class_PropertyWith_InternalInitOnlySetter))]
[JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
[JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
[JsonSerializable(typeof(ClassWithObsoleteAndIgnoredProperty))]
[JsonSerializable(typeof(ClassWithPublicGetterAndPrivateSetter))]
[JsonSerializable(typeof(ClassWithInitializedProps))]
[JsonSerializable(typeof(ClassWithNewSlotInternalProperty))]
Expand Down