'Swashbuckle - Add Model and Example values to Swagger UI from a Model from another project

I am using Swagger to document my .NET C# API and when my models are on another project Swagger just crashes and doesn't load anything.

When I load the sample WebAPI Project from Visual Studio it uses the models that are on the same project and it works:

Visual Studio Sample Project Example Image

But when I use Models from other project it just crashes before loading anything.

I have an API project and a Business Project. My models are View Models stored(and shared among other projects, therfore needed there) on my Buisiness Project.

Is there any way I can indicate to Swagger where my model definitions are?



Solution 1:[1]

I have recently handled a similar scenario when I worked on a web API project which was hosted on IIS. I was required to enable xml documentations for all view models and models from two different projects.

Below is a summary of the main steps of the work:

  1. Enable XML documentation for related projects (refer to here)
  2. For each project, build it first and then include the xml file in the project. Set the property of the file "Copy to Output Directory" to be "Copy is newer" to ensure it is copied to the bin folder of the server.
  3. In the Swagger config, invoke IncludeXmlComments() to include the XML documentation files as suggested by Simon88.

The key point is to ensure all xml documentation files are copied to the bin folder of the host server and Swagger UI knows where they are :-).

By the way, there are few similar questions/answers for this kind of issue. One is here

Hope it helps.

Solution 2:[2]

I faced same issue with Swashbuckle 5.6.0 and managed to fixed as follows. Enable XML documentation file on model objects containing project by changing project properties as follows.

enter image description here

Then go to SwaggerConfig.cs file of the api project and add xml comments file of model object project as follows.

c.IncludeXmlComments(string.Format(@"{0}\bin\WebApplication1.XML", System.AppDomain.CurrentDomain.BaseDirectory));
// New code line                            
c.IncludeXmlComments(string.Format(@"{0}\bin\core.XML", System.AppDomain.CurrentDomain.BaseDirectory));

Now you will have xml comments for model properties on swagger doc.

Solution 3:[3]

I am actually looking to do the same thing!

In the Swagger config i tried this, where the functions point to my XML documentation files.

c.IncludeXmlComments(GetXmlCommentsPathForControllers());
c.IncludeXmlComments(GetXmlCommentsPathForModels());

It's working fine for the controllers but not much is happening when i document the models.

Solution 4:[4]

  1. Source
    1. Models
    2. Web App

If the source of Web application and Model are in different projects, you can always generate the metadata xml and copy them as part of the build into a common location inside the web application.

So in this case the edit the project properties of Models to output xml to "..\WebApp\bin"

For generating dynamic examples, i would recommend you go through this link.

Solution 5:[5]

Step 1: Update reference project .csproj file and add DocumentationFile

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <GenerateDocumentationFile>True</GenerateDocumentationFile>
        <DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildThisFileName).xml</DocumentationFile>
    </PropertyGroup>

This will cause the build of the project to output XML document along with the assembly of compiled project inside the bin folder.

API/Web project can access all reference assembly with a reflection by calling Assembly.GetReferencedAssemblies() method on the currently executing assembly.

Step 2: Update swagger configuration

builder.Services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.ToString());

    var currentAssembly = Assembly.GetExecutingAssembly();
    var xmlDocs = currentAssembly.GetReferencedAssemblies()
        .Union(new AssemblyName[] { currentAssembly.GetName() })
        .Select(a => Path.Combine(Path.GetDirectoryName(currentAssembly.Location), $"{a.Name}.xml"))
        .Where(f => File.Exists(f)).ToArray();

    Array.ForEach(xmlDocs, (d) =>
    {
        options.IncludeXmlComments(d);
    });

});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Janith Widarshana
Solution 3 Simon88
Solution 4 Mridul
Solution 5 Niraj Trivedi