Skip to content

Commit

Permalink
Merge pull request #305 from DiscoPYF/postDocumentReturnType
Browse files Browse the repository at this point in the history
Add a PostDocumentAsync with extra generic for new doc type.
  • Loading branch information
DiscoPYF authored Nov 13, 2020
2 parents 5db3cde + 9206987 commit 26a7327
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
20 changes: 20 additions & 0 deletions arangodb-net-standard.Test/DocumentApi/DocumentApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ public async Task PostDocument_ShouldSucceed_WhenNewDocIsReturned()
Assert.Equal(123, (int)response.New.test);
}

[Fact]
public async Task PostDocument_ShouldSucceed_WhenNewDocIsReturnedWithDifferentType()
{
var doc = new PostDocumentMockModelNew
{
Message = "Hello"
};
var response = await _docClient.PostDocumentAsync<PostDocumentMockModelNew, PostDocumentMockModel>(
_testCollection,
doc,
new PostDocumentsQuery
{
ReturnNew = true
});
Assert.Null(response.Old);
Assert.NotNull(response.New);
Assert.Equal(doc.Message, response.New.Message);
Assert.Equal(response._id, response.New._id);
}

[Fact]
public async Task PostDocument_ShouldFail_WhenDocumentIsInvalid()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ArangoDBNetStandard.DocumentApi.Models;

namespace ArangoDBNetStandardTest.DocumentApi.Models
{
public class PostDocumentMockModel : DocumentBase
{
public string Message { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace ArangoDBNetStandardTest.DocumentApi.Models
{
public class PostDocumentMockModelNew
{
public string Message { get; set; }
}
}
31 changes: 29 additions & 2 deletions arangodb-net-standard/DocumentApi/DocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,34 @@ public DocumentApiClient(IApiClientTransport client, IApiClientSerialization ser
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
public virtual Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
string collectionName,
T document,
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
return PostDocumentAsync<T, T>(
collectionName,
document,
query,
serializationOptions);
}

/// <summary>
/// Post a single document with the possibility to specify a different type
/// for the new document object returned in the response.
/// </summary>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <typeparam name="U">Type of the returned document, only applies when
/// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/>
/// are used.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentResponse<U>> PostDocumentAsync<T, U>(
string collectionName,
T document,
PostDocumentsQuery query = null,
Expand All @@ -72,7 +99,7 @@ public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync();
return DeserializeJsonFromStream<PostDocumentResponse<T>>(stream);
return DeserializeJsonFromStream<PostDocumentResponse<U>>(stream);
}
throw await GetApiErrorException(response);
}
Expand Down
22 changes: 21 additions & 1 deletion arangodb-net-standard/DocumentApi/IDocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IDocumentApiClient
/// <summary>
/// Post a single document.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
Expand All @@ -27,6 +27,26 @@ Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null);

/// <summary>
/// Post a single document with the possibility to specify a different type
/// for the new document object returned in the response.
/// </summary>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <typeparam name="U">Type of the returned document, only applies when
/// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/>
/// are used.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
Task<PostDocumentResponse<U>> PostDocumentAsync<T, U>(
string collectionName,
T document,
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null);

/// <summary>
/// Post multiple documents in a single request.
/// </summary>
Expand Down

0 comments on commit 26a7327

Please sign in to comment.