Skip to content

Unify handling of documentation IDs in OpenAPI XML comment generator #62692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix runtime lookup to use normalized documentation IDs
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
  • Loading branch information
Copilot and captainsafia committed Jul 12, 2025
commit 2d427913d804a249a2bd5380c0fc18751a505caa
33 changes: 30 additions & 3 deletions src/OpenApi/gen/XmlCommentGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,33 @@ private static string GetTypeDocId(Type type, bool includeGenericArguments, bool
// For non-generic types, use FullName (if available) and replace nested type separators.
return (type.FullName ?? type.Name).Replace('+', '.');
}

/// <summary>
/// Normalizes a documentation comment ID to match the compiler-style format.
/// Strips the return type suffix for ordinary methods but retains it for conversion operators.
/// </summary>
/// <param name="docId">The documentation comment ID to normalize.</param>
/// <returns>The normalized documentation comment ID.</returns>
public static string NormalizeDocId(string docId)
{
// Find the tilde character that indicates the return type suffix
var tildeIndex = docId.IndexOf('~');
if (tildeIndex == -1)
{
// No return type suffix, return as-is
return docId;
}

// Check if this is a conversion operator (op_Implicit or op_Explicit)
// For these operators, we need to keep the return type suffix
if (docId.Contains("op_Implicit") || docId.Contains("op_Explicit"))
{
return docId;
}

// For ordinary methods, strip the return type suffix
return docId.Substring(0, tildeIndex);
}
}

{{GeneratedCodeAttribute}}
Expand All @@ -317,7 +344,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
{
return Task.CompletedTask;
}
if (XmlCommentCache.Cache.TryGetValue(methodInfo.CreateDocumentationId(), out var methodComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(methodInfo.CreateDocumentationId()), out var methodComment))
{
if (methodComment.Summary is { } summary)
{
Expand Down Expand Up @@ -423,7 +450,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
{
if (context.JsonPropertyInfo is { AttributeProvider: PropertyInfo propertyInfo })
{
if (XmlCommentCache.Cache.TryGetValue(propertyInfo.CreateDocumentationId(), out var propertyComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(propertyInfo.CreateDocumentationId()), out var propertyComment))
{
schema.Description = propertyComment.Value ?? propertyComment.Returns ?? propertyComment.Summary;
if (propertyComment.Examples?.FirstOrDefault() is { } jsonString)
Expand All @@ -432,7 +459,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
}
}
}
if (XmlCommentCache.Cache.TryGetValue(context.JsonTypeInfo.Type.CreateDocumentationId(), out var typeComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples?.FirstOrDefault() is { } jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,33 @@ private static string GetTypeDocId(Type type, bool includeGenericArguments, bool
// For non-generic types, use FullName (if available) and replace nested type separators.
return (type.FullName ?? type.Name).Replace('+', '.');
}

/// <summary>
/// Normalizes a documentation comment ID to match the compiler-style format.
/// Strips the return type suffix for ordinary methods but retains it for conversion operators.
/// </summary>
/// <param name="docId">The documentation comment ID to normalize.</param>
/// <returns>The normalized documentation comment ID.</returns>
public static string NormalizeDocId(string docId)
{
// Find the tilde character that indicates the return type suffix
var tildeIndex = docId.IndexOf('~');
if (tildeIndex == -1)
{
// No return type suffix, return as-is
return docId;
}

// Check if this is a conversion operator (op_Implicit or op_Explicit)
// For these operators, we need to keep the return type suffix
if (docId.Contains("op_Implicit") || docId.Contains("op_Explicit"))
{
return docId;
}

// For ordinary methods, strip the return type suffix
return docId.Substring(0, tildeIndex);
}
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.AspNetCore.OpenApi.SourceGenerators, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")]
Expand All @@ -299,7 +326,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
{
return Task.CompletedTask;
}
if (XmlCommentCache.Cache.TryGetValue(methodInfo.CreateDocumentationId(), out var methodComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(methodInfo.CreateDocumentationId()), out var methodComment))
{
if (methodComment.Summary is { } summary)
{
Expand Down Expand Up @@ -405,7 +432,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
{
if (context.JsonPropertyInfo is { AttributeProvider: PropertyInfo propertyInfo })
{
if (XmlCommentCache.Cache.TryGetValue(propertyInfo.CreateDocumentationId(), out var propertyComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(propertyInfo.CreateDocumentationId()), out var propertyComment))
{
schema.Description = propertyComment.Value ?? propertyComment.Returns ?? propertyComment.Summary;
if (propertyComment.Examples?.FirstOrDefault() is { } jsonString)
Expand All @@ -414,7 +441,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
}
}
}
if (XmlCommentCache.Cache.TryGetValue(context.JsonTypeInfo.Type.CreateDocumentationId(), out var typeComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples?.FirstOrDefault() is { } jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,33 @@ private static string GetTypeDocId(Type type, bool includeGenericArguments, bool
// For non-generic types, use FullName (if available) and replace nested type separators.
return (type.FullName ?? type.Name).Replace('+', '.');
}

/// <summary>
/// Normalizes a documentation comment ID to match the compiler-style format.
/// Strips the return type suffix for ordinary methods but retains it for conversion operators.
/// </summary>
/// <param name="docId">The documentation comment ID to normalize.</param>
/// <returns>The normalized documentation comment ID.</returns>
public static string NormalizeDocId(string docId)
{
// Find the tilde character that indicates the return type suffix
var tildeIndex = docId.IndexOf('~');
if (tildeIndex == -1)
{
// No return type suffix, return as-is
return docId;
}

// Check if this is a conversion operator (op_Implicit or op_Explicit)
// For these operators, we need to keep the return type suffix
if (docId.Contains("op_Implicit") || docId.Contains("op_Explicit"))
{
return docId;
}

// For ordinary methods, strip the return type suffix
return docId.Substring(0, tildeIndex);
}
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.AspNetCore.OpenApi.SourceGenerators, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")]
Expand All @@ -328,7 +355,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
{
return Task.CompletedTask;
}
if (XmlCommentCache.Cache.TryGetValue(methodInfo.CreateDocumentationId(), out var methodComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(methodInfo.CreateDocumentationId()), out var methodComment))
{
if (methodComment.Summary is { } summary)
{
Expand Down Expand Up @@ -434,7 +461,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
{
if (context.JsonPropertyInfo is { AttributeProvider: PropertyInfo propertyInfo })
{
if (XmlCommentCache.Cache.TryGetValue(propertyInfo.CreateDocumentationId(), out var propertyComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(propertyInfo.CreateDocumentationId()), out var propertyComment))
{
schema.Description = propertyComment.Value ?? propertyComment.Returns ?? propertyComment.Summary;
if (propertyComment.Examples?.FirstOrDefault() is { } jsonString)
Expand All @@ -443,7 +470,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
}
}
}
if (XmlCommentCache.Cache.TryGetValue(context.JsonTypeInfo.Type.CreateDocumentationId(), out var typeComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples?.FirstOrDefault() is { } jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,33 @@ private static string GetTypeDocId(Type type, bool includeGenericArguments, bool
// For non-generic types, use FullName (if available) and replace nested type separators.
return (type.FullName ?? type.Name).Replace('+', '.');
}

/// <summary>
/// Normalizes a documentation comment ID to match the compiler-style format.
/// Strips the return type suffix for ordinary methods but retains it for conversion operators.
/// </summary>
/// <param name="docId">The documentation comment ID to normalize.</param>
/// <returns>The normalized documentation comment ID.</returns>
public static string NormalizeDocId(string docId)
{
// Find the tilde character that indicates the return type suffix
var tildeIndex = docId.IndexOf('~');
if (tildeIndex == -1)
{
// No return type suffix, return as-is
return docId;
}

// Check if this is a conversion operator (op_Implicit or op_Explicit)
// For these operators, we need to keep the return type suffix
if (docId.Contains("op_Implicit") || docId.Contains("op_Explicit"))
{
return docId;
}

// For ordinary methods, strip the return type suffix
return docId.Substring(0, tildeIndex);
}
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.AspNetCore.OpenApi.SourceGenerators, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")]
Expand All @@ -420,7 +447,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
{
return Task.CompletedTask;
}
if (XmlCommentCache.Cache.TryGetValue(methodInfo.CreateDocumentationId(), out var methodComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(methodInfo.CreateDocumentationId()), out var methodComment))
{
if (methodComment.Summary is { } summary)
{
Expand Down Expand Up @@ -526,7 +553,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
{
if (context.JsonPropertyInfo is { AttributeProvider: PropertyInfo propertyInfo })
{
if (XmlCommentCache.Cache.TryGetValue(propertyInfo.CreateDocumentationId(), out var propertyComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(propertyInfo.CreateDocumentationId()), out var propertyComment))
{
schema.Description = propertyComment.Value ?? propertyComment.Returns ?? propertyComment.Summary;
if (propertyComment.Examples?.FirstOrDefault() is { } jsonString)
Expand All @@ -535,7 +562,7 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
}
}
}
if (XmlCommentCache.Cache.TryGetValue(context.JsonTypeInfo.Type.CreateDocumentationId(), out var typeComment))
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples?.FirstOrDefault() is { } jsonString)
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy