8000 Ability to hide URLs with regex (if required) · surgicalcoder/ApiClientGenerator@a51ac94 · GitHub
[go: up one dir, main page]

Skip to content

Commit a51ac94

Browse files
author
Monty
committed
Ability to hide URLs with regex (if required)
1 parent 387f92c commit a51ac94

File tree

8 files changed

+44
-17
lines changed

8 files changed

+44
-17
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.0.1</Version>
3+
<Version>1.1.0</Version>
44
</PropertyGroup>
55
</Project>
10000
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"OutputFile": "GeneratedApiClient.cs",
33
"Namespace": "GoLive.Generator.ApiClientGenerator.Tests.WebApi.Generated",
4-
"CustomDiscriminator": "CustomDiscriminatorCreate()",
4+
"CustomDiscriminator": "",
55
"Includes": [
66
"GoLive.Generator.ApiClientGenerator"
77
],
@@ -10,5 +10,9 @@
1010
],
1111
"PostAppendLines": [
1212
"// ReSharper disable All"
13-
]
13+
] ,
14+
"HideUrlsRegex": [
15+
"^/_]*",
16+
"^_]*"
17+
]
1418
}

GoLive.Generator.ApiClientGenerator.Tests.WebApi/Controllers/WeatherForecastController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public IEnumerable<WeatherForecast> Get()
3030
.ToArray();
3131
}
3232

33+
[HttpGet("_secretUrl")]
34+
public async Task<ActionResult> SecretUrl()
35+
{
36+
return Ok();
37+
}
38+
3339
public WeatherForecast GetSingle(int Id)
3440
{
3541
return null;

GoLive.Generator.ApiClientGenerator.Tests.WebApi/GeneratedApiClient.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
namespace GoLive.Generator.ApiClientGenerator.Tests.WebApi.Generated
99
{
10-
1110
public class ApiClient
1211
{
1312
private readonly HttpClient _client;
14-
1513
public ApiClient(HttpClient client)
1614
{
1715
_client = client;
@@ -24,23 +22,21 @@ public ApiClient(HttpClient client)
2422
public class WeatherForecastClient
2523
{
2624
private readonly HttpClient _client;
27-
28-
public WeatherForecastClient (HttpClient client)
25+
public WeatherForecastClient(HttpClient client)
2926
{
3027
_client = client;
3128
}
3229

3330
public async Task<global::System.Collections.Generic.IEnumerable<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast>> Get()
3431
{
3532
var result = await _client.GetAsync($"/WeatherForecast/Get");
36-
return await result.Content.ReadFromJsonAsync<global::System.Collections.Generic.IEnumerable<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast>>(CustomDiscriminatorCreate());
33+
return await result.Content.ReadFromJsonAsync<global::System.Collections.Generic.IEnumerable<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast>>();
3734
}
3835

39-
public async Task<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast> GetSingle(int Id )
36+
public async Task<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast> GetSingle(int Id)
4037
{
4138
var result = await _client.GetAsync($"/WeatherForecast/GetSingle/{Id}");
42-
return await result.Content.ReadFromJsonAsync<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast>(CustomDiscriminatorCreate());
39+
return await result.Content.ReadFromJsonAsync<global::GoLive.Generator.ApiClientGenerator.Tests.WebApi.WeatherForecast>();
4340
}
4441
}
45-
}
46-
// ReSharper disable All
42+
}// ReSharper disable All

GoLive.Generator.ApiClientGenerator/ApiClientGenerator.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text.Json;
6+
using System.Text.RegularExpressions;
67
using Microsoft.CodeAnalysis;
78

89
namespace GoLive.Generator.ApiClientGenerator
@@ -168,10 +169,7 @@ private void SetUpSingleApi(RouteGeneratorSettings config, ControllerRoute contr
168169
}
169170

170171
string useCustomFormatter = config.CustomDiscriminator;
171-
172-
source.AppendLine();
173-
source.AppendLine($"public async {returnType} {action.Name}({parameterList})");
174-
source.AppendOpenCurlyBracketLine();
172+
175173
string routeValue = string.Empty;
176174

177175
if (!string.IsNullOrWhiteSpace(action.Route))
@@ -203,6 +201,18 @@ private void SetUpSingleApi(RouteGeneratorSettings config, ControllerRoute contr
203201

204202
var routeString = $"$\"{routeValue}\"";
205203

204+
if (config.HideUrlsRegex is { Count: > 0 })
205+
{
206+
if (config.HideUrlsRegex.Any(e => Regex.IsMatch(routeValue, e)))
207+
{
208+
continue;
209+
}
210+
}
211+
212+
source.AppendLine();
213+
source.AppendLine($"public async {returnType} {action.Name}({parameterList})");
214+
source.AppendOpenCurlyBracketLine();
215+
206216
if (action.Mapping.Any(f => f.Key.ToLower() != "id" && action.Body?.Key != f.Key))
207217
{
208218
source.AppendLine("Dictionary<string, string> queryString=new();");

GoLive.Generator.ApiClientGenerator/GoLive.Generator.ApiClientGenerator.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
</PropertyGroup>
2626
<ItemGroup>
2727
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
28+
<PackageReference Include="System.Buffers" Version="4.5.1" GeneratePathProperty="true" PrivateAssets="all" />
29+
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" GeneratePathProperty="true" PrivateAssets="all" />
2830
<PackageReference Include="System.Text.Json" Version="5.0.1" GeneratePathProperty="true" PrivateAssets="all" />
2931
</ItemGroup>
3032
<PropertyGroup>
3133
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
3234
</PropertyGroup>
3335
<Target Name="GetDependencyTargetPaths" AfterTargets="ResolvePackageDependenciesForBuild">
3436
<ItemGroup>
37+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Buffers)\lib\netstandard2.0\System.Buffers.dll" IncludeRuntimeDependency="false" />
38+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Numerics_Vectors)\lib\netstandard2.0\System.Numerics.Vectors.dll" IncludeRuntimeDependency="false" />
3539
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" IncludeRuntimeDependency="false" />
3640
<TargetPathWithTargetPlatformMoniker Include="@(ResolvedCompileFileDefinitions)" IncludeRuntimeDependency="false" />
3741
</ItemGroup>

GoLive.Generator.ApiClientGenerator/RouteGeneratorSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public class RouteGeneratorSettings
1414

1515
public List<String> PreAppendLines { get; set; }
1616
public List<String> PostAppendLines { get; set; }
17+
18+
public List<String> HideUrlsRegex { get; set; }
1719
}
1820
}

GoLive.Generator.ApiClientGenerator/SourceStringBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Text;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
35

46
namespace GoLive.Generator.ApiClientGenerator
57
{
@@ -68,7 +70,10 @@ public void AppendLine(string text)
6870

6971
public override string ToString()
7072
{
71-
return _stringBuilder.ToString();
73+
var text = _stringBuilder.ToString();
74+
return string.IsNullOrWhiteSpace(text)
75+
? string.Empty
76+
: CSharpSyntaxTree.ParseText(text).GetRoot().NormalizeWhitespace().SyntaxTree.GetText().ToString();
7277
}
7378
}
7479
}

0 commit comments

Comments
 (0)
0