Skip to content

Commit 44cfb7e

Browse files
rgramaRazvan Gramabuehler
authored
fix: Add support for System.Object in CRDs (#857)
Co-authored-by: Razvan Grama <[email protected]> Co-authored-by: Christoph Bühler <[email protected]>
1 parent 59e19d6 commit 44cfb7e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/KubeOps.Transpiler/Crds.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
300300
return new V1JSONSchemaProps { Type = String };
301301
}
302302

303+
if (type.FullName == "System.Object")
304+
{
305+
return new V1JSONSchemaProps { Type = Object, XKubernetesPreserveUnknownFields = true };
306+
}
307+
303308
if (type.Name == typeof(Nullable<>).Name && type.GenericTypeArguments.Length == 1)
304309
{
305310
var props = context.Map(type.GenericTypeArguments[0]);
@@ -345,7 +350,29 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
345350
return context.MapObjectType(type);
346351
}
347352

348-
return type.BaseType?.FullName switch
353+
static Type GetRootBaseType(Type type)
354+
{
355+
var current = type;
356+
while (current.BaseType != null)
357+
{
358+
var baseName = current.BaseType.FullName;
359+
360+
if (baseName == "System.Object" ||
361+
baseName == "System.ValueType" ||
362+
baseName == "System.Enum")
363+
{
364+
return current.BaseType; // This is the root base we're after
365+
}
366+
367+
current = current.BaseType;
368+
}
369+
370+
return current; // In case it's already System.Object
371+
}
372+
373+
var rootBase = GetRootBaseType(type);
374+
375+
return rootBase.FullName switch
349376
{
350377
"System.Object" => context.MapObjectType(type),
351378
"System.ValueType" => context.MapValueType(type),

test/KubeOps.Transpiler.Test/Crds.Mlc.Test.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ public void Should_Set_Preserve_Unknown_Fields_On_Classes()
350350
specProperties.XKubernetesPreserveUnknownFields.Should().BeTrue();
351351
}
352352

353+
[Fact]
354+
public void Should_Set_Preserve_Unknown_Fields_On_System_Object()
355+
{
356+
var crd = _mlc.Transpile(typeof(EntityWithSystemObject));
357+
358+
var specProperties = crd.Spec.Versions.First().Schema.OpenAPIV3Schema.Properties["spec"].Properties["obj"];
359+
specProperties.XKubernetesPreserveUnknownFields.Should().BeTrue();
360+
}
361+
353362
[Fact]
354363
public void Should_Set_Preserve_Unknown_Fields_On_ObjectLists()
355364
{
@@ -713,6 +722,15 @@ private class UnknownFieldsEntity : CustomKubernetesEntity<UnknownFieldsEntity.E
713722
public class EntitySpec;
714723
}
715724

725+
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
726+
private class EntityWithSystemObject : CustomKubernetesEntity<EntityWithSystemObject.EntitySpec>
727+
{
728+
public class EntitySpec
729+
{
730+
public object Obj { get; set; } = null!;
731+
}
732+
}
733+
716734
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
717735
private class UnknownFieldsListEntity : CustomKubernetesEntity<UnknownFieldsListEntity.EntitySpec>
718736
{

0 commit comments

Comments
 (0)