CapitalSix.AspNetCore.Swagger.Xml 1.0.4

dotnet add package CapitalSix.AspNetCore.Swagger.Xml --version 1.0.4
NuGet\Install-Package CapitalSix.AspNetCore.Swagger.Xml -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CapitalSix.AspNetCore.Swagger.Xml" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CapitalSix.AspNetCore.Swagger.Xml --version 1.0.4
#r "nuget: CapitalSix.AspNetCore.Swagger.Xml, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CapitalSix.AspNetCore.Swagger.Xml as a Cake Addin
#addin nuget:?package=CapitalSix.AspNetCore.Swagger.Xml&version=1.0.4

// Install CapitalSix.AspNetCore.Swagger.Xml as a Cake Tool
#tool nuget:?package=CapitalSix.AspNetCore.Swagger.Xml&version=1.0.4

CapitalSix.AspNetCore.Swagger.Xml

This module corrects the XML handling in Swagger in AspNetCore. The default Swagger implementation does not handle XML annotation attributes correctly. This module handles XmlRoot, XmlElement and XmlIgnore attributes in data models.

The module should be used when endpoints consumes an XML or produces an XML.

How to configure the module.

void ConfigureServices(IServicesCollection services)
{
    ...
    services.AddSwagger();

    // or use the following if you need additional customization

    services.AddSwaggerGen(c =>
        {
            // Add the response filter. This filter automatically adds
            // 400 and 500 response definition.
            c.OperationFilter<DefaultOperationResponseFilter>();

            // Add the Xml schema filter to handle all Xml annotation
            // attributes
            c.SchemaFilter<XmlSchemaFilter>();
            
            c.CustomSchemaIds(currentClass =>
            {
                // This custom schema respects the use of the XmlRootAttribute
                // for the schema's in Swagger. If a model uses the
                // XmlRootAttribute, it's name will be represented in the OpenAPI.
                var rootAttribute = currentClass.GetCustomAttributes()
                    .OfType<XmlRootAttribute>()
                    .SingleOrDefault();
                var className = rootAttribute?.ElementName ?? currentClass.Name;
                return className;
            });

            // The name of the startup assembly
            var assemblyName = Assembly.GetEntryAssembly()?.GetName()?.Name;
            
            // The path to the xml documentation which is generated during the build
            var filePath = Path.Combine(AppContext.BaseDirectory, $"{assemblyName}.xml");
            c.IncludeXmlComments(filePath);
        });
    ...
}

Sample model

[XmlRoot("SampleModel")] // Remove the Dto suffix
public class SampleModelDto
{
    [XmlIgnore]
    public int? Id { get; set; }

    [XmlElement("Id")]
    [SwaggerXmlSchemaType(typeof(int))]
    public string? IdAsString
    {
        get => Id.ToString();
        set => Id = int.TryParse(value, out var intValue) ? intValue : default(int);
    }

    public string? Name { get; set; }

    [XmlElement("Extra")]
    public string? Description { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 1,796 6/28/2022
1.0.3 1,338 4/22/2022
1.0.2 372 4/22/2022
1.0.1 395 4/20/2022
1.0.0 392 4/20/2022