Skip to content

JSON source generator emits CS0612 warnings if serialized types use [Obsolete] #76829

Closed

Description

@ejball

Description

The JSON source generator should not trigger warnings when used with obsolete types. See #62076, which only fixed the bug when the Obsolete attribute includes a message.

Reproduction Steps

Same as #62076, but with [Obsolete] instead of [Obsolete("Use NewValue instead")].

Expected behavior

No warnings.

Actual behavior

warning CS0612: 'MyObject.OldValue' is obsolete

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Oct 10, 2022
  2. added this to the 8.0.0 milestone on Oct 11, 2022
  3. ghost removed
    untriaged
    on Oct 11, 2022
  4. layomia commented on Dec 2, 2022

    @layomia
    Contributor

    Up for grabs. Same fix as #63496, but we're disabling CS0612 warnings this time.

  5. added
    help wanted
    on Dec 2, 2022
  6. jeffhandley commented on Dec 13, 2022

    @jeffhandley
    Member

    If we do something for CS0612, then we would also need to be able to address obsoletions with custom diagnostic IDs, which can be SYSLIB values or user-defined diagnostics.

  7. jeffhandley commented on Dec 13, 2022

    @jeffhandley
    Member

    Even more generally... what if our generated code triggers a diagnostic from a completely different diagnostic, even one that is user-defined and intentionally running over generated code? How would customers suppress any arbitrary diagnostic within our generated code? How would that be configured?

  8. eiriktsarpalis commented on Dec 13, 2022

    @eiriktsarpalis
    Member

    This could be added to the existing JsonSourceGenerationOptionsAttribute via a new property:

    public class JsonSourceGenerationOptionsAttribute
    {
        public string[]? SuppressedWarnings { get; set; }
    }

    The source generator reads that attribute at compile time to configure source gen. Usage of the new property would look as follows:

    [JsonSourceGenerationOptions(SuppressedWarnings = new string[] { "CS0612" } )]
    [JsonSerializable(typeof(Foo))]
    public partial class MyContext : JsonSerializerContext { }
  9. eiriktsarpalis commented on Dec 13, 2022

    @eiriktsarpalis
    Member

    Alternatively we could employ a convention where suppressions active in the partial class are applied to the extra generated code:

    #pragma warning disable CS0618 // gets replicated in all generated instantiations of the partial class
    
    [JsonSerializable(typeof(Foo))]
    public partial class MyContext : JsonSerializerContext { }
    
    #pragma warning restore CS0618
  10. Hellevar commented on Jan 10, 2023

    @Hellevar

    This could be added to the existing JsonSourceGenerationOptionsAttribute via a new property:

    public class JsonSourceGenerationOptionsAttribute
    {
        public string[]? SuppressedWarnings { get; set; }
    }

    The source generator reads that attribute at compile time to configure source gen. Usage of the new property would look as follows:

    [JsonSourceGenerationOptions(SuppressedWarnings = new string[] { "CS0612" } )]
    [JsonSerializable(typeof(Foo))]
    public partial class MyContext : JsonSerializerContext { }

    Hi, @eiriktsarpalis !
    Is it a final shape of expected API, so it can be implemented in a such way, or should it go through api review process as per https://github.com/dotnet/runtime/blob/main/docs/project/api-review-process.md ?

  11. eiriktsarpalis commented on Jan 10, 2023

    @eiriktsarpalis
    Member

    @Hellevar this needs to go through API review before an implementation PR can be submitted.

  12. added a commit that references this issue on Jan 26, 2023
    d287f93
  13. ghost added
    in-pr
    on Jan 26, 2023
  14. added a commit that references this issue on Jan 27, 2023
    9e7d3c0
  15. ghost removed
    in-pr
    on Jan 27, 2023
  16. ghost locked as resolved and limited conversation to collaborators on Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions