Skip to content

Commit e9d9201

Browse files
authored
avoid redundant async in AsyncAPI (#7800)
* avoid redundant async in AsyncAPI * . * .
1 parent d193a08 commit e9d9201

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

samples/asyncapi/custom-message-types/Core_9/AsyncAPI.Feature/ApiDocumentGenerator.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace AsyncAPI.Feature;
1111

1212
public class ApiDocumentGenerator(IServiceProvider serviceProvider) : IAsyncApiDocumentGenerator
1313
{
14-
public async Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type> markupTypes, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
14+
public Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type> markupTypes, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
1515
{
1616
ArgumentNullException.ThrowIfNull(options);
1717

@@ -21,14 +21,14 @@ public async Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type
2121
var document = serviceProvider.GetRequiredService<IV3AsyncApiDocumentBuilder>();
2222
options.V3BuilderSetup?.Invoke(document);
2323

24-
await GenerateChannels(document, options, cancellationToken);
24+
GenerateChannels(document, options);
2525

2626
documents.Add(document.Build());
2727

28-
return documents;
28+
return Task.FromResult<IEnumerable<IAsyncApiDocument>>(documents);
2929
}
3030

31-
async Task GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
31+
void GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumentGenerationOptions options)
3232
{
3333
ArgumentNullException.ThrowIfNull(document);
3434
ArgumentNullException.ThrowIfNull(options);
@@ -47,13 +47,13 @@ async Task GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumen
4747
.WithAddress(typeCache.EndpointName)
4848
.WithDescription(actualType.FullName);
4949
});
50-
await GenerateV3OperationForAsync(document, channelName, channelBuilder, actualType, publishedType, options, cancellationToken);
50+
GenerateV3OperationFor(document, channelName, channelBuilder, actualType, publishedType, options);
5151
}
5252

5353
//NOTE this is where more channels and operations can be defined, for example subscribed to events, sent/received commands and messages
5454
}
5555

56-
Task GenerateV3OperationForAsync(IV3AsyncApiDocumentBuilder document, string channelName, IV3ChannelDefinitionBuilder channel, Type actualType, Type producedType, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
56+
static void GenerateV3OperationFor(IV3AsyncApiDocumentBuilder document, string channelName, IV3ChannelDefinitionBuilder channel, Type actualType, Type producedType, AsyncApiDocumentGenerationOptions options)
5757
{
5858
ArgumentNullException.ThrowIfNull(document);
5959
ArgumentException.ThrowIfNullOrWhiteSpace(channelName);
@@ -83,6 +83,5 @@ Task GenerateV3OperationForAsync(IV3AsyncApiDocumentBuilder document, string cha
8383
.WithChannel($"#/channels/{channelName}")
8484
.WithMessage(messageChannelReference);
8585
});
86-
return Task.CompletedTask;
8786
}
8887
}

samples/asyncapi/simple/Core_9/AsyncAPI.Feature/ApiDocumentGenerator.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ApiDocumentGenerator(IServiceProvider serviceProvider) : IAsyncApiDocumentGenerator
1111
{
12-
public async Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type> markupTypes, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
12+
public Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type> markupTypes, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
1313
{
1414
ArgumentNullException.ThrowIfNull(options);
1515

@@ -19,14 +19,14 @@ public async Task<IEnumerable<IAsyncApiDocument>> GenerateAsync(IEnumerable<Type
1919
var document = serviceProvider.GetRequiredService<IV3AsyncApiDocumentBuilder>();
2020
options.V3BuilderSetup?.Invoke(document);
2121

22-
await GenerateChannels(document, options, cancellationToken);
22+
GenerateChannels(document, options);
2323

2424
documents.Add(document.Build());
2525

26-
return documents;
26+
return Task.FromResult<IEnumerable<IAsyncApiDocument>>(documents);
2727
}
2828

29-
async Task GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
29+
void GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumentGenerationOptions options)
3030
{
3131
ArgumentNullException.ThrowIfNull(document);
3232
ArgumentNullException.ThrowIfNull(options);
@@ -46,20 +46,19 @@ async Task GenerateChannels(IV3AsyncApiDocumentBuilder document, AsyncApiDocumen
4646
.WithAddress(typeCache.EndpointName)
4747
.WithDescription(publishedEvent.FullName);
4848
});
49-
await GenerateV3OperationForAsync(
49+
GenerateV3OperationFor(
5050
document,
5151
channelName,
5252
channelBuilder,
5353
publishedEvent,
54-
options,
55-
cancellationToken);
54+
options);
5655
}
5756
#endregion
5857

5958
//NOTE this is where more channels and operations can be defined, for example subscribed to events, sent/received commands and messages
6059
}
6160

62-
Task GenerateV3OperationForAsync(IV3AsyncApiDocumentBuilder document, string channelName, IV3ChannelDefinitionBuilder channel, Type eventType, AsyncApiDocumentGenerationOptions options, CancellationToken cancellationToken = default)
61+
static void GenerateV3OperationFor(IV3AsyncApiDocumentBuilder document, string channelName, IV3ChannelDefinitionBuilder channel, Type eventType, AsyncApiDocumentGenerationOptions options)
6362
{
6463
ArgumentNullException.ThrowIfNull(document);
6564
ArgumentException.ThrowIfNullOrWhiteSpace(channelName);
@@ -88,6 +87,5 @@ Task GenerateV3OperationForAsync(IV3AsyncApiDocumentBuilder document, string cha
8887
.WithChannel($"#/channels/{channelName}")
8988
.WithMessage(messageChannelReference);
9089
});
91-
return Task.CompletedTask;
9290
}
9391
}

0 commit comments

Comments
 (0)