Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit fb09285

Browse files
committed
fix: method to format properties to camel case when property end with . or is null
1 parent 634f48e commit fb09285

2 files changed

Lines changed: 109 additions & 6 deletions

File tree

src/ProblemDetailsFactory.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
7474

7575
private (string Property, string Error) _mappingModelStateError(KeyValuePair<string, ModelStateEntry> modelStateError)
7676
{
77-
var error = modelStateError
78-
.Value
79-
.Errors
80-
.Select(s => s.ErrorMessage)
81-
.First();
82-
8377
if(modelStateError.Key.StartsWith("$."))
8478
{
8579
return (
@@ -88,6 +82,12 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
8882
);
8983
}
9084

85+
var error = modelStateError
86+
.Value
87+
.Errors
88+
.Select(s => s.ErrorMessage)
89+
.First();
90+
9191
return _checkRequestBody(modelStateError.Key, error);
9292
}
9393

@@ -117,6 +117,11 @@ private string _formatPropertyName(string propertyName)
117117

118118
private static string _formatPropertyToCamelCase(string propertyName)
119119
{
120+
if(string.IsNullOrWhiteSpace(propertyName))
121+
{
122+
return "";
123+
}
124+
120125
var propertyParts = propertyName.Split('.');
121126
if(propertyParts.Length == 1)
122127
{
@@ -126,6 +131,12 @@ private static string _formatPropertyToCamelCase(string propertyName)
126131

127132
for(var count = 0; count < propertyParts.Length; count++)
128133
{
134+
// Prevent System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' for cases "Hello."
135+
if(propertyParts[count].Length == 0)
136+
{
137+
continue;
138+
}
139+
129140
propertyParts[count] = char.ToLowerInvariant(propertyParts[count][0]) + propertyParts[count][1..];
130141
}
131142

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/ProblemDetailsFactoryTests.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,96 @@ public void PropertyNamingPolicy_SnakeCase_FormattedCamelCase()
110110
// Assert
111111
act.Should().Be("prop_name.prop_value");
112112
}
113+
114+
[Fact(DisplayName = "Format to camel case null value - Should return empty")]
115+
public void FormatPropertyToCamelCase_Null_Empty()
116+
{
117+
// Arrange
118+
string propertyName = null;
119+
120+
121+
// Act
122+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
123+
124+
125+
// Assert
126+
act.Should().BeEmpty();
127+
}
128+
129+
[Fact(DisplayName = "Format to camel case empty value - Should return empty")]
130+
public void FormatPropertyToCamelCase_Empty_Empty()
131+
{
132+
// Arrange
133+
var propertyName = "";
134+
135+
136+
// Act
137+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
138+
139+
140+
// Assert
141+
act.Should().BeEmpty();
142+
}
143+
144+
[Fact(DisplayName = "Format to camel case white spaces value - Should return empty")]
145+
public void FormatPropertyToCamelCase_WhiteSpaces_Empty()
146+
{
147+
// Arrange
148+
var propertyName = " ";
149+
150+
151+
// Act
152+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
153+
154+
155+
// Assert
156+
act.Should().BeEmpty();
157+
}
158+
159+
[Fact(DisplayName = "Format to camel case white one character - Should return the character lowercase")]
160+
public void FormatPropertyToCamelCase_OneCharacter_Lower()
161+
{
162+
// Arrange
163+
var propertyName = "G";
164+
165+
166+
// Act
167+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
168+
169+
170+
// Assert
171+
act.Should().Be("g");
172+
}
173+
174+
[Fact(DisplayName = "Format to camel case a text with only one dot - Should return a dot")]
175+
public void FormatPropertyToCamelCase_OnlyOneDot_Dot()
176+
{
177+
// Arrange
178+
var propertyName = ".";
179+
180+
181+
// Act
182+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
183+
184+
185+
// Assert
186+
act.Should().Be(".");
187+
}
188+
189+
190+
191+
[Fact(DisplayName = "Format to camel case a text ended with an dot - Should format")]
192+
public void FormatPropertyToCamelCase_EndWitDot_Format()
193+
{
194+
// Arrange
195+
var propertyName = "Hello.";
196+
197+
198+
// Act
199+
var act = ObjectInvoker.Invoke<string>(typeof(ProblemDetailsFactory), "_formatPropertyToCamelCase", propertyName);
200+
201+
202+
// Assert
203+
act.Should().Be("hello.");
204+
}
113205
}

0 commit comments

Comments
 (0)