Skip to content

Commit

Permalink
Merge pull request #95 from tochka-public/CSHARP-182/master
Browse files Browse the repository at this point in the history
Don't break on void and task methods
  • Loading branch information
OptimumDev authored Mar 4, 2024
2 parents 3dfa794 + 98d24ac commit 258d4d8
Show file tree
Hide file tree
Showing 9 changed files with 1,307 additions and 1,125 deletions.
5 changes: 5 additions & 0 deletions src/Tochka.JsonRpc.ApiExplorer/TypeEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public Type CreateResponseType(string actionFullName, string methodName, Type re
return definedType;
}

if (resultType == typeof(void))
{
resultType = typeof(object);
}

var responseType = typeof(Response<>).MakeGenericType(resultType);
return GenerateTypeWithInfoAttribute(responseTypeName, responseType, resultType, serializerOptionsProviderType, methodName);
}
Expand Down
1 change: 1 addition & 0 deletions src/Tochka.JsonRpc.Server/Filters/JsonRpcResultFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void OnResultExecuted(ResultExecutedContext context)
ObjectResult { StatusCode: >= 400 } result => errorFactory.HttpError(result.StatusCode.Value, result.Value),
ObjectResult result => result.Value,
StatusCodeResult { StatusCode: >= 400 } result => errorFactory.HttpError(result.StatusCode, null),
StatusCodeResult { StatusCode: >= 200 and < 300 } => null,
EmptyResult => null,
_ => actionResult
};
Expand Down
11 changes: 11 additions & 0 deletions src/tests/Tochka.JsonRpc.ApiExplorer.Tests/TypeEmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,17 @@ public void CreateResponseType_ResultTypeWithoutEmptyConstructor_HasMetadataAttr
attributes.Should().ContainEquivalentOf(new JsonRpcTypeMetadataAttribute(serializerOptionsProviderType, MethodName));
}

[Test]
public void CreateResponseType_ResultTypeIsVoid_GenericArgumentIsObject()
{
var resultType = typeof(void);
var serializerOptionsProviderType = typeof(SnakeCaseJsonSerializerOptionsProvider);

var responseType = typeEmitter.CreateResponseType(ActionFullName, MethodName, resultType, serializerOptionsProviderType);

responseType.Should().BeAssignableTo<Response<object>>();
}

private const string ActionFullName = "namespace.controller.action";
private const string MethodName = "method";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ internal class IntegrationTests : IntegrationTestsBase<Program>
[TestCase("return_error_from_factory")]
[TestCase("return_mvc_error")]
[TestCase("error_throw_as_response_exception")]
[TestCase("void_method")]
[TestCase("task_method")]
[TestCase("empty_ok")]
public async Task GetDocument_ReturnsAllMethods(string method)
{
var response = await ApiClient.GetAsync("/openrpc/jsonrpc_v1.json");
Expand Down
Loading

0 comments on commit 258d4d8

Please sign in to comment.