Skip to content

Internal class IntOrStringJsonConverter doesn't work with System.Text.Json Source Generator #1535

Closed as not planned

Description

@peter-glotfelty

Describe the bug

I know the C# k8s client has been working to get compatible with source trimming. I tried to get Json serialization working, but get this error at compile time:

The 'JsonConverterAttribute' type 'k8s.Models.IntOrStringJsonConverter' specified on member 'k8s.Models.IntstrIntOrString' is not a converter type or does not contain an accessible parameterless constructor.

Kubernetes C# SDK Client Version
13.0.11

Dotnet Runtime Version
net6

To Reproduce

This snippet is enough for me to hit the warning along with adding <IsTrimmable>true</IsTrimmable> to my library project

using System.Text.Json.Serialization;
using k8s.Models;

namespace MyApp;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(IntstrIntOrString))]
internal partial class MyAppJsonContext : JsonSerializerContext
{
}

Expected behavior

This shouldn't raise an error. InOrStringJsonConverter is internal so making it public would likely fix the issue, but I'll leave that up to you if that's the best fix or not.

Activity

  1. peter-glotfelty commented on Mar 4, 2024

    @peter-glotfelty
    Author

    A little more context here would be that for some code paths, we log the json dump of the object that we're applying to the cluster so this doesn't affect the Client itself per se but would be nice for some of the observability things we do.

    So more generally, then I guess the ask is that either the appropriate converters + models are public, or that the associated source generator metadata can be made public enough for us to use them outside of the K8s Client itself

  2. IvanJosipovic commented on Mar 4, 2024

    @IvanJosipovic
    Contributor

    I fixed this in, #1311 but it was closed. I would also like this PR to be completed, as I would like the performance benefits but don't plan on using full AOT.

  3. self-assigned this
    on Mar 5, 2024
  4. tg123 commented on Mar 5, 2024

    @tg123
    Member

    did you try Kubernetes.Aot?

  5. tg123 commented on Mar 24, 2024

    @tg123
    Member

    @peter-glotfelty could you please try example?
    https://github.com/kubernetes-client/csharp/tree/master/examples/aot
    dotnet publish -r win-x64

    @IvanJosipovic seesm too many warnings when introduce trimming to main sdk, not 100% if any bugs introduced by it

  6. peter-glotfelty commented on Mar 26, 2024

    @peter-glotfelty
    Author

    Hi @tg123, thank you for following up, I tried switching to the Aot lib and that doesn't make the error go away. Here's my Program.cs.

    using System.Text.Json.Serialization;
    using k8s.Models;
    
    // See https://aka.ms/new-console-template for more information
    Console.WriteLine("Hello, World!");
    
    [JsonSourceGenerationOptions(WriteIndented = true)]
    [JsonSerializable(typeof(V1Pod))]
    internal partial class SourceGenerationContext : JsonSerializerContext
    {
    }

    And the errors from dotnet publish -r win-x64

    Q:\src\K8sTest\Program.cs(9,24): warning SYSLIB1220: The 'JsonConverterAttribute' type 'k8s.Models.ResourceQuantityJsonConverter' specified on member 'k8s.Models.ResourceQuantity' is not a converter type or does not contain a
    n accessible parameterless constructor. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1220) [Q:\src\K8sTest\K8sTest.csproj]
    Q:\src\K8sTest\Program.cs(9,24): warning SYSLIB1030: Did not generate serialization metadata for type 'k8s.Models.ResourceQuantity'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1030) [Q:\src\K8sT 
    est\K8sTest.csproj]
    Q:\src\K8sTest\Program.cs(9,24): warning SYSLIB1220: The 'JsonConverterAttribute' type 'k8s.Models.IntOrStringJsonConverter' specified on member 'k8s.Models.IntstrIntOrString' is not a converter type or does not contain an ac 
    cessible parameterless constructor. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1220) [Q:\src\K8sTest\K8sTest.csproj]
    Q:\src\K8sTest\Program.cs(9,24): warning SYSLIB1030: Did not generate serialization metadata for type 'k8s.Models.IntstrIntOrString'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1030) [Q:\src\K8s 
    Test\K8sTest.csproj]
    Q:\src\K8sTest\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SourceGenerationContext.IDictionaryStringResourceQuantity.g.cs(54,84): error CS0103: The name 'ResourceQuantity' does not  
    exist in the current context [Q:\src\K8sTest\K8sTest.csproj]
    
  7. tg123 commented on Mar 28, 2024

    @tg123
    Member

    may i know why you have your own SourceGenerationContext in code?

    here is the one in aot, you do not have to create a new
    https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient.Aot/SourceGenerationContext.cs

  8. peter-glotfelty commented on Apr 9, 2024

    @peter-glotfelty
    Author

    The scenario I'm running into is that I'm including a V1Pod in another class I have for auditing/logging. Since I want to serialize my wrapper into json, I need to create a SourceGenerationContext for my own type. However, the source generation for that fails because it doesn't have access to the internal member IntOrStringJsonConverter. Here's a simplified example with a little more context.

    // Example Class
    public sealed class AdmissionControllerAuditLog
    {
         public bool Admitted { get; init; }
         public string CreationSource { get; init; }
         public V1Pod Target { get; init; }
    }
    
    [JsonSerializable(typeof(AdmissionControllerAuditLog))]
    internal partial class SourceGenerationContext : JsonSerializerContext
    {
    }
  9. tg123 commented on Apr 10, 2024

    @tg123
    Member

    ah i see, this is by design at the moment.
    but will support it later

  10. IvanJosipovic commented on Jun 19, 2024

    @IvanJosipovic
    Contributor

    I also need the converters to be public as I'm generating classes for CRDs. Due to this, the provided SourceGenerationContext is not sufficient.

  11. k8s-triage-robot commented on Sep 17, 2024

    @k8s-triage-robot

    The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

    This bot triages un-triaged issues according to the following rules:

    • After 90d of inactivity, lifecycle/stale is applied
    • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
    • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

    You can:

    • Mark this issue as fresh with /remove-lifecycle stale
    • Close this issue with /close
    • Offer to help out with Issue Triage

    Please send feedback to sig-contributor-experience at kubernetes/community.

    /lifecycle stale

  12. added
    lifecycle/stale
    on Sep 17, 2024
  13. k8s-triage-robot commented on Oct 17, 2024

    @k8s-triage-robot

    The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

    This bot triages un-triaged issues according to the following rules:

    • After 90d of inactivity, lifecycle/stale is applied
    • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
    • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

    You can:

    • Mark this issue as fresh with /remove-lifecycle rotten
    • Close this issue with /close
    • Offer to help out with Issue Triage

    Please send feedback to sig-contributor-experience at kubernetes/community.

    /lifecycle rotten

  14. added
    lifecycle/rotten
    and removed
    lifecycle/stale
    on Oct 17, 2024
  15. k8s-triage-robot commented on Nov 16, 2024

    @k8s-triage-robot

    The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

    This bot triages issues according to the following rules:

    • After 90d of inactivity, lifecycle/stale is applied
    • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
    • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

    You can:

    • Reopen this issue with /reopen
    • Mark this issue as fresh with /remove-lifecycle rotten
    • Offer to help out with Issue Triage

    Please send feedback to sig-contributor-experience at kubernetes/community.

    /close not-planned

  16. k8s-ci-robot commented on Nov 16, 2024

    @k8s-ci-robot
    Contributor

    @k8s-triage-robot: Closing this issue, marking it as "Not Planned".

    Details

    In response to this:

    The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

    This bot triages issues according to the following rules:

    • After 90d of inactivity, lifecycle/stale is applied
    • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
    • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

    You can:

    • Reopen this issue with /reopen
    • Mark this issue as fresh with /remove-lifecycle rotten
    • Offer to help out with Issue Triage

    Please send feedback to sig-contributor-experience at kubernetes/community.

    /close not-planned

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Metadata

Metadata

Assignees

Labels

lifecycle/rotten

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions