Skip to content

Commit 7b60674

Browse files
authored
Merge pull request #9 from Jalalx/bugfix/string-param-route
fixed a bug related to string parameter in route
2 parents 37f84c3 + 0221ffc commit 7b60674

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

samples/SampleRestApi/Controllers/UserController.cs

+19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ public IActionResult SearchByName(string name)
6060
return Ok(result);
6161
}
6262

63+
[HttpGet("by-name/{name}")]
64+
public IActionResult GetByName(string name)
65+
{
66+
if (string.IsNullOrWhiteSpace(name))
67+
{
68+
return BadRequest();
69+
}
70+
71+
var result = _users.FirstOrDefault(x =>
72+
x.FirstName.Contains(name, StringComparison.OrdinalIgnoreCase) ||
73+
x.LastName.Contains(name, StringComparison.OrdinalIgnoreCase));
74+
if (result == null)
75+
{
76+
return NotFound();
77+
}
78+
79+
return Ok(result);
80+
}
81+
6382
[HttpGet("wrapped/search")]
6483
public IActionResult WrappedSearchByName(string name)
6584
{

src/HttpClientGenerator/HttpClientGenerator.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
4-
<Version>0.6.0</Version>
4+
<Version>0.6.1</Version>
55
<LangVersion>9.0</LangVersion>
66
</PropertyGroup>
77
<PropertyGroup>

src/HttpClientGenerator/Internals/PartialServiceClassSourceBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private void DefinePathAndRouteAndQueryParameter(SourceBuilder source, Attribute
309309

310310
private bool IsValidParameter(ITypeSymbol type)
311311
{
312-
if (!type.IsValueType)
312+
if (type.FullName() != typeof(string).FullName && !type.IsValueType)
313313
{
314314
return false;
315315
}

tests/HttpClientCodeGeneratorIntegrationTests/Basics/MyHttpClientBasicScenarioTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ public async Task GetUser_ByValidId_ReturnsExpectedValue()
3838
Assert.Equal("+981234567", user.PhoneNumber);
3939
}
4040

41+
// Fetch by name
42+
[Fact]
43+
public async Task GetUser_ByValidName_ReturnsExpectedValue()
44+
{
45+
// Arrange
46+
var client = _factory.CreateClient();
47+
var myClient = new MyHttpService(client);
48+
49+
// Act
50+
var user = await myClient.GetUserByNameAsync("Will");
51+
52+
// Assert
53+
Assert.Equal("Will", user.FirstName);
54+
Assert.Equal("Smith", user.LastName);
55+
Assert.Equal("+981234567", user.PhoneNumber);
56+
}
57+
4158
[Fact]
4259
public async Task GetUser_ByInvalidId_Throws404HttpRequestException()
4360
{

tests/HttpClientCodeGeneratorIntegrationTests/Basics/MyHttpService.cs

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public MyHttpService(HttpClient httpClient)
2525
[HttpGet("user/search")]
2626
public partial Task<IEnumerable<User>> SearchUserByNameAsync(string name);
2727

28+
[HttpGet("user/by-name/{name}")]
29+
public partial Task<User> GetUserByNameAsync(string name);
30+
2831
[HttpPost("user")]
2932
public partial Task<User> CreateUser(User user);
3033

0 commit comments

Comments
 (0)