Skip to content

Commit 0e1bc51

Browse files
Pass cancellationToken in MultipartForm WriteToAsync (#8615)
1 parent 5e66c01 commit 0e1bc51

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/MultiPartFormDataBinaryContentDefinition.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private MethodProvider BuildWriteToMethod()
342342
var streamParam = new ParameterProvider("stream", FormattableStringHelpers.Empty, typeof(Stream));
343343
var streamExpression = (ValueExpression)streamParam;
344344
var cancellationTokenParam = KnownParameters.CancellationTokenParameter;
345-
var cancellatinTokenExpression = (ValueExpression)cancellationTokenParam;
345+
var cancellationTokenExpression = (ValueExpression)cancellationTokenParam;
346346
var signature = new MethodSignature(
347347
Name: "WriteTo",
348348
Description: null,
@@ -363,7 +363,7 @@ private MethodProvider BuildWriteToMethod()
363363
new IfElsePreprocessorStatement(
364364
"NET6_0_OR_GREATER",
365365
/*_multipartContent.CopyTo(stream, cancellationToken);*/
366-
_multipartContentExpression.CopyTo(streamExpression, cancellatinTokenExpression).Terminate(),
366+
_multipartContentExpression.CopyTo(streamExpression, cancellationTokenExpression).Terminate(),
367367
taskWaitCompletedStatementsList.ToArray()),
368368
};
369369
return new MethodProvider(signature, body, this);
@@ -374,7 +374,7 @@ private MethodProvider BuildWriteToAsyncMethod()
374374
var streamParam = new ParameterProvider("stream", FormattableStringHelpers.Empty, typeof(Stream));
375375
var streamExpression = (ValueExpression)streamParam;
376376
var cancellationTokenParam = KnownParameters.CancellationTokenParameter;
377-
var cancellatinTokenExpression = (ValueExpression)cancellationTokenParam;
377+
var cancellationTokenExpression = (ValueExpression)cancellationTokenParam;
378378

379379
var signature = new MethodSignature(
380380
Name: "WriteToAsync",
@@ -392,7 +392,7 @@ private MethodProvider BuildWriteToAsyncMethod()
392392
new IfElsePreprocessorStatement(
393393
"NET6_0_OR_GREATER",
394394
/*await _multipartContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);,*/
395-
_multipartContentExpression.CopyToAsync(streamExpression, cancellatinTokenExpression).Terminate(),
395+
_multipartContentExpression.CopyToAsync(streamExpression, cancellationTokenExpression).Terminate(),
396396
/*await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);*/
397397
_multipartContentExpression.CopyToAsync(streamExpression).Terminate()),
398398
};

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Snippets/MultiPartFormDataContentSnippets.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ internal static class MultiPartFormDataContentSnippets
1414
public static ScopedApi<HttpContentHeaders> Headers(this ScopedApi<MultipartFormDataContent> multipartContent)
1515
=> multipartContent.Property(nameof(MultipartFormDataContent.Headers)).As<HttpContentHeaders>();
1616

17+
// Older .NET versions do not have the CancellationToken parameter, so we provide an overload without it.
1718
public static InvokeMethodExpression CopyToAsync(this ScopedApi<MultipartFormDataContent> multipartContent, ValueExpression destination, bool isAsync = true)
1819
=> multipartContent.Invoke(nameof(MultipartFormDataContent.CopyToAsync), [destination], null, isAsync, isAsync);
1920

2021
public static InvokeMethodExpression CopyToAsync(this ScopedApi<MultipartFormDataContent> multipartContent, ValueExpression destination, ValueExpression cancellationTokenExpression)
21-
=> multipartContent.Invoke(nameof(MultipartFormDataContent.CopyToAsync), [destination], null, true, true);
22+
=> multipartContent.Invoke(nameof(MultipartFormDataContent.CopyToAsync), [destination, cancellationTokenExpression], null, true, true);
2223

2324
public static InvokeMethodExpression CopyTo(this ScopedApi<MultipartFormDataContent> multipartContent, ValueExpression destination, ValueExpression cancellationTokenExpression)
2425
=> multipartContent.Invoke(nameof(MultipartFormDataContent.CopyTo), [destination, Default, cancellationTokenExpression]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Linq;
5+
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
6+
using Microsoft.TypeSpec.Generator.Tests.Common;
7+
using NUnit.Framework;
8+
9+
namespace Microsoft.TypeSpec.Generator.ClientModel.Tests.Providers.Definitions
10+
{
11+
public class MultiPartFormDataBinaryContentDefinitionTests
12+
{
13+
[Test]
14+
public void WriteToMethodIsCorrectlyDefined()
15+
{
16+
MockHelpers.LoadMockGenerator();
17+
18+
var multiPartFormData = new MultiPartFormDataBinaryContentDefinition();
19+
Assert.IsNotNull(multiPartFormData.Methods);
20+
var writeToMethod = multiPartFormData.Methods.Single(m => m.Signature.Name == "WriteTo");
21+
Assert.IsNotNull(writeToMethod);
22+
Assert.IsNotNull(writeToMethod.BodyStatements);
23+
Assert.AreEqual(Helpers.GetExpectedFromFile(), writeToMethod.BodyStatements!.ToDisplayString());
24+
}
25+
26+
[Test]
27+
public void WriteToAsyncMethodIsCorrectlyDefined()
28+
{
29+
MockHelpers.LoadMockGenerator();
30+
31+
var multiPartFormData = new MultiPartFormDataBinaryContentDefinition();
32+
Assert.IsNotNull(multiPartFormData.Methods);
33+
var writeToMethod = multiPartFormData.Methods.Single(m => m.Signature.Name == "WriteToAsync");
34+
Assert.IsNotNull(writeToMethod);
35+
Assert.IsNotNull(writeToMethod.BodyStatements);
36+
Assert.AreEqual(Helpers.GetExpectedFromFile(), writeToMethod.BodyStatements!.ToDisplayString());
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if NET6_0_OR_GREATER
2+
await _multipartContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
3+
#else
4+
await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);
5+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if NET6_0_OR_GREATER
2+
_multipartContent.CopyTo(stream, default, cancellationToken);
3+
#else
4+
_multipartContent.CopyToAsync(stream).GetAwaiter().GetResult();
5+
#endif

0 commit comments

Comments
 (0)