kubernetes-client / csharp

Officially supported dotnet Kubernetes Client library
Apache License 2.0
1.08k stars 292 forks source link

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

Open peter-glotfelty opened 5 months ago

peter-glotfelty commented 5 months ago

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 commented 5 months ago

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

IvanJosipovic commented 5 months ago

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.

tg123 commented 5 months ago

did you try Kubernetes.Aot?

tg123 commented 5 months ago

@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

peter-glotfelty commented 5 months ago

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]
tg123 commented 5 months ago

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

peter-glotfelty commented 4 months ago

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
{
}
tg123 commented 4 months ago

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

IvanJosipovic commented 2 months ago

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