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

Fix: double underscore in case uppercase character is preceeded by an underscore #190

Merged
merged 8 commits into from
Aug 22, 2022
18 changes: 7 additions & 11 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,14 @@ def dotnet(Closure body){

def deploy(Map args = [:]) {
def secret = args.secret
def repo = getVaultSecret(secret: secret)
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [
[var: 'REPO_API_KEY', password: repo.data.apiKey],
[var: 'REPO_API_URL', password: repo.data.url],
]]) {
if (isUnix()) {
dotnet(){
def repo = getVaultSecret(secret: secret)
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [
[var: 'REPO_API_KEY', password: repo.data.apiKey],
[var: 'REPO_API_URL', password: repo.data.url],
]]) {
withEnv(["REPO_API_KEY=${repo.data.apiKey}", "REPO_API_URL=${repo.data.url}"]) {
sh(label: 'Deploy', script: '.ci/linux/deploy.sh ${REPO_API_KEY} ${REPO_API_URL}')
}
} else {
withEnv(["REPO_API_KEY=${repo.data.apiKey}", "REPO_API_URL=${repo.data.url}"]) {
bat(label: 'Deploy', script: '.ci/deploy.bat ${REPO_API_KEY} ${REPO_API_URL}')
sh(label: 'Deploy', script: '.ci/deploy.sh ${REPO_API_KEY} ${REPO_API_URL}')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static string ToSnakeCase(string s)
sb.Append(char.ToLowerInvariant(c));
else if (char.IsUpper(s[i - 1])) // WriteIO => write_io
sb.Append(char.ToLowerInvariant(c));
else if (s[i - 1] == '_') // User_Id => user_id
sb.Append(char.ToLowerInvariant(c));
else
{
sb.Append("_");
Expand Down
26 changes: 26 additions & 0 deletions tests/Elastic.CommonSchema.Tests/Serializes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Elastic.CommonSchema.Serialization;
using FluentAssertions;
Expand Down Expand Up @@ -30,6 +31,31 @@ public void SerializesSomethingToString()
deserialized.Log.Level.Should().Be("debug");
}

[Fact]
public void SerializesMetadataPropertiesToSnakeCase()
{
var b = new Base
{
Metadata = new Dictionary<string, object>
{
["MessageTemplate"] = "some-template",
["WriteIO"] = "some-io",
["User_Id"] = 1,
["eventId"] = "some-id",
["rule"] = "some-rule",
}
};

var serialized = b.Serialize();
var deserialized = Base.Deserialize(serialized);

deserialized.Metadata.Should().ContainKey("message_template");
deserialized.Metadata.Should().ContainKey("write_io");
deserialized.Metadata.Should().ContainKey("user_id");
deserialized.Metadata.Should().ContainKey("event_id");
deserialized.Metadata.Should().ContainKey("rule");
}

public class SubclassedBase : Base
{
[DataMember(Name = "agent2")]
Expand Down