Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum in FromQuery and FromRoute #99

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Acheve.TestHost/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ internal static bool IsPrimitiveType(this Type typeToInspect)
|| type == typeof(string)
|| type == typeof(decimal)
|| type == typeof(Guid)
|| type.IsDateTime();
|| type.IsDateTime()
|| type.IsEnum;
}

internal static bool IsDateTime(this Type typeToInspect)
Expand Down
4 changes: 3 additions & 1 deletion src/Acheve.TestHost/Routing/UriDiscover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@
throw new InvalidOperationException($"The action selector is not a valid lambda expression");
}

var methodCall = (MethodCallExpression)actionSelector.Body;
var methodCall = actionSelector.Body as MethodCallExpression;
if (methodCall is null)
methodCall = (actionSelector.Body as UnaryExpression).Operand as MethodCallExpression;

var action = new TestServerAction(methodCall.Method);

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
methodCall
may be null at this access because of
this
assignment.
bool haveAttributeApiController = typeof(TController).GetTypeInfo().GetCustomAttribute(typeof(ApiControllerAttribute)) != null;
bool isGetOrDelete = action.MethodInfo.GetCustomAttributes().FirstOrDefault(attr => attr.GetType() == typeof(HttpGetAttribute)
|| attr.GetType() == typeof(HttpDeleteAttribute)) != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ public ActionResult<ParamWithDifferentFroms> PostWithDifferentFroms(ParamWithDif
[HttpPut($"{nameof(PutWithDifferentFroms)}/{{{nameof(ParamWithDifferentFroms.ParamFromRoute)}}}")]
public ActionResult<ParamWithDifferentFroms> PutWithDifferentFroms(ParamWithDifferentFroms request)
=> Ok(request);

[HttpGet($"{nameof(GetWithEnumInRoute)}/{{request}}")]
public string GetWithEnumInRoute([FromRoute] SampleEnumeration request)
=> request.ToString();

[HttpGet($"{nameof(GetWithEnumInQuery)}")]
public string GetWithEnumInQuery([FromQuery] SampleEnumeration request)
=> request.ToString();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UnitTests.Acheve.TestHost.Routing.Models;

public enum SampleEnumeration
{
Value1,
Value2,
Value3
}
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,40 @@ public async Task Create_put_request_with_object_with_different_froms()
content.ParamFromBody.Should().Be(model.ParamFromBody);
}

[Fact]
public async Task Create_get_request_with_enum_from_query()
{
var server = new TestServerBuilder()
.UseDefaultStartup()
.Build();

var enumValue = SampleEnumeration.Value3;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.GetWithEnumInQuery(enumValue));
var responseMessage = await request.GetAsync();

await responseMessage.IsSuccessStatusCodeOrThrow();
var response = await responseMessage.Content.ReadAsStringAsync();
response.Should().Be(enumValue.ToString());
}

[Fact]
public async Task Create_get_request_with_enum_from_route()
{
var server = new TestServerBuilder()
.UseDefaultStartup()
.Build();

var enumValue = SampleEnumeration.Value3;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.GetWithEnumInRoute(enumValue));
var responseMessage = await request.GetAsync();

await responseMessage.IsSuccessStatusCodeOrThrow();
var response = await responseMessage.Content.ReadAsStringAsync();
response.Should().Be(enumValue.ToString());
}

private class PrivateNonControllerClass
{
public int SomeAction()
Expand Down
Loading