Skip to content

Commit

Permalink
Added more validation around UriFormat to method mapping logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jark committed Nov 1, 2016
1 parent 523c65b commit 1c48261
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,54 @@ public async Task RegisterController_TwoDifferentControllersWithSimilarlyNamedMe
await AssertHandleRequest("/Post", HttpMethod.POST, HttpResponseStatus.Created);
}

private class UriFormatWitMisnamedPathInUrlController
{
[UriFormat("/Get/{param}/")]
public IGetResponse Get(string param2) => new GetResponse(GetResponse.ResponseStatus.OK, param2);
}

[TestMethod]
public void RegisterController_AControllerWithMismatchedParametersInPath_ThrowsException()
{
AssertRegisterControllerThrows<UriFormatWitMisnamedPathInUrlController>();
}

private class UriFormatWitMisnamedUriParameterInUrlController
{
[UriFormat("/Get/?param={param}")]
public IGetResponse Get(string param2) => new GetResponse(GetResponse.ResponseStatus.OK, param2);
}

[TestMethod]
public void RegisterController_AControllerWithMismatchedParametersInUriParameters_ThrowsException()
{
AssertRegisterControllerThrows<UriFormatWitMisnamedUriParameterInUrlController>();
}

private class UriFormatWithMoreInUrlPathController
{
[UriFormat("/Get/{param}/{param2}")]
public IGetResponse Get(string param) => new GetResponse(GetResponse.ResponseStatus.OK, param);
}

[TestMethod]
public void RegisterController_AControllerWithMoreInUrlPath_ThrowsException()
{
AssertRegisterControllerThrows<UriFormatWithMoreInUrlPathController>();
}

private class UriFormatWithMoreInUrlParameterController
{
[UriFormat("/Get/?param={param}&param2={param}")]
public IGetResponse Get(string param) => new GetResponse(GetResponse.ResponseStatus.OK, param);
}

[TestMethod]
public void RegisterController_AControllerWithMoreInUrlParameter_ThrowsException()
{
AssertRegisterControllerThrows<UriFormatWithMoreInUrlParameterController>();
}

private void AssertRegisterControllerThrows<T>(params string[] args) where T : class
{
Assert.ThrowsException<Exception>(() =>
Expand Down
17 changes: 12 additions & 5 deletions src/WebServer/Rest/RestControllerMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,23 @@ private bool TryGetContentParameterType(MethodInfo methodInfo, out Type content)

private ParameterValueGetter[] GetParameterGetters(MethodInfo methodInfo)
{
var fromUriParams = (from p in methodInfo.GetParameters()
where p.GetCustomAttribute<FromContentAttribute>() == null
select p).ToList();
var methodParameters = (from p in methodInfo.GetParameters()
where p.GetCustomAttribute<FromContentAttribute>() == null
select p).ToList();

if (!ParametersHaveValidType(fromUriParams.Select(p => p.ParameterType)))
if (!ParametersHaveValidType(methodParameters.Select(p => p.ParameterType)))
{
throw new InvalidOperationException("Can't use method parameters with a custom type.");
}

return fromUriParams.Select(x => GetParameterGetter(x, MatchUri)).ToArray();
var parameterValueGetters = methodParameters.Select(x => GetParameterGetter(x, MatchUri)).ToArray();
if (parameterValueGetters.Length !=
MatchUri.Parameters.Count + MatchUri.PathParts.Count(x => x.PartType == PathPart.PathPartType.Argument))
{
throw new Exception($"Uri format {MatchUri} has got more method parameters defined than the method has got.");
}

return parameterValueGetters;
}

private static ParameterValueGetter GetParameterGetter(ParameterInfo parameterInfo, ParsedUri matchUri)
Expand Down

0 comments on commit 1c48261

Please sign in to comment.