Skip to content

Commit 3ea5a05

Browse files
committed
Add detailed Swagger configuration and options
Updated `appsettings.json` to include a comprehensive "swagger" section with configuration options such as `enabled`, `title`, `description`, `version`, `termsOfService`, `contactName`, `contactEmail`, `contactUrl`, `licenseName`, `licenseUrl`, `includeSecurity`, and a list of `servers`. Extended `SwaggerOptions` class to include new properties: `Description`, `ContactName`, `ContactEmail`, `ContactUrl`, `LicenseName`, `LicenseUrl`, `TermsOfService`, and a list of `OpenApiServer` objects. Added an inner class `OpenApiServer` to represent server details. Modified `AddSwaggerDocs` method in `Extensions.cs` to handle the new `SwaggerOptions` properties, including logic for security definitions, custom operation IDs, and server details. Added XML comments to the Swagger documentation. Provided specific values for the "swagger" section in `appsettings.json` for different environments (local development, containerized, and production).
1 parent 21f2798 commit 3ea5a05

File tree

4 files changed

+154
-14
lines changed

4 files changed

+154
-14
lines changed

src/Genocs.Core.Demo.WebApi/appsettings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,17 @@
7070
"caller": "<<>>",
7171
"public": "<<>>",
7272
"private": "<<>>"
73+
},
74+
"swagger": {
75+
"enabled": true,
76+
"title": "Demo WebApi",
77+
"description": "Demo WebApi",
78+
"version": "v1",
79+
"servers": [
80+
{
81+
"name": "Genocs",
82+
"email": "[email protected]"
83+
}
84+
]
7385
}
7486
}

src/Genocs.WebApi.Swagger/Docs/Configurations/SwaggerOptions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ public class SwaggerOptions
77
public string? Name { get; set; }
88
public string? Title { get; set; }
99
public string? Version { get; set; }
10+
public string? Description { get; set; }
1011
public string? RoutePrefix { get; set; }
12+
public string? ContactName { get; set; }
13+
public string? ContactEmail { get; set; }
14+
public string? ContactUrl { get; set; }
15+
public string? LicenseName { get; set; }
16+
public string? LicenseUrl { get; set; }
17+
public string? TermsOfService { get; set; }
1118
public bool IncludeSecurity { get; set; }
1219
public bool SerializeAsOpenApiV2 { get; set; }
13-
}
20+
public List<OpenApiServer>? Servers { get; set; }
21+
22+
public class OpenApiServer
23+
{
24+
25+
public string? Url { get; set; }
26+
public string? Description { get; set; }
27+
}
28+
}
29+

src/Genocs.WebApi.Swagger/Docs/Extensions.cs

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Reflection;
12
using Genocs.Core.Builders;
23
using Genocs.WebApi.Swagger.Docs.Builders;
34
using Genocs.WebApi.Swagger.Docs.Configurations;
45
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Mvc.Controllers;
57
using Microsoft.Extensions.DependencyInjection;
68
using Microsoft.OpenApi.Models;
79

@@ -19,38 +21,126 @@ public static IGenocsBuilder AddSwaggerDocs(this IGenocsBuilder builder, string
1921
sectionName = SectionName;
2022
}
2123

22-
var options = builder.GetOptions<SwaggerOptions>(sectionName);
23-
return builder.AddSwaggerDocs(options);
24+
SwaggerOptions settings = builder.GetOptions<SwaggerOptions>(sectionName);
25+
26+
if (settings is null)
27+
{
28+
return builder;
29+
}
30+
31+
return builder.AddSwaggerDocs(settings);
2432
}
2533

2634
public static IGenocsBuilder AddSwaggerDocs(this IGenocsBuilder builder, Func<ISwaggerOptionsBuilder, ISwaggerOptionsBuilder> buildOptions)
2735
{
28-
var options = buildOptions(new SwaggerOptionsBuilder()).Build();
29-
return builder.AddSwaggerDocs(options);
36+
SwaggerOptions settings = buildOptions(new SwaggerOptionsBuilder()).Build();
37+
38+
if (settings is null)
39+
{
40+
return builder;
41+
}
42+
43+
return builder.AddSwaggerDocs(settings);
3044
}
3145

32-
public static IGenocsBuilder AddSwaggerDocs(this IGenocsBuilder builder, SwaggerOptions options)
46+
public static IGenocsBuilder AddSwaggerDocs(this IGenocsBuilder builder, SwaggerOptions settings)
3347
{
34-
if (!options.Enabled || !builder.TryRegister(RegistryName))
48+
if (!settings.Enabled || !builder.TryRegister(RegistryName))
3549
{
3650
return builder;
3751
}
3852

39-
builder.Services.AddSingleton(options);
53+
// TODO: Double-check if this is necessary
54+
builder.Services.AddSingleton(settings);
55+
56+
// Register the Swagger generator, defining 1 or more Swagger documents
4057
builder.Services.AddSwaggerGen(c =>
4158
{
4259
c.EnableAnnotations();
43-
c.SwaggerDoc(options.Name, new OpenApiInfo { Title = options.Title, Version = options.Version });
44-
if (options.IncludeSecurity)
60+
61+
c.SwaggerDoc(
62+
settings.Name,
63+
new OpenApiInfo
64+
{
65+
Version = settings.Version,
66+
Title = settings.Title,
67+
Description = settings.Description,
68+
TermsOfService = new Uri(settings.TermsOfService ?? "https://www.genocs.com/terms_and_conditions.html"),
69+
Contact = new OpenApiContact
70+
{
71+
Name = settings.ContactName,
72+
Email = settings.ContactEmail,
73+
Url = new Uri(settings.ContactUrl ?? "https://www.genocs.com")
74+
},
75+
License = new OpenApiLicense
76+
{
77+
Name = settings.LicenseName,
78+
Url = new Uri(settings.LicenseUrl ?? "https://opensource.org/license/mit/")
79+
}
80+
});
81+
82+
if (settings.IncludeSecurity)
4583
{
4684
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
4785
{
4886
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
4987
Name = "Authorization",
5088
In = ParameterLocation.Header,
51-
Type = SecuritySchemeType.ApiKey
89+
Type = SecuritySchemeType.ApiKey,
90+
Scheme = "Bearer"
5291
});
92+
93+
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
94+
{
95+
{
96+
new OpenApiSecurityScheme
97+
{
98+
Name = "Bearer",
99+
Reference = new OpenApiReference
100+
{
101+
Type = ReferenceType.SecurityScheme,
102+
Id = "Bearer"
103+
},
104+
Scheme = "oauth2",
105+
In = ParameterLocation.Header
106+
},
107+
new List<string>()
108+
}
109+
});
110+
}
111+
112+
// This is required to make the custom operation ids work
113+
// It's required to be used by LangChain tools
114+
c.CustomOperationIds(oid =>
115+
{
116+
if (oid.ActionDescriptor is not ControllerActionDescriptor actionDescriptor)
117+
{
118+
return null; // default behavior
119+
}
120+
121+
return oid.GroupName switch
122+
{
123+
"v1" => $"{actionDescriptor.ActionName}",
124+
_ => $"_{actionDescriptor.ActionName}", // default behavior
125+
};
126+
});
127+
128+
// Add list of servers
129+
130+
if (settings.Servers != null)
131+
{
132+
foreach (var server in settings.Servers)
133+
{
134+
c.AddServer(new OpenApiServer() { Url = server.Url, Description = server.Description });
135+
}
53136
}
137+
138+
// c.AddServer(new OpenApiServer() { Url = "http://localhost:5300", Description = "Local version to be used for development" });
139+
// c.AddServer(new OpenApiServer() { Url = "http://fiscanner-api", Description = "Containerized version to be used into with docker or k8s" });
140+
// c.AddServer(new OpenApiServer() { Url = "https://fiscanner-api.azurewebsites.net", Description = "Production deployed on Azure" });
141+
142+
string documentationFile = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetEntryAssembly()?.GetName().Name}.xml");
143+
c.IncludeXmlComments(documentationFile);
54144
});
55145

56146
return builder;

src/apps/orders/Genocs.Orders.WebApi/appsettings.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,32 @@
133133
"swagger": {
134134
"enabled": true,
135135
"reDocEnabled": false,
136-
"name": "v1",
136+
"name": "Orders",
137137
"title": "Orders Service",
138-
"version": "v1",
139-
"routePrefix": "swagger"
138+
"version": "v002",
139+
"description": "Orders Service",
140+
"routePrefix": "swagger",
141+
"termsOfService": "https://www.genocs.com/terms_and_conditions.html",
142+
"contactName": "Giovanni Nocco",
143+
"contactEmail": "[email protected]",
144+
"contactUrl": "https://www.genocs.com",
145+
"licenseName": "MIT",
146+
"licenseUrl": "https://www.genocs.com/license.html",
147+
"includeSecurity": true,
148+
"servers": [
149+
{
150+
"url": "http://localhost:5300",
151+
"description": "Local version to be used for development"
152+
},
153+
{
154+
"url": "http://fiscanner-api",
155+
"description": "Containerized version to be used into with docker or k8s"
156+
},
157+
{
158+
"url": "https://fiscanner-api.azurewebsites.net",
159+
"description": "Production deployed on Azure"
160+
}
161+
]
140162
},
141163
"redis": {
142164
"connectionString": "localhost",

0 commit comments

Comments
 (0)