abp icon indicating copy to clipboard operation
abp copied to clipboard

Adding endpoint descriptions in swagger for services from separate modules

Open dkaczor97 opened this issue 4 years ago • 5 comments

  • ABP Framework version. 5.0.1

I am trying to create a better documentation for my API by adding a ///summary for every public method. I want it to be visible in my swagger. I already know that I cannot add summary in AppServices, I have to create Controllers and add summaries there. I also configured the project following this document. What should I do to see summaries of methods defined in separate modules? When I added Controllers in .HttpApi projects their descriptions were not visible in swagger. Is this even possible to achieve that?

dkaczor97 avatar Jan 05 '22 15:01 dkaczor97

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 09 '22 02:03 stale[bot]

bump? am also interested in enabling SwaggerUI description from xmldoc comments

there seems to be no extension point to enable xmldocs as described in the Microsoft docs...

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
       ....
        app.UseSwagger();
        app.UseAbpSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/v1/swagger.json", "Blox API");

            var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
            options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
            options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);

            options.DocExpansion(DocExpansion.None);
        });
       ....
}

sanghel-payoff avatar Mar 10 '22 14:03 sanghel-payoff

Never mind, I've found that SwaggerGen is already setup in the ...HttpApiHostModule.ConfigureSwagger method..

Still, I've tried adding the xml doc files, as described below

private static void ConfigureSwagger(ServiceConfigurationContext context, IConfiguration configuration)
{
    context.Services.AddAbpSwaggerGenWithOAuth(
        configuration["AuthServer:Authority"],
        new Dictionary<string, string>
        {
            {"Project", "Project API"}
        },
        options =>
        {
            //options.SwaggerDoc("v1", new OpenApiInfo {Title = "Project API", Version = "v1"});
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);

            options.SwaggerDoc("v1", new OpenApiInfo {
                Title = "Project API",
                Version = "v1",
                TermsOfService = null,
                Description = null,
                License = new OpenApiLicense { Name = "Proprietary" }
            });
            IncludeXmlDocFiles(options, $"Project.Namespace.*.xml");
        });
}

private static void IncludeXmlDocFiles(SwaggerGenOptions options, string pattern)
{
    string WildcardToRegex(string wildcardPattern)
    {
        return $"^{Regex.Escape(wildcardPattern).Replace("\\*", ".*").Replace("\\?", ".")}$";
    }

    //Find XML Comments File In Pattern
    var basePath = AppContext.BaseDirectory;
    var regex = new Regex(WildcardToRegex(pattern));

    var allDirectoryFiles = Directory.GetFiles(basePath, "*.*", SearchOption.TopDirectoryOnly).ToList();

    var xmlDocFiles = allDirectoryFiles.Where(p => regex.IsMatch(Path.GetFileName(p))).ToList();
    foreach (var xmlDocFilePath in xmlDocFiles)
    {
        options.IncludeXmlComments(xmlDocFilePath, true);
    }
}

Xml files are generated, found and included via IncludeXmlComments, but still, APIService Actions that do have comments (visible in the xml files also) DO NOT show up on the Swagger UI...

What am I missing?

@maliming any ideas ?

sanghel-payoff avatar Mar 10 '22 16:03 sanghel-payoff

but still, APIService Actions that do have comments (visible in the xml files also) DO NOT show up on the Swagger UI...

Can you share a simple project?

maliming avatar Mar 11 '22 03:03 maliming

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 12 '22 09:06 stale[bot]