Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
peter-glotfelty opened this issue Mar 4, 2024 · 8 comments
Assignees

Comments

@peter-glotfelty
Copy link

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
Copy link
Author

peter-glotfelty commented Mar 4, 2024

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
Copy link
Contributor

IvanJosipovic commented Mar 4, 2024

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.

@tg123 tg123 self-assigned this Mar 5, 2024
@tg123
Copy link
Member

tg123 commented Mar 5, 2024

did you try Kubernetes.Aot?

@tg123
Copy link
Member

tg123 commented Mar 24, 2024

@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
Copy link
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]

@tg123
Copy link
Member

tg123 commented Mar 28, 2024

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

@tg123
Copy link
Member

tg123 commented Apr 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants