Registry Mirror
Skip to content

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

Closed

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

  1. ghost added
    untriaged
    on Aug 28, 2021
  2. added and removed
    untriaged
    on Aug 30, 2021
  3. added this to the 6.0.0 milestone on Aug 30, 2021
  4. steveharter commented on Aug 30, 2021

    @steveharter
    Contributor

    This should be fixed for 6.0 if possible

  5. self-assigned this
    on Aug 30, 2021
  6. added
    in-pr
    on Aug 30, 2021
  7. ghost removed
    in-pr
    on Sep 3, 2021
  8. ghost locked as resolved and limited conversation to collaborators on Oct 7, 2021
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