Skip to content

Commit

Permalink
Merge pull request tomkuijsten#96 from Jark/feature/check-uriformat-c…
Browse files Browse the repository at this point in the history
…ould-not-be-mapped

Added more validation around UriFormat to method mapping logic.
  • Loading branch information
tomkuijsten authored Nov 10, 2016
2 parents 523c65b + 1c48261 commit 0768645
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 0768645

Please sign in to comment.