csharp icon indicating copy to clipboard operation
csharp copied to clipboard

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

Open peter-glotfelty opened this issue 1 year ago • 11 comments

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.

peter-glotfelty avatar Mar 04 '24 19:03 peter-glotfelty

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

peter-glotfelty avatar Mar 04 '24 19:03 peter-glotfelty

I fixed this in, https://github.com/kubernetes-client/csharp/pull/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.

IvanJosipovic avatar Mar 04 '24 20:03 IvanJosipovic

did you try Kubernetes.Aot?

tg123 avatar Mar 05 '24 06:03 tg123

@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

tg123 avatar Mar 24 '24 19:03 tg123

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]

peter-glotfelty avatar Mar 26 '24 00:03 peter-glotfelty

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

tg123 avatar Mar 28 '24 00:03 tg123

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
{
}

peter-glotfelty avatar Apr 09 '24 22:04 peter-glotfelty

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

tg123 avatar Apr 10 '24 15:04 tg123