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

Commit dea43b2

Browse files
committed
enhancements: Create internal extension to properties naming
1 parent fb09285 commit dea43b2

7 files changed

Lines changed: 264 additions & 143 deletions

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44

55

6+
## [1.0.1] - 2022-05-28
7+
[Full Changelog](https://github.com/TechNobre/PowerUtils.Text/compare/v1.0.0...v1.0.1)
8+
9+
10+
### Fixes
11+
12+
- Fix `System.IndexOutOfRangeException` when format the properties name to camel case;
13+
14+
15+
16+
617
## [1.0.0] - 2022-03-15
718

819
- Kickoff;

src/PowerUtils.AspNetCore.ErrorHandler.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageId>PowerUtils.AspNetCore.ErrorHandler</PackageId>
1616
<title>PowerUtils.AspNetCore.ErrorHandler</title>
1717
<Product>PowerUtils.AspNetCore.ErrorHandler</Product>
18-
<Version>1.0.0</Version>
18+
<Version>1.0.1</Version>
1919

2020
<Authors>Nelson Nobre</Authors>
2121
<Company>TechNobre</Company>

src/ProblemDetailsFactory.cs

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.AspNetCore.WebUtilities;
88
using Microsoft.Extensions.Options;
99
using PowerUtils.Net.Constants;
10-
using PowerUtils.Text;
1110

1211
namespace PowerUtils.AspNetCore.ErrorHandler
1312
{
@@ -22,16 +21,14 @@ internal static ProblemDetailsResponse Create(HttpContext httpContext)
2221
{
2322
var result = new ProblemDetailsResponse();
2423

25-
result.Status = httpContext.GetStatusCode() ?? 0;
24+
result.Status = httpContext.GetStatusCode() ?? 0; // Default value is 0
2625
result.Type = result.Status.GetStatusCodeLinkOrDefault();
2726
result.Title = result.Status == 0 ? null : ReasonPhrases.GetReasonPhrase(result.Status);
2827

2928
result.Instance = httpContext.GetRequestEndpoint();
3029

3130
result.TraceID = httpContext.GetCorrelationId();
3231

33-
result.Errors = new Dictionary<string, string>();
34-
3532
return result;
3633
}
3734

@@ -111,50 +108,9 @@ private string _formatPropertyName(string propertyName)
111108
=> _options.Value.PropertyNamingPolicy switch
112109
{
113110
PropertyNamingPolicy.Original => propertyName,
114-
PropertyNamingPolicy.SnakeCase => _formatPropertyToSnakeCase(propertyName),
115-
_ => _formatPropertyToCamelCase(propertyName),
111+
PropertyNamingPolicy.SnakeCase => propertyName.FormatToSnakeCase(),
112+
_ => propertyName.FormatToCamelCase(),
116113
};
117114

118-
private static string _formatPropertyToCamelCase(string propertyName)
119-
{
120-
if(string.IsNullOrWhiteSpace(propertyName))
121-
{
122-
return "";
123-
}
124-
125-
var propertyParts = propertyName.Split('.');
126-
if(propertyParts.Length == 1)
127-
{
128-
return char.ToLowerInvariant(propertyName[0]) + propertyName[1..];
129-
}
130-
131-
132-
for(var count = 0; count < propertyParts.Length; count++)
133-
{
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-
140-
propertyParts[count] = char.ToLowerInvariant(propertyParts[count][0]) + propertyParts[count][1..];
141-
}
142-
143-
144-
return string.Join(".", propertyParts);
145-
}
146-
147-
private static string _formatPropertyToSnakeCase(string propertyName)
148-
{
149-
var propertyParts = propertyName.Split('.');
150-
151-
for(var count = 0; count < propertyParts.Length; count++)
152-
{
153-
propertyParts[count] = propertyParts[count].ToSnakeCase();
154-
}
155-
156-
157-
return string.Join(".", propertyParts);
158-
}
159115
}
160116
}

src/ProblemDetailsResponse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace PowerUtils.AspNetCore.ErrorHandler
55
{
66
public class ProblemDetailsResponse
77
{
8-
#region PROPERTIES
98
/// <summary>
109
/// The HTTP status code([RFC7231], Section 6) generated
1110
/// </summary>
@@ -38,16 +37,17 @@ public class ProblemDetailsResponse
3837
/// { "Property": "Error" }
3938
/// </example>
4039
public IDictionary<string, string> Errors { get; set; }
41-
#endregion
4240

4341
/// <summary>
4442
/// Serialize problem details to JSON
4543
/// </summary>
46-
/// <returns></returns>
4744
public override string ToString()
4845
=> JsonSerializer.Serialize(this);
4946

5047
public static implicit operator string(ProblemDetailsResponse problemDetailsResponse)
5148
=> problemDetailsResponse.ToString();
49+
50+
public ProblemDetailsResponse()
51+
=> Errors = new Dictionary<string, string>();
5252
}
5353
}

src/PropertiesNamingExtensions.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using PowerUtils.Text;
2+
3+
namespace PowerUtils.AspNetCore.ErrorHandler
4+
{
5+
internal static class PropertiesNamingExtensions
6+
{
7+
public static string FormatToCamelCase(this string propertyName)
8+
{
9+
if(string.IsNullOrWhiteSpace(propertyName))
10+
{
11+
return "";
12+
}
13+
14+
var propertyParts = propertyName.Split('.');
15+
for(var count = 0; count < propertyParts.Length; count++)
16+
{
17+
// Prevent System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' for cases "Hello."
18+
if(propertyParts[count].Length == 0)
19+
{
20+
continue;
21+
}
22+
23+
propertyParts[count] = char.ToLowerInvariant(propertyParts[count][0]) + propertyParts[count][1..];
24+
}
25+
26+
27+
return string.Join(".", propertyParts);
28+
}
29+
30+
public static string FormatToSnakeCase(this string propertyName)
31+
{
32+
if(string.IsNullOrWhiteSpace(propertyName))
33+
{
34+
return "";
35+
}
36+
37+
var propertyParts = propertyName.Split('.');
38+
39+
for(var count = 0; count < propertyParts.Length; count++)
40+
{
41+
propertyParts[count] = propertyParts[count].ToSnakeCase();
42+
}
43+
44+
45+
return string.Join(".", propertyParts);
46+
}
47+
}
48+
}

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

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -110,96 +110,4 @@ 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-
}
205113
}

0 commit comments

Comments
 (0)