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

Commit da875bd

Browse files
authored
Merge pull request #21 from TechNobre/develop
Fix `System.IndexOutOfRangeException` when format the properties name to camel case;
2 parents e6170d3 + 6053f9f commit da875bd

File tree

12 files changed

+353
-110
lines changed

12 files changed

+353
-110
lines changed
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
name: 'Publish Nuget'
1+
name: 'Publish nuget'
2+
3+
24

35
on:
46
push:
57
branches:
6-
- main
8+
- deploy-nuget
9+
10+
11+
12+
env:
13+
SDK_VERSION: '6.0.300'
14+
PACKAGE_PATH: ./src/**/*.nupkg
15+
NUGET_SERVER: https://api.nuget.org/v3/index.json
16+
717

818

919
jobs:
1020

1121
deploy-nuget:
12-
name: "Deploy NuGet"
22+
name: "Deploy nuget"
1323
runs-on: ubuntu-latest
1424

1525
steps:
16-
- uses: actions/checkout@main
26+
- name: "Checkout"
27+
uses: actions/checkout@v3
1728

18-
- name: Setup .NET
29+
- name: "Setup .NET"
1930
uses: actions/setup-dotnet@v2
2031
with:
21-
dotnet-version: '6.0.201'
32+
dotnet-version: ${{ env.SDK_VERSION }}
2233

23-
- name: Restore dependencies
34+
- name: "Restore dependencies"
2435
run: dotnet restore
2536

26-
- name: Build
27-
run: dotnet build --configuration Release
37+
- name: "Build"
38+
run: dotnet build --configuration Release --no-restore
39+
40+
- name: "Test"
41+
run: dotnet test --configuration Release --no-build
2842

29-
- name: Create the Package
30-
run: dotnet pack --configuration Release
43+
- name: "Pack"
44+
run: dotnet pack --configuration Release --no-build
3145

32-
- name: Publish
33-
run: dotnet nuget push "./src/bin/PowerUtils.AspNetCore.ErrorHandler.*.nupkg" -k ${{ secrets.NUGET_TOKEN }} -s https://api.nuget.org/v3/index.json
46+
- name: "Publish nuget"
47+
run: dotnet nuget push ${{ env.PACKAGE_PATH }} --api-key ${{ secrets.NUGET_TOKEN }} --source ${{ env.NUGET_SERVER }} --skip-duplicate

.github/workflows/sonarcloud.yml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: 'SonarCloud'
22

3+
4+
35
on:
46
push:
57
branches:
@@ -10,57 +12,64 @@ on:
1012
- main
1113

1214

15+
16+
env:
17+
SDK_VERSION: '6.0.300'
18+
19+
20+
1321
jobs:
1422

1523
sonar-scanner:
1624
name: "Sonar scanner"
1725
runs-on: windows-latest
18-
steps:
1926

20-
- name: Setup .NET
27+
steps:
28+
- name: "Setup .NET"
2129
uses: actions/setup-dotnet@v2
2230
with:
23-
dotnet-version: '6.0.201'
31+
dotnet-version: ${{ env.SDK_VERSION }}
2432

25-
- name: Set up JDK 11
26-
uses: actions/setup-java@v3.0.0
33+
- name: "Set up JDK 11"
34+
uses: actions/setup-java@v3.3.0
2735
with:
2836
distribution: 'adopt'
2937
java-version: '11'
3038

31-
- uses: actions/checkout@v2
39+
- name: "Checkout"
40+
uses: actions/checkout@v3
3241
with:
3342
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
3443

35-
- name: Cache SonarCloud packages
36-
uses: actions/cache@v2.1.7
44+
- name: "Cache SonarCloud packages"
45+
uses: actions/cache@v3
3746
with:
3847
path: ~\sonar\cache
3948
key: ${{ runner.os }}-sonar
4049
restore-keys: ${{ runner.os }}-sonar
4150

42-
- name: Cache SonarCloud scanner
51+
- name: "Cache SonarCloud scanner"
4352
id: cache-sonar-scanner
44-
uses: actions/cache@v2.1.7
53+
uses: actions/cache@v3
4554
with:
4655
path: .\.sonar\scanner
4756
key: ${{ runner.os }}-sonar-scanner
4857
restore-keys: ${{ runner.os }}-sonar-scanner
4958

50-
- name: Install SonarCloud scanner
59+
- name: "Install SonarCloud scanner"
5160
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
5261
shell: pwsh
5362
run: |
5463
New-Item -Path .\.sonar\scanner -ItemType Directory
5564
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
5665
57-
- name: Build and analyze
66+
- name: "Build and analyze"
5867
env:
5968
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
6069
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
6170
shell: pwsh
6271
run: |
6372
.\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ secrets.SONAR_PROJECT_KEY }}" /o:"${{ secrets.SONAR_ORGANIZATION }}" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
64-
dotnet build --configuration Release
65-
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
66-
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
73+
dotnet build
74+
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --no-build
75+
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

.github/workflows/test-project.yml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
name: 'Tests'
22

3+
4+
35
on:
4-
push:
5-
branches:
6-
- develop
76
pull_request:
87
types: [opened, reopened, synchronize]
98
branches:
109
- main
1110

1211

12+
13+
env:
14+
SDK_VERSION: '6.0.300'
15+
16+
17+
1318
jobs:
1419

1520
test-project:
16-
name: "Testing project"
21+
name: "Test nuget"
1722
runs-on: ubuntu-latest
1823

1924
steps:
20-
- uses: actions/checkout@v2
25+
- name: "Checkout"
26+
uses: actions/checkout@v3
2127

22-
- name: Setup .NET
28+
- name: "Setup .NET"
2329
uses: actions/setup-dotnet@v2
2430
with:
25-
dotnet-version: '6.0.201'
31+
dotnet-version: ${{ env.SDK_VERSION }}
2632

27-
- name: Restore dependencies
33+
- name: "Restore dependencies"
2834
run: dotnet restore
2935

30-
- name: Build
31-
run: dotnet build --no-restore
36+
- name: "Build"
37+
run: dotnet build --configuration Release --no-restore
3238

33-
- name: Test
34-
run: dotnet test --no-build
39+
- name: "Test"
40+
run: dotnet test --configuration Release --no-build

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.AspNetCore.ErrorHandler/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;

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "6.0.201"
3+
"version": "6.0.300"
44
}
55
}

samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Samples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
<PackageReference Include="PowerUtils.AspNetCore.Authentication.BasicAuth" Version="1.0.0" />
1515
<PackageReference Include="PowerUtils.AspNetCore.Authentication.JwtBearer" Version="1.0.0" />
1616
<PackageReference Include="PowerUtils.Security" Version="1.1.0" />
17-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
17+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
1818
<!-- LOGS -->
1919
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
2020
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
2121
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
2222
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
23-
<PackageReference Include="Serilog.Exceptions" Version="8.1.0" />
23+
<PackageReference Include="Serilog.Exceptions" Version="8.2.0+build.634" />
2424
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
2525
</ItemGroup>
2626

src/PowerUtils.AspNetCore.ErrorHandler.csproj

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<PropertyGroup>
44
<ProjectGuid>848b072c-e1a1-4fcf-b00a-f14750be758b</ProjectGuid>
55

6+
<CurrentYear>$([System.DateTime]::UtcNow.ToString(yyyy))</CurrentYear>
7+
8+
69
<!-- Assembly and projecto details -->
710
<!-- https://docs.microsoft.com/en-us/dotnet/standard/frameworks -->
811
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1</TargetFrameworks>
@@ -15,19 +18,17 @@
1518
<PackageId>PowerUtils.AspNetCore.ErrorHandler</PackageId>
1619
<title>PowerUtils.AspNetCore.ErrorHandler</title>
1720
<Product>PowerUtils.AspNetCore.ErrorHandler</Product>
18-
<Version>1.0.0</Version>
21+
<Version>1.0.1</Version>
1922

2023
<Authors>Nelson Nobre</Authors>
2124
<Company>TechNobre</Company>
2225

2326
<License>MIT</License>
2427
<PackageLicenseFile>LICENSE</PackageLicenseFile>
25-
<Copyright>Copyright © 2022 by TechNobre</Copyright>
28+
<Copyright>Copyright © $(CurrentYear) by TechNobre</Copyright>
2629

2730
<Description>Hendler to standardize error responses</Description>
28-
<PackageReleaseNotes>
29-
Kickoff
30-
</PackageReleaseNotes>
31+
<PackageReleaseNotes>https://github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler/blob/main/CHANGELOG.md</PackageReleaseNotes>
3132
<Summary>Hendler to standardize error responses</Summary>
3233
<PackageTags>PowerUtils;Utils;Helpers;AspNetCore;ProblemDetails;ErrorHandler</PackageTags>
3334
<RepositoryUrl>https://github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler</RepositoryUrl>
@@ -66,23 +67,12 @@
6667
<PlatformTarget>AnyCPU</PlatformTarget>
6768
<DebugType>none</DebugType>
6869
<Optimize>true</Optimize>
69-
<DefineConstants>RELEASE;TRACE</DefineConstants>
70+
<DefineConstants>RELEASE</DefineConstants>
7071
<ErrorReport>none</ErrorReport>
7172
<WarningLevel>4</WarningLevel>
7273
</PropertyGroup>
7374

7475

75-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
76-
<DefineConstants>NETCOREAPP;NETCOREAPP3_1</DefineConstants>
77-
</PropertyGroup>
78-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
79-
<DefineConstants>NET;NET5</DefineConstants>
80-
</PropertyGroup>
81-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
82-
<DefineConstants>NET;NET6</DefineConstants>
83-
</PropertyGroup>
84-
85-
8676
<ItemGroup>
8777
<None Include="..\LICENSE" Pack="true" PackagePath="" />
8878
</ItemGroup>

src/ProblemDetailsFactory.cs

Lines changed: 9 additions & 42 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

@@ -74,12 +71,6 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
7471

7572
private (string Property, string Error) _mappingModelStateError(KeyValuePair<string, ModelStateEntry> modelStateError)
7673
{
77-
var error = modelStateError
78-
.Value
79-
.Errors
80-
.Select(s => s.ErrorMessage)
81-
.First();
82-
8374
if(modelStateError.Key.StartsWith("$."))
8475
{
8576
return (
@@ -88,6 +79,12 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
8879
);
8980
}
9081

82+
var error = modelStateError
83+
.Value
84+
.Errors
85+
.Select(s => s.ErrorMessage)
86+
.First();
87+
9188
return _checkRequestBody(modelStateError.Key, error);
9289
}
9390

@@ -111,39 +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-
var propertyParts = propertyName.Split('.');
121-
if(propertyParts.Length == 1)
122-
{
123-
return char.ToLowerInvariant(propertyName[0]) + propertyName[1..];
124-
}
125-
126-
127-
for(var count = 0; count < propertyParts.Length; count++)
128-
{
129-
propertyParts[count] = char.ToLowerInvariant(propertyParts[count][0]) + propertyParts[count][1..];
130-
}
131-
132-
133-
return string.Join(".", propertyParts);
134-
}
135-
136-
private static string _formatPropertyToSnakeCase(string propertyName)
137-
{
138-
var propertyParts = propertyName.Split('.');
139-
140-
for(var count = 0; count < propertyParts.Length; count++)
141-
{
142-
propertyParts[count] = propertyParts[count].ToSnakeCase();
143-
}
144-
145-
146-
return string.Join(".", propertyParts);
147-
}
148115
}
149116
}

0 commit comments

Comments
 (0)