Skip to content

Commit 9f61808

Browse files
committed
Fixing FromServicesAttribute detection
1 parent 1d77b44 commit 9f61808

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
13
using Microsoft.AspNetCore.Mvc;
24

35
namespace GoLive.Generator.ApiClientGenerator.Tests.WebApi.Controllers;
@@ -35,4 +37,10 @@ public string HttpPatchTest()
3537
{
3638
return "Ok";
3739
}
40+
41+
[HttpGet]
42+
public async Task<ActionResult> FromServiceTest(string Id, [FromServices] HttpContent TestItem)
43+
{
44+
return Ok();
45+
}
3846
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,18 @@ public string HttpPatchTest_Url(QueryString queryString = default)
14501450
{
14511451
return $"/api/YetAnother/HttpPatchTest{queryString}";
14521452
}
1453+
1454+
public async Task<Response> FromServiceTest(string Id, QueryString queryString = default, CancellationToken _token = default)
1455+
{
1456+
using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/YetAnother/FromServiceTest/{Id}{queryString}");
1457+
using var result = await _client.SendAsync(request, _token);
1458+
return new Response(result.StatusCode, result.Headers);
1459+
}
1460+
1461+
public string FromServiceTest_Url(string Id, QueryString queryString = default)
1462+
{
1463+
return $"/api/YetAnother/FromServiceTest/{Id}{queryString}";
1464+
}
14531465
}
14541466

14551467
// JSON Source Generator

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,39 @@
20332033
"AllAttributes": [
20342034
"Microsoft.AspNetCore.Mvc.HttpPatchAttribute"
20352035
]
2036+
},
2037+
{
2038+
"Name": "FromServiceTest",
2039+
"FullMethodName": "GoLive.Generator.ApiClientGenerator.Tests.WebApi.Controllers.YetAnotherController.FromServiceTest",
2040+
"Method": {
2041+
"Method": "GET"
2042+
},
2043+
"Route": "",
2044+
"RouteSetByAttributes": false,
2045+
"ReturnTypeName": null,
2046+
"ReturnTypeStruct": true,
2047+
"HasCustomFormatter": false,
2048+
"Mapping": [
2049+
{
2050+
"Key": "Id",
2051+
"Parameter": {
2052+
"FullTypeName": "string",
2053+
"GenericTypeName": null,
2054+
"HasDefaultValue": false,
2055+
"DefaultValue": null,
2056+
"Nullable": false,
2057+
"Attributes": [],
2058+
"SpecialType": 20,
2059+
"AllowedStringValues": null
2060+
}
2061+
}
2062+
],
2063+
"Body": [],
2064+
"XmlComments": "",
2065+
"CalculatedURL": "/api/YetAnother/FromServiceTest/{Id}",
2066+
"AllAttributes": [
2067+
"Microsoft.AspNetCore.Mvc.HttpGetAttribute"
2068+
]
20362069
}
20372070
],
20382071
"XmlComments": "",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,9 @@ public static string YetAnother_HttpPatchTest(QueryString queryString = default)
294294
{
295295
return $"/api/YetAnother/HttpPatchTest{queryString}";
296296
}
297+
298+
public static string YetAnother_FromServiceTest(string Id, QueryString queryString = default)
299+
{
300+
return $"/api/YetAnother/FromServiceTest/{Id}{queryString}";
301+
}
297302
}

GoLive.Generator.ApiClientGenerator/Scanner.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ private static IEnumerable<ActionRoute> ScanForActionMethods(SemanticModel model
203203
)); })
204204
.ToArray();
205205

206-
var bodyParameter = methodSymbol.Parameters.Where(t => !IsPrimitive(t.Type) || t.GetAttributes().Any(e => e.AttributeClass?.Name == "FromBodyAttribute"))
206+
var bodyParameter = methodSymbol.Parameters
207+
.Where(t => !IsPrimitive(t.Type)
208+
&& t.GetAttributes().All(e => e.AttributeClass?.ToDisplayString() != "Microsoft.AspNetCore.Mvc.FromServicesAttribute")
209+
|| t.GetAttributes().Any(e => e.AttributeClass?.Name == "FromBodyAttribute")
210+
)
207211
.Select(t => new ParameterMapping(getParameterName(t), new Parameter(
208212
t.Type.ToString(),
209213
t.Type.OriginalDefinition is INamedTypeSymbol nts ? (nts.IsGenericType ? nts.ToDisplayString() : null ) : null,

0 commit comments

Comments
 (0)