Skip to content

Commit df0c459

Browse files
authored
Update accessibility check for types in same assembly (#61082)
1 parent 15cc4f4 commit df0c459

File tree

4 files changed

+111
-38
lines changed

4 files changed

+111
-38
lines changed

Diff for: src/OpenApi/gen/Helpers/AssemblyTypeSymbolsVisitor.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override void VisitNamedType(INamedTypeSymbol type)
4444
{
4545
_cancellationToken.ThrowIfCancellationRequested();
4646

47-
if (!IsDeclaredInAssembly(type) || !_exportedTypes.Add(type))
47+
if (!IsAccessibleType(type) || !_exportedTypes.Add(type))
4848
{
4949
return;
5050
}
@@ -61,7 +61,7 @@ public override void VisitNamedType(INamedTypeSymbol type)
6161
foreach (var property in properties)
6262
{
6363
_cancellationToken.ThrowIfCancellationRequested();
64-
if (IsDeclaredInAssembly(property) && _exportedProperties.Add(property))
64+
if (IsAccessibleType(property) && _exportedProperties.Add(property))
6565
{
6666
property.Type.Accept(this);
6767
}
@@ -70,13 +70,16 @@ public override void VisitNamedType(INamedTypeSymbol type)
7070
foreach (var method in methods)
7171
{
7272
_cancellationToken.ThrowIfCancellationRequested();
73-
if (IsDeclaredInAssembly(method) && _exportedMethods.Add(method))
73+
if (IsAccessibleType(method) && _exportedMethods.Add(method))
7474
{
7575
method.Accept(this);
7676
}
7777
}
7878
}
7979

80-
private bool IsDeclaredInAssembly(ISymbol symbol) =>
81-
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assemblySymbol);
80+
private bool IsAccessibleType(ISymbol symbol) =>
81+
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assemblySymbol)
82+
&& (symbol.DeclaredAccessibility == Accessibility.Public ||
83+
symbol.DeclaredAccessibility == Accessibility.Internal ||
84+
symbol.DeclaredAccessibility == Accessibility.ProtectedOrInternal);
8285
}

Diff for: src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/AdditionalTextsTests.Schemas.cs

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Globalization;
45
using System.Text.Json.Nodes;
56
using Microsoft.OpenApi.Models;
67

@@ -67,6 +68,34 @@ public class BoardItem
6768
/// </summary>
6869
public string Name { get; set; }
6970
}
71+
72+
private class Element
73+
{
74+
/// <summary>
75+
/// The unique identifier for the element.
76+
/// </summary>
77+
/// <remarks>
78+
/// This won't be emitted since it is a public
79+
/// property on a private class.
80+
/// </remarks>
81+
public string Name { get; set; }
82+
}
83+
84+
protected internal class ProtectedInternalElement
85+
{
86+
/// <summary>
87+
/// The unique identifier for the element.
88+
/// </summary>
89+
public string Name { get; set; }
90+
}
91+
92+
protected class ProtectedElement
93+
{
94+
/// <summary>
95+
/// The unique identifier for the element.
96+
/// </summary>
97+
public string Name { get; set; }
98+
}
7099
}
71100
72101
/// <summary>
@@ -201,18 +230,17 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, additionalAssemblies, docume
201230
var longTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["longType"].Example);
202231
Assert.Equal(1234567890123456789, longTypeExample.GetValue<long>());
203232

204-
// Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137
205-
// var doubleTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["doubleType"].Example);
206-
// Assert.Equal("3.14", doubleTypeExample.GetValue<string>());
233+
var doubleTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["doubleType"].Example);
234+
Assert.Equal(3.14, doubleTypeExample.GetValue<double>());
207235

208-
// var floatTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["floatType"].Example);
209-
// Assert.Equal(3.14f, floatTypeExample.GetValue<float>());
236+
var floatTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["floatType"].Example);
237+
Assert.Equal(3.14f, floatTypeExample.GetValue<float>());
210238

211-
// var dateTimeTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateTimeType"].Example);
212-
// Assert.Equal(DateTime.Parse("2022-01-01T00:00:00Z", CultureInfo.InvariantCulture), dateTimeTypeExample.GetValue<DateTime>());
239+
var dateTimeTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateTimeType"].Example);
240+
Assert.Equal(new DateTime(2022, 01, 01), dateTimeTypeExample.GetValue<DateTime>());
213241

214-
// var dateOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateOnlyType"].Example);
215-
// Assert.Equal(DateOnly.Parse("2022-01-01", CultureInfo.InvariantCulture), dateOnlyTypeExample.GetValue<DateOnly>());
242+
var dateOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateOnlyType"].Example);
243+
Assert.Equal("2022-01-01", dateOnlyTypeExample.GetValue<string>());
216244

217245
var stringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Example);
218246
Assert.Equal("Hello, World!", stringTypeExample.GetValue<string>());
@@ -223,15 +251,14 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, additionalAssemblies, docume
223251
var byteTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["byteType"].Example);
224252
Assert.Equal(255, byteTypeExample.GetValue<int>());
225253

226-
// Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137
227-
// var timeOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeOnlyType"].Example);
228-
// Assert.Equal(TimeOnly.Parse("12:30:45", CultureInfo.InvariantCulture), timeOnlyTypeExample.GetValue<TimeOnly>());
254+
var timeOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeOnlyType"].Example);
255+
Assert.Equal("12:30:45", timeOnlyTypeExample.GetValue<string>());
229256

230-
// var timeSpanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeSpanType"].Example);
231-
// Assert.Equal(TimeSpan.Parse("P3DT4H5M", CultureInfo.InvariantCulture), timeSpanTypeExample.GetValue<TimeSpan>());
257+
var timeSpanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeSpanType"].Example);
258+
Assert.Equal("P3DT4H5M", timeSpanTypeExample.GetValue<string>());
232259

233-
// var decimalTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["decimalType"].Example);
234-
// Assert.Equal(3.14159265359m, decimalTypeExample.GetValue<decimal>());
260+
var decimalTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["decimalType"].Example);
261+
Assert.Equal(3.14159265359m, decimalTypeExample.GetValue<decimal>());
235262

236263
var uriTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["uriType"].Example);
237264
Assert.Equal("https://example.com", uriTypeExample.GetValue<string>());

Diff for: src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/SchemaTests.cs

+58-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public async Task SupportsXmlCommentsOnSchemas()
2727
app.MapPost("/todo", (Todo todo) => { });
2828
app.MapPost("/project", (Project project) => { });
2929
app.MapPost("/board", (ProjectBoard.BoardItem boardItem) => { });
30+
app.MapPost("/protected-internal-element", (ProjectBoard.ProtectedInternalElement element) => { });
3031
app.MapPost("/project-record", (ProjectRecord project) => { });
3132
app.MapPost("/todo-with-description", (TodoWithDescription todo) => { });
3233
app.MapPost("/type-with-examples", (TypeWithExamples typeWithExamples) => { });
@@ -58,6 +59,43 @@ public class BoardItem
5859
{
5960
public string Name { get; set; }
6061
}
62+
63+
/// <summary>
64+
/// No XML comment processed here.
65+
/// </summary>
66+
private class Element
67+
{
68+
/// <summary>
69+
/// The unique identifier for the element.
70+
/// </summary>
71+
/// <remarks>
72+
/// This won't be emitted since it is a public
73+
/// property on a private class.
74+
/// </remarks>
75+
public string Name { get; set; }
76+
}
77+
78+
/// <summary>
79+
/// Can find this XML comment.
80+
/// </summary>
81+
protected internal class ProtectedInternalElement
82+
{
83+
/// <summary>
84+
/// The unique identifier for the element.
85+
/// </summary>
86+
public string Name { get; set; }
87+
}
88+
89+
/// <summary>
90+
/// No XML comment processed here.
91+
/// </summary>
92+
protected class ProtectedElement
93+
{
94+
/// <summary>
95+
/// The unique identifier for the element.
96+
/// </summary>
97+
public string Name { get; set; }
98+
}
6199
}
62100
63101
/// <summary>
@@ -130,7 +168,7 @@ public interface IUser
130168
}
131169
132170
/// <inheritdoc/>
133-
public class User : IUser
171+
internal class User : IUser
134172
{
135173
/// <inheritdoc/>
136174
public int Id { get; set; }
@@ -155,6 +193,11 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
155193
var board = path.RequestBody.Content["application/json"].Schema;
156194
Assert.Equal("An item on the board.", board.Description);
157195

196+
path = document.Paths["/protected-internal-element"].Operations[OperationType.Post];
197+
var element = path.RequestBody.Content["application/json"].Schema;
198+
Assert.Equal("The unique identifier for the element.", element.Properties["name"].Description);
199+
Assert.Equal("Can find this XML comment.", element.Description);
200+
158201
path = document.Paths["/project-record"].Operations[OperationType.Post];
159202
project = path.RequestBody.Content["application/json"].Schema;
160203

@@ -179,18 +222,17 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
179222
var longTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["longType"].Example);
180223
Assert.Equal(1234567890123456789, longTypeExample.GetValue<long>());
181224

182-
// Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137
183-
// var doubleTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["doubleType"].Example);
184-
// Assert.Equal("3.14", doubleTypeExample.GetValue<string>());
225+
var doubleTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["doubleType"].Example);
226+
Assert.Equal(3.14, doubleTypeExample.GetValue<double>());
185227

186-
// var floatTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["floatType"].Example);
187-
// Assert.Equal(3.14f, floatTypeExample.GetValue<float>());
228+
var floatTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["floatType"].Example);
229+
Assert.Equal(3.14f, floatTypeExample.GetValue<float>());
188230

189-
// var dateTimeTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateTimeType"].Example);
190-
// Assert.Equal(DateTime.Parse("2022-01-01T00:00:00Z", CultureInfo.InvariantCulture), dateTimeTypeExample.GetValue<DateTime>());
231+
var dateTimeTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateTimeType"].Example);
232+
Assert.Equal(new DateTime(2022, 01, 01), dateTimeTypeExample.GetValue<DateTime>());
191233

192-
// var dateOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateOnlyType"].Example);
193-
// Assert.Equal(DateOnly.Parse("2022-01-01", CultureInfo.InvariantCulture), dateOnlyTypeExample.GetValue<DateOnly>());
234+
var dateOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateOnlyType"].Example);
235+
Assert.Equal("2022-01-01", dateOnlyTypeExample.GetValue<string>());
194236

195237
var stringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Example);
196238
Assert.Equal("Hello, World!", stringTypeExample.GetValue<string>());
@@ -201,15 +243,14 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
201243
var byteTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["byteType"].Example);
202244
Assert.Equal(255, byteTypeExample.GetValue<int>());
203245

204-
// Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137
205-
// var timeOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeOnlyType"].Example);
206-
// Assert.Equal(TimeOnly.Parse("12:30:45", CultureInfo.InvariantCulture), timeOnlyTypeExample.GetValue<TimeOnly>());
246+
var timeOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeOnlyType"].Example);
247+
Assert.Equal("12:30:45", timeOnlyTypeExample.GetValue<string>());
207248

208-
// var timeSpanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeSpanType"].Example);
209-
// Assert.Equal(TimeSpan.Parse("P3DT4H5M", CultureInfo.InvariantCulture), timeSpanTypeExample.GetValue<TimeSpan>());
249+
var timeSpanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeSpanType"].Example);
250+
Assert.Equal("P3DT4H5M", timeSpanTypeExample.GetValue<string>());
210251

211-
// var decimalTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["decimalType"].Example);
212-
// Assert.Equal(3.14159265359m, decimalTypeExample.GetValue<decimal>());
252+
var decimalTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["decimalType"].Example);
253+
Assert.Equal(3.14159265359m, decimalTypeExample.GetValue<decimal>());
213254

214255
var uriTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["uriType"].Example);
215256
Assert.Equal("https://example.com", uriTypeExample.GetValue<string>());

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

+2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ private static Dictionary<MemberKey, XmlComment> GenerateCacheEntries()
189189
_cache.Add(new MemberKey(typeof(global::Todo), MemberType.Type, null, null, []), new XmlComment(@"This is a todo item.", null, null, null, null, false, null, null, null));
190190
_cache.Add(new MemberKey(typeof(global::Project), MemberType.Type, null, null, []), new XmlComment(@"The project that contains Todo items.", null, null, null, null, false, null, null, null));
191191
_cache.Add(new MemberKey(typeof(global::ProjectBoard.BoardItem), MemberType.Type, null, null, []), new XmlComment(@"An item on the board.", null, null, null, null, false, null, null, null));
192+
_cache.Add(new MemberKey(typeof(global::ProjectBoard.ProtectedInternalElement), MemberType.Type, null, null, []), new XmlComment(@"Can find this XML comment.", null, null, null, null, false, null, null, null));
192193
_cache.Add(new MemberKey(typeof(global::ProjectRecord), MemberType.Type, null, null, []), new XmlComment(@"The project that contains Todo items.", null, null, null, null, false, null, [new XmlParameterComment(@"Name", @"The name of the project.", null, false), new XmlParameterComment(@"Description", @"The description of the project.", null, false)], null));
193194
_cache.Add(new MemberKey(typeof(global::User), MemberType.Type, null, null, []), new XmlComment(null, null, null, null, null, false, null, null, null));
195+
_cache.Add(new MemberKey(typeof(global::ProjectBoard.ProtectedInternalElement), MemberType.Property, "Name", null, []), new XmlComment(@"The unique identifier for the element.", null, null, null, null, false, null, null, null));
194196
_cache.Add(new MemberKey(typeof(global::ProjectRecord), MemberType.Property, "Name", null, []), new XmlComment(@"The name of the project.", null, null, null, null, false, null, null, null));
195197
_cache.Add(new MemberKey(typeof(global::ProjectRecord), MemberType.Property, "Description", null, []), new XmlComment(@"The description of the project.", null, null, null, null, false, null, null, null));
196198
_cache.Add(new MemberKey(typeof(global::TodoWithDescription), MemberType.Property, "Id", null, []), new XmlComment(@"The identifier of the todo.", null, null, null, null, false, null, null, null));

0 commit comments

Comments
 (0)