Skip to content

Commit 9456c74

Browse files
author
Christoph Bühler
committed
fix(naming convention): use correct names for reserved words
1 parent b4f6282 commit 9456c74

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/KubeOps/Operator/KubernetesOperator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ private void ConfigureRequiredServices() =>
7575
services.AddTransient(
7676
_ => new JsonSerializerSettings
7777
{
78-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
78+
ContractResolver = new NamingConvention(),
7979
Converters = new List<JsonConverter>
8080
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}},
8181
});
8282
services.AddTransient(
8383
_ => new SerializerBuilder()
8484
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
85-
.WithNamingConvention(CamelCaseNamingConvention.Instance)
85+
.WithNamingConvention(new NamingConvention())
8686
.Build());
8787

8888
services.AddTransient<EntitySerializer>();
@@ -97,13 +97,13 @@ private void ConfigureRequiredServices() =>
9797
{
9898
SerializationSettings =
9999
{
100-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
100+
ContractResolver = new NamingConvention(),
101101
Converters = new List<JsonConverter>
102102
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}}
103103
},
104104
DeserializationSettings =
105105
{
106-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
106+
ContractResolver = new NamingConvention(),
107107
Converters = new List<JsonConverter>
108108
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}}
109109
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Serialization;
7+
using YamlDotNet.Serialization;
8+
using YamlDotNet.Serialization.NamingConventions;
9+
10+
namespace KubeOps.Operator.Serialization
11+
{
12+
internal class NamingConvention : CamelCasePropertyNamesContractResolver, INamingConvention
13+
{
14+
private readonly INamingConvention _yamlNaming = CamelCaseNamingConvention.Instance;
15+
16+
private readonly IDictionary<string, string> _rename = new Dictionary<string, string>
17+
{
18+
{"namespaceProperty", "namespace"},
19+
{"enumProperty", "enum"},
20+
{"objectProperty", "object"},
21+
};
22+
23+
public string Apply(string value)
24+
{
25+
var (key, renamedValue) = _rename.FirstOrDefault(p =>
26+
string.Equals(value, p.Key, StringComparison.InvariantCultureIgnoreCase));
27+
28+
if (key != default)
29+
{
30+
value = renamedValue;
31+
}
32+
33+
return _yamlNaming.Apply(value);
34+
}
35+
36+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
37+
{
38+
var property = base.CreateProperty(member, memberSerialization);
39+
40+
var (key, renamedValue) = _rename.FirstOrDefault(p =>
41+
string.Equals(property.PropertyName, p.Key, StringComparison.InvariantCultureIgnoreCase));
42+
43+
if (key != default)
44+
{
45+
property.PropertyName = renamedValue;
46+
}
47+
48+
return property;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)