Skip to content

csharp-opensource/CSharpExtensions-Swagger

Repository files navigation

Swagger Generator C#

NuGet Version

this nuget base on NSwag

NuGet Version

How to use (Startup.cs)


using SwaggerGenerator;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        .
        .
        .
        services.AddAutoGeneratedSwagger();
        .
        .
        .
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        .
        .
        .
        app.UseAutoGeneratedSwagger();
        .
        .
        .
    }
}

Example of attributes


using SwaggerGenerator;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

public class AttributesExample
{
    [JsonProperty(Order = 101)] public string LastProp { get; set; }
    [Description("my desc")] public string HaveDescription { get; set; }
    [Display(Name = "display name")] public string HaveTitle { get; set; }
    [Obsolete] public bool? Deprecated { get; set; }
    public bool HaveDefaultValue { get; set; } = true;
    [RegularExpression("https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\\+.~#?&\\/\\/=]*)")] public string HavePattern { get; set; }
    [StringLength(100, MinimumLength = 10)] public string HaveLength { get; set; }
    [MinLength(10), MaxLength(100)] public string HaveLength2 { get; set; }
    [Range(10, 100)] public int HaveRange { get; set; }
    [MinLength(2), MaxLength(100)] public List<string> ListMinMaxItems { get; set; }
    [SwaggerUniqueItems] public List<string> UniqueList { get; set; }
    // one of those attributes
    [BsonIgnore,JsonIgnore,ForeignKey,InverseProperty,SwaggerExclude] public object HideProp { get; set; }
    // only DogType will be in the swagger
    [SwaggerIgnoreInheritProps] public Dog DogWithoutInheritProps { get; set; }
}

public class Animal
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Dog : Animal
{
    public string DogType { get; set; }
}