Skip to content
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

Fix OpenAPI XML code gen #60977

Merged
merged 2 commits into from
Mar 19, 2025
Merged

Fix OpenAPI XML code gen #60977

merged 2 commits into from
Mar 19, 2025

Conversation

captainsafia
Copy link
Member

Fixes some of the issues in #60783.

@captainsafia captainsafia requested a review from a team as a code owner March 17, 2025 20:10
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 17, 2025
@@ -189,4 +189,26 @@ public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol? type)
current = current.BaseType;
}
}

public static bool IsAccessibleType(this ISymbol symbol)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps us avoid generating XML comments for non-public types and internal properties in non-public types.

Note: this does mean that DTOs have to be defined as public types if they are defined in the same assembly as the API (see where this is called in the AssemnlyTypeSymbolVisitor above). We can consider changing this behavior to allow XML comments on internal types in the application assembly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit overaggressive. I'm guessing we'll change this behavior a bit in the future.

Any reason to disallow the type entirely just because it has internal types?

@@ -27,6 +27,9 @@ internal static string GenerateXmlCommentSupportSource(string commentsFromXmlFil
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
// Suppress warnings about obsolete types and members
// in generated code
#pragma warning disable CS0612, CS0618
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppress obsoletion warnings in generated code. If someone happens to be using an obsolete type in their API, we don't want to resurface this warning in generated code where we call typeof(ObsolteType).

{
var parameter = comment.Parameters[i];
var exampleLiteral = string.IsNullOrEmpty(parameter.Example)
? "null"
: FormatStringForCode(parameter.Example!);
codeWriter.Write($"new XmlParameterComment(@\"{parameter.Name}\", @\"{parameter.Description}\", {exampleLiteral}, {(parameter.Deprecated == true ? "true" : "false")})");
codeWriter.Write("new XmlParameterComment(");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below we format strings in the emitted code so that we can properly escape quoted strings inside XML descriptions/summaries/etc.

@@ -46,7 +46,7 @@ public static MemberKey FromMethodSymbol(IMethodSymbol method, Compilation compi

returnType = actualReturnType.TypeKind == TypeKind.TypeParameter
? "typeof(object)"
: $"typeof({actualReturnType.ToDisplayString(_typeKeyFormat)})";
: $"typeof({ReplaceGenericArguments(actualReturnType.ToDisplayString(_typeKeyFormat))})";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure that all generics are emitted as open generics for method declarations. We already do this elsewhere in the MemberKey for the declaring type and the property type but not for methods.

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

public class DeeplyNestedLevel1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we require all types to be public for discovery, we moved the shared types that are used by tests and the sample app to the sample app since the tests reference the sample app.

Previously, we used compile includes with internal types which doesn't work in this new model.

@captainsafia captainsafia force-pushed the cs/openapi-xml-bugfix branch from d231073 to 196bd95 Compare March 17, 2025 20:19
@@ -118,10 +118,12 @@
"type": "object",
"properties": {
"title": {
"type": "string"
"type": "string",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STJ's schema generation assumes strings are nullable in an oblivious nullability context (see disable in Sample.csproj), hence the delta here.

Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really understand all this but everything that I could understand looks fine! 👍

@@ -189,4 +189,26 @@ public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol? type)
current = current.BaseType;
}
}

public static bool IsAccessibleType(this ISymbol symbol)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit overaggressive. I'm guessing we'll change this behavior a bit in the future.

Any reason to disallow the type entirely just because it has internal types?

@captainsafia
Copy link
Member Author

Any reason to disallow the type entirely just because it has internal types?

I assume you mean internal properties? This will omit public properties in internal types.

We can relax the rule a little bit and still discover XML comments on internal types on the application assembly but prohibit them on internal types in referenced files.

This would strike a balance between not discovering internal types in assemblies we don't have access to while still permitting them in the application assembly.

@captainsafia captainsafia merged commit 06f3c41 into main Mar 19, 2025
27 checks passed
@captainsafia captainsafia deleted the cs/openapi-xml-bugfix branch March 19, 2025 02:42
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview3 milestone Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants