Skip to content

System.Text.Json code gen: ignored properties are not completely ignored #58320

Description

@Jericho

One of my classes has a property decorated with [JsonIgnore] similar to this:

[JsonIgnore]
[JsonPropertyName("foo")]
[Obsolete("This property is no longer valid because blablabla.")]
public string Foo { get; set; }

One important detail to notice is that this property is ALSO decorated with an "obsolete" attribute with a custom message that allows me to warm developers to discontinue using this property.

The Json code generator generates the following:

properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.String>(
    options,
    isProperty: true,
    isPublic: true,
    isVirtual: false,
    declaringType: typeof(global::MyLibrary.Models.ClassA),
    propertyTypeInfo: jsonContext.String,
    converter: null,
    getter: static (obj) => ((global::MyLibrary.Models.ClassA)obj).Foo,
    setter: static (obj, value) => ((global::MyLibrary.Models.ClassA)obj).Foo = value!,
    ignoreCondition: global::System.Text.Json.Serialization.JsonIgnoreCondition.Always,
    hasJsonInclude: false,
    numberHandling: default,
    propertyName: "Foo",
    jsonPropertyName: "foo");

ignoreCondition is set to ignore always, which I guess is good and will presumably prevent the value from being serialized but the problem is that the Foo property is referenced in the "getter" and the "setter" which triggers a CS0618 when I build my library coupled with the fact that I treat warnings as errors during the build process, this causes my library to fail to build.

Is there a way to indicate to the code generator that a given class property should be completely ignored?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions