From 0b67950590ab99a5b06cbcbc69d61d506ecf6abb Mon Sep 17 00:00:00 2001
From: stan-sz <37585349+stan-sz@users.noreply.github.com>
Date: Fri, 14 Oct 2022 11:44:47 +0200
Subject: [PATCH] Provide strict k8s yaml model deserializer (#1047)
* Provide strict k8s yaml model deserializer
* Provide documentation
* Add UT coverage
---
src/KubernetesClient.Models/KubernetesYaml.cs | 47 +++++++++++--------
.../KubernetesClientConfigurationTests.cs | 1 +
.../KubernetesYamlTests.cs | 4 ++
3 files changed, 33 insertions(+), 19 deletions(-)
diff --git a/src/KubernetesClient.Models/KubernetesYaml.cs b/src/KubernetesClient.Models/KubernetesYaml.cs
index dd79e9d7..c677bcbf 100644
--- a/src/KubernetesClient.Models/KubernetesYaml.cs
+++ b/src/KubernetesClient.Models/KubernetesYaml.cs
@@ -15,16 +15,22 @@ namespace k8s
///
public static class KubernetesYaml
{
- private static readonly IDeserializer Deserializer =
+ private static readonly DeserializerBuilder CommonDeserializerBuilder =
new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.WithTypeConverter(new ResourceQuantityYamlConverter())
.WithAttemptingUnquotedStringTypeDeserialization()
- .WithOverridesFromJsonPropertyAttributes()
- .IgnoreUnmatchedProperties()
- .Build();
+ .WithOverridesFromJsonPropertyAttributes();
+ private static readonly IDeserializer StrictDeserializer =
+ CommonDeserializerBuilder
+ .Build();
+ private static readonly IDeserializer Deserializer =
+ CommonDeserializerBuilder
+ .IgnoreUnmatchedProperties()
+ .Build();
+ private static IDeserializer GetDeserializer(bool strict) => strict ? StrictDeserializer : Deserializer;
private static readonly IValueSerializer Serializer =
new SerializerBuilder()
@@ -100,8 +106,9 @@ public void WriteYaml(IEmitter emitter, object value, Type type)
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
/// be used.
///
+ /// true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise
/// collection of objects
- public static async Task> LoadAllFromStreamAsync(Stream stream, IDictionary typeMap = null)
+ public static async Task> LoadAllFromStreamAsync(Stream stream, IDictionary typeMap = null, bool strict = false)
{
var reader = new StreamReader(stream);
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
@@ -117,8 +124,9 @@ public static async Task> LoadAllFromStreamAsync(Stream stream, IDi
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
/// be used.
///
+ /// true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise
/// collection of objects
- public static async Task> LoadAllFromFileAsync(string fileName, IDictionary typeMap = null)
+ public static async Task> LoadAllFromFileAsync(string fileName, IDictionary typeMap = null, bool strict = false)
{
using (var fileStream = File.OpenRead(fileName))
{
@@ -136,8 +144,9 @@ public static async Task> LoadAllFromFileAsync(string fileName, IDi
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
/// be used.
///
+ /// true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise
/// collection of objects
- public static List