Skip to content

Commit 8861cc5

Browse files
Handle langword for see elements
Handle `<see langword="" />` references in XML documentation as inline code. Resolves #61042.
1 parent c5d6996 commit 8861cc5

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

src/OpenApi/gen/XmlComments/XmlComment.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ private XmlComment(Compilation compilation, string xml)
3535
// Transform triple slash comment
3636
var doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
3737

38-
ResolveCrefLink(compilation, doc, $"//{DocumentationCommentXmlNames.SeeAlsoElementName}[@cref]");
39-
ResolveCrefLink(compilation, doc, $"//{DocumentationCommentXmlNames.SeeElementName}[@cref]");
38+
ResolveCrefLink(compilation, doc, DocumentationCommentXmlNames.SeeAlsoElementName);
39+
ResolveCrefLink(compilation, doc, DocumentationCommentXmlNames.SeeElementName);
40+
41+
ResolveLangKeyword(doc, DocumentationCommentXmlNames.SeeElementName);
42+
4043
// Resolve <list> and <item> tags into bullets
4144
ResolveListTags(doc);
4245
// Resolve <code> tags into code blocks
@@ -171,18 +174,20 @@ private static void ResolveParaTags(XDocument document)
171174
/// </summary>
172175
/// <param name="compilation">The compilation to resolve type symbol declarations from.</param>
173176
/// <param name="node">The target node to process crefs in.</param>
174-
/// <param name="nodeSelector">The node type to process crefs for, can be `see` or `seealso`.</param>
175-
private static void ResolveCrefLink(Compilation compilation, XNode node, string nodeSelector)
177+
/// <param name="elementName">The node type to process crefs for, can be `see` or `seealso`.</param>
178+
private static void ResolveCrefLink(Compilation compilation, XNode node, string elementName)
176179
{
177-
if (node == null || string.IsNullOrEmpty(nodeSelector))
180+
if (node == null || string.IsNullOrEmpty(elementName))
178181
{
179182
return;
180183
}
181184

182-
var nodes = node.XPathSelectElements(nodeSelector + "[@cref]").ToArray();
185+
var attributeName = DocumentationCommentXmlNames.CrefAttributeName;
186+
187+
var nodes = node.XPathSelectElements($"//{elementName}[@{attributeName}]").ToArray();
183188
foreach (var item in nodes)
184189
{
185-
var cref = item.Attribute(DocumentationCommentXmlNames.CrefAttributeName).Value;
190+
var cref = item.Attribute(attributeName).Value;
186191
if (string.IsNullOrEmpty(cref))
187192
{
188193
continue;
@@ -197,6 +202,33 @@ private static void ResolveCrefLink(Compilation compilation, XNode node, string
197202
}
198203
}
199204

205+
/// <summary>
206+
/// Resolves the links in the XML documentation into type names.
207+
/// </summary>
208+
/// <param name="node">The target node to process crefs in.</param>
209+
/// <param name="elementName">The node type to process crefs for, can be `see` or `seealso`.</param>
210+
private static void ResolveLangKeyword(XNode node, string elementName)
211+
{
212+
if (node == null || string.IsNullOrEmpty(elementName))
213+
{
214+
return;
215+
}
216+
217+
var attributeName = DocumentationCommentXmlNames.LangwordAttributeName;
218+
219+
var nodes = node.XPathSelectElements($"//{elementName}[@{attributeName}]").ToArray();
220+
foreach (var item in nodes)
221+
{
222+
var langword = item.Attribute(attributeName).Value;
223+
if (string.IsNullOrEmpty(langword))
224+
{
225+
continue;
226+
}
227+
228+
item.ReplaceWith(new XText($"`{langword}`"));
229+
}
230+
}
231+
200232
private static IEnumerable<string?> GetMultipleExampleNodes(XPathNavigator navigator, string selector)
201233
{
202234
var iterator = navigator.Select(selector);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/CompletenessTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Net.Http;
5+
using Microsoft.OpenApi.Models;
56

67
namespace Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests;
78

@@ -437,6 +438,39 @@ public static T GetGenericValue<T>(T para)
437438
return para;
438439
}
439440
}
441+
442+
/// <summary>
443+
/// A class that implements the <see cref="IDisposable"/> interface.
444+
/// </summary>
445+
public class DisposableType : IDisposable
446+
{
447+
/// <summary>
448+
/// Finalizes an instance of the <see cref="DisposableType"/> class.
449+
/// </summary>
450+
~DisposableType()
451+
{
452+
Dispose(false);
453+
}
454+
455+
/// <inheritdoc />
456+
public void Dispose()
457+
{
458+
Dispose(true);
459+
GC.SuppressFinalize(this);
460+
}
461+
462+
/// <summary>
463+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
464+
/// </summary>
465+
/// <param name="disposing">
466+
/// <see langword="true" /> to release both managed and unmanaged resources;
467+
/// <see langword="false" /> to release only unmanaged resources.
468+
/// </param>
469+
protected virtual void Dispose(bool disposing)
470+
{
471+
// No-op
472+
}
473+
}
440474
""";
441475
var generator = new XmlCommentGenerator();
442476
await SnapshotTestHelper.Verify(source, generator, out var compilation);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/CompletenessTests.SupportsAllXmlTagsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ type as a cref attribute.
148148
generic types to open generics for use in
149149
typeof expressions.", null, null, null, null, false, null, null, null));
150150
cache.Add(@"T:ParamsAndParamRefs", new XmlComment(@"This shows examples of typeparamref and typeparam tags", null, null, null, null, false, null, null, null));
151-
cache.Add(@"P:ExampleClass.Label", new XmlComment(null, null, @" The string? ExampleClass.Label is a <see langword=""string"" />
151+
cache.Add(@"T:DisposableType", new XmlComment(@"A class that implements the IDisposable interface.", null, null, null, null, false, null, null, null));
152+
cache.Add(@"P:ExampleClass.Label", new XmlComment(null, null, @" The string? ExampleClass.Label is a `string`
152153
that you use for a label.
153154
Note that there isn't a way to provide a ""cref"" to
154155
each accessor, only to the property itself.", null, @"The `Label` property represents a label
@@ -191,6 +192,7 @@ that implement this interface when the
191192
method as a cref attribute.
192193
The parameter and return value are both of an arbitrary type,
193194
T", null, null, false, null, null, null));
195+
cache.Add(@"M:DisposableType.Dispose", new XmlComment(null, null, null, null, null, false, null, null, null));
194196

195197
return cache;
196198
}

0 commit comments

Comments
 (0)