Skip to content

Commit 28dccf5

Browse files
committed
Address feedback
1 parent 4bff0d2 commit 28dccf5

10 files changed

+55
-90
lines changed

src/OpenApi/build/Microsoft.AspNetCore.OpenApi.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<Target Name="GenerateAdditionalXmlFilesForOpenApi"
99
AfterTargets="ResolveReferences">
1010
<ItemGroup>
11-
<Foo Include="@(ReferencePath)" />
1211
<AdditionalFiles
1312
Include="@(ReferencePath->'%(RootDir)%(Directory)%(Filename).xml')"
1413
Condition="'%(ReferencePath.Extension)' == '.dll' AND

src/OpenApi/gen/Helpers/AddOpenApiOverloadVariant.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ internal enum AddOpenApiOverloadVariant
99
AddOpenApiDocumentName,
1010
AddOpenApiDocumentNameConfigureOptions,
1111
AddOpenApiConfigureOptions,
12+
Unknown
1213
}

src/OpenApi/gen/XmlCommentGenerator.Emitter.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,7 @@ file record XmlComment(
7878
file static class XmlCommentCache
7979
{
8080
private static Dictionary<(Type?, string?), XmlComment>? _cache;
81-
public static Dictionary<(Type?, string?), XmlComment> Cache
82-
{
83-
get
84-
{
85-
if (_cache is null)
86-
{
87-
_cache = GenerateCacheEntries();
88-
}
89-
return _cache;
90-
}
91-
}
81+
public static Dictionary<(Type?, string?), XmlComment> Cache => _cache ??= GenerateCacheEntries();
9282
9383
private static Dictionary<(Type?, string?), XmlComment> GenerateCacheEntries()
9484
{
@@ -114,17 +104,17 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
114104
}
115105
if (XmlCommentCache.Cache.TryGetValue((methodInfo.DeclaringType, methodInfo.Name), out var methodComment))
116106
{
117-
if (methodComment.Summary is not null)
107+
if (methodComment.Summary is { } summary)
118108
{
119-
operation.Summary = methodComment.Summary;
109+
operation.Summary = summary;
120110
}
121-
if (methodComment.Description is not null)
111+
if (methodComment.Description is { } description)
122112
{
123-
operation.Description = methodComment.Description;
113+
operation.Description = description;
124114
}
125-
if (methodComment.Remarks is not null)
115+
if (methodComment.Remarks is { } remarks)
126116
{
127-
operation.Description = methodComment.Remarks;
117+
operation.Description = remarks;
128118
}
129119
if (methodComment.Parameters is { Count: > 0})
130120
{
@@ -285,7 +275,7 @@ public static IServiceCollection AddOpenApi(this IServiceCollection services, st
285275
});
286276
}
287277
""",
288-
_ => throw new InvalidOperationException("Invalid overload variant for `AddOpenApi`.")
278+
_ => string.Empty // Effectively no-op for AddOpenApi invocations that do not conform to a variant
289279
};
290280

291281
internal static string GenerateAddOpenApiInterceptions(ImmutableArray<(AddOpenApiInvocation Source, int Index, ImmutableArray<InterceptableLocation?> Elements)> groupedAddOpenApiInvocations)

src/OpenApi/gen/XmlCommentGenerator.Parser.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static string ReplaceGenericArguments(string typeName)
101101
// Replace everything between < and > with empty strings separated by commas
102102
var segment = result.ToString(start + 1, i - start - 1);
103103
var commaCount = segment.Count(c => c == ',');
104-
var replacement = string.Join(",", Enumerable.Repeat("", commaCount + 1));
104+
var replacement = new string(',', commaCount);
105105
result.Remove(start + 1, i - start - 1);
106106
result.Insert(start + 1, replacement);
107107
i = start + replacement.Length + 1;
@@ -143,6 +143,17 @@ internal static bool FilterInvocations(SyntaxNode node, CancellationToken _)
143143
internal static AddOpenApiInvocation GetAddOpenApiOverloadVariant(GeneratorSyntaxContext context, CancellationToken cancellationToken)
144144
{
145145
var invocationExpression = (InvocationExpressionSyntax)context.Node;
146+
147+
// Soft check to validate that the method is from the OpenApiServiceCollectionExtensions class
148+
// in the Microsoft.AspNetCore.OpenApi assembly.
149+
var symbol = context.SemanticModel.GetSymbolInfo(invocationExpression, cancellationToken).Symbol;
150+
if (symbol is not IMethodSymbol methodSymbol
151+
|| methodSymbol.ContainingType.Name != "OpenApiServiceCollectionExtensions"
152+
|| methodSymbol.ContainingAssembly.Name != "Microsoft.AspNetCore.OpenApi")
153+
{
154+
return new(AddOpenApiOverloadVariant.Unknown, invocationExpression, null);
155+
}
156+
146157
var interceptableLocation = context.SemanticModel.GetInterceptableLocation(invocationExpression, cancellationToken);
147158
var argumentsCount = invocationExpression.ArgumentList.Arguments.Count;
148159
if (argumentsCount == 0)

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<HelixContent Include="$(MSBuildProjectDirectory)\snapshots\**" LinkBase="$(MSBuildThisFileDirectory)\snapshots"/>
34+
<HelixContent
35+
Include="$(MSBuildProjectDirectory)\snapshots\**"
36+
LinkBase="$(MSBuildThisFileDirectory)\snapshots"/>
3537
</ItemGroup>
3638

3739
</Project>

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/AddOpenApiTests.CanInterceptAddOpenApi#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ file record XmlComment(
5959
file static class XmlCommentCache
6060
{
6161
private static Dictionary<(Type?, string?), XmlComment>? _cache;
62-
public static Dictionary<(Type?, string?), XmlComment> Cache
63-
{
64-
get
65-
{
66-
if (_cache is null)
67-
{
68-
_cache = GenerateCacheEntries();
69-
}
70-
return _cache;
71-
}
72-
}
62+
public static Dictionary<(Type?, string?), XmlComment> Cache => _cache ??= GenerateCacheEntries();
7363

7464
private static Dictionary<(Type?, string?), XmlComment> GenerateCacheEntries()
7565
{
@@ -95,17 +85,17 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
9585
}
9686
if (XmlCommentCache.Cache.TryGetValue((methodInfo.DeclaringType, methodInfo.Name), out var methodComment))
9787
{
98-
if (methodComment.Summary is not null)
88+
if (methodComment.Summary is { } summary)
9989
{
100-
operation.Summary = methodComment.Summary;
90+
operation.Summary = summary;
10191
}
102-
if (methodComment.Description is not null)
92+
if (methodComment.Description is { } description)
10393
{
104-
operation.Description = methodComment.Description;
94+
operation.Description = description;
10595
}
106-
if (methodComment.Remarks is not null)
96+
if (methodComment.Remarks is { } remarks)
10797
{
108-
operation.Description = methodComment.Remarks;
98+
operation.Description = remarks;
10999
}
110100
if (methodComment.Parameters is { Count: > 0})
111101
{

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromControllers#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ file record XmlComment(
5959
file static class XmlCommentCache
6060
{
6161
private static Dictionary<(Type?, string?), XmlComment>? _cache;
62-
public static Dictionary<(Type?, string?), XmlComment> Cache
63-
{
64-
get
65-
{
66-
if (_cache is null)
67-
{
68-
_cache = GenerateCacheEntries();
69-
}
70-
return _cache;
71-
}
72-
}
62+
public static Dictionary<(Type?, string?), XmlComment> Cache => _cache ??= GenerateCacheEntries();
7363

7464
private static Dictionary<(Type?, string?), XmlComment> GenerateCacheEntries()
7565
{
@@ -98,17 +88,17 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
9888
}
9989
if (XmlCommentCache.Cache.TryGetValue((methodInfo.DeclaringType, methodInfo.Name), out var methodComment))
10090
{
101-
if (methodComment.Summary is not null)
91+
if (methodComment.Summary is { } summary)
10292
{
103-
operation.Summary = methodComment.Summary;
93+
operation.Summary = summary;
10494
}
105-
if (methodComment.Description is not null)
95+
if (methodComment.Description is { } description)
10696
{
107-
operation.Description = methodComment.Description;
97+
operation.Description = description;
10898
}
109-
if (methodComment.Remarks is not null)
99+
if (methodComment.Remarks is { } remarks)
110100
{
111-
operation.Description = methodComment.Remarks;
101+
operation.Description = remarks;
112102
}
113103
if (methodComment.Parameters is { Count: > 0})
114104
{

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromMinimalApis#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ file record XmlComment(
5959
file static class XmlCommentCache
6060
{
6161
private static Dictionary<(Type?, string?), XmlComment>? _cache;
62-
public static Dictionary<(Type?, string?), XmlComment> Cache
63-
{
64-
get
65-
{
66-
if (_cache is null)
67-
{
68-
_cache = GenerateCacheEntries();
69-
}
70-
return _cache;
71-
}
72-
}
62+
public static Dictionary<(Type?, string?), XmlComment> Cache => _cache ??= GenerateCacheEntries();
7363

7464
private static Dictionary<(Type?, string?), XmlComment> GenerateCacheEntries()
7565
{
@@ -102,17 +92,17 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
10292
}
10393
if (XmlCommentCache.Cache.TryGetValue((methodInfo.DeclaringType, methodInfo.Name), out var methodComment))
10494
{
105-
if (methodComment.Summary is not null)
95+
if (methodComment.Summary is { } summary)
10696
{
107-
operation.Summary = methodComment.Summary;
97+
operation.Summary = summary;
10898
}
109-
if (methodComment.Description is not null)
99+
if (methodComment.Description is { } description)
110100
{
111-
operation.Description = methodComment.Description;
101+
operation.Description = description;
112102
}
113-
if (methodComment.Remarks is not null)
103+
if (methodComment.Remarks is { } remarks)
114104
{
115-
operation.Description = methodComment.Remarks;
105+
operation.Description = remarks;
116106
}
117107
if (methodComment.Parameters is { Count: > 0})
118108
{

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.SupportsXmlCommentsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ file record XmlComment(
5959
file static class XmlCommentCache
6060
{
6161
private static Dictionary<(Type?, string?), XmlComment>? _cache;
62-
public static Dictionary<(Type?, string?), XmlComment> Cache
63-
{
64-
get
65-
{
66-
if (_cache is null)
67-
{
68-
_cache = GenerateCacheEntries();
69-
}
70-
return _cache;
71-
}
72-
}
62+
public static Dictionary<(Type?, string?), XmlComment> Cache => _cache ??= GenerateCacheEntries();
7363

7464
private static Dictionary<(Type?, string?), XmlComment> GenerateCacheEntries()
7565
{
@@ -123,17 +113,17 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
123113
}
124114
if (XmlCommentCache.Cache.TryGetValue((methodInfo.DeclaringType, methodInfo.Name), out var methodComment))
125115
{
126-
if (methodComment.Summary is not null)
116+
if (methodComment.Summary is { } summary)
127117
{
128-
operation.Summary = methodComment.Summary;
118+
operation.Summary = summary;
129119
}
130-
if (methodComment.Description is not null)
120+
if (methodComment.Description is { } description)
131121
{
132-
operation.Description = methodComment.Description;
122+
operation.Description = description;
133123
}
134-
if (methodComment.Remarks is not null)
124+
if (methodComment.Remarks is { } remarks)
135125
{
136-
operation.Description = methodComment.Remarks;
126+
operation.Description = remarks;
137127
}
138128
if (methodComment.Parameters is { Count: > 0})
139129
{

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Microsoft.AspNetCore.OpenApi.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<Import Project="$(MvcTestingTargets)" Condition="'$(MvcTestingTargets)' != ''" />
3737

3838
<ItemGroup>
39-
<HelixContent Include="$(MSBuildProjectDirectory)\Integration\snapshots\**" LinkBase="$(MSBuildThisFileDirectory)\Integration\snapshots"/>
39+
<HelixContent
40+
Include="$(MSBuildProjectDirectory)\Integration\snapshots\**"
41+
LinkBase="$(MSBuildThisFileDirectory)\Integration\snapshots"/>
4042
</ItemGroup>
4143
</Project>

0 commit comments

Comments
 (0)