-
Notifications
You must be signed in to change notification settings - Fork 305
Open
Description
Summary
The client currently uses reflection-based serialization in non-AOT library even though a SourceGenerationContext is already generated.
This adds unnecessary reflection-based overhead.
Additionally, SourceGenerationContext is private and prevents us chaining it with our own custom IJsonTypeInfoResolver.
Current Behavior
- Non-AOT library use reflection-based serialization.
- The generated
SourceGenerationContextis not used by default, even though it's in the library. - Callers cannot compose their own resolvers since the generated context is not exposed.
Expected Behavior
- Non-AOT builds should use the generated
SourceGenerationContextby default and fall back to reflection-based if type is missing. SourceGenerationContextshould be public so that we canKubernetesJson.AddJsonOptionsand append our ownIJsonTypeInfoResolverfor our CRDs.
Rationale
- Source generation improves performance and reduces allocations even outside AOT scenarios.
- Keeps behaviour predictable and consistent between AOT and non-AOT.
- Also solves CreateNamespacedCustomObjectAsync unusable with AOT #1670
Proposed Changes
- Initialize the client’s default
JsonSerializerOptionswith the generated context:public static class KubernetesJson { private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions { #if !K8S_AOT TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()) // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection #else TypeInfoResolver = SourceGenerationContext.Default // Uses Source Generated IJsonTypeInfoResolver #endif }; }
- Make
SourceGenerationContextpublic
This allows us to useKubernetesJson.AddJsonOptionsand chain our own custom Source Generated IJsonTypeInfoResolverKubernetesJson.AddJsonOptions(x => { x.TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, MyCRDSourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()); });
- Remove
src/KubernetesClient.Aot/KubernetesJson.csas it is now redundant.
Happy to make this PR.
Thanks!
Metadata
Metadata
Assignees
Labels
No labels