Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli Swagger.json can't resolve anything #2622

Closed
josh-hemphill opened this issue Mar 17, 2023 · 2 comments
Closed

Cli Swagger.json can't resolve anything #2622

josh-hemphill opened this issue Mar 17, 2023 · 2 comments

Comments

@josh-hemphill
Copy link

Potential continuation of #1580 and a couple others I can't find now; however, none of the solutions seemed to work.
I'm just trying to start a fresh C# .NET7 web api + static website (with forwarding set for Vite during development, though that shouldn't affect anything here)

I tried recreating the Startup.cs file/class and usage as described by others, but that didn't seem to affect it. So for brevity, I've gone back to a single Program.cs file I share below.
I've tried swapping the ApiExplorer in various ways and removing the options I set, all to no avail.

I'm at my wits end with this, which is why I'm posting here.
If this gets figured out, I'd be happy to submit a PR for a getting started example for NET7+ so no one else has to go through this.

Here's the whole program initialization:

using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.AspNetCore.Mvc.Versioning;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
	ApplicationName = "TheNamespaceIUseForEverything",
	Args = args
});
// Add services to the container.

var config = builder.Configuration;
var parsedRequiredConfig = new ParsedRequiredConfig(config);
var services = builder.Services;
var env = builder.Environment;


services.AddControllers().AddJsonOptions(options =>
	{
		options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
		options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
	});
services.AddMvc();
services.AddEndpointsApiExplorer();
services.AddApiVersioning(options =>
	{
		options.AssumeDefaultVersionWhenUnspecified = true;
		options.ApiVersionReader = new UrlSegmentApiVersionReader();
		options.UseApiBehavior = false;
	});
services.AddVersionedApiExplorer(options =>
	{
		options.GroupNameFormat = "VVV";
		options.SubstituteApiVersionInUrl = true;
	});

services.AddSwaggerGen();

// services.Configure<SwaggerGenOptions>(Configuration.GetSection("SwaggerGen"));

services.Configure<SwaggerOptions>(config.GetSection("Swagger"));
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();

var app = builder.Build();


if (env.IsDevelopment())
{
	app.UseDeveloperExceptionPage();
}
else
{
	// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
	app.UseHsts();
}

app.UseStaticFiles()
	.UseApiVersioning()
	.UseRouting()
	.UseEndpoints(endpoints =>
	{
		endpoints.MapControllers();
	})
	.UseSwagger();

app.MapControllerRoute(
	 name: "default",
	 pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html");

app.Run();

The SwaggerGenOptions:

public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
	readonly IApiVersionDescriptionProvider provider;

	public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) =>
		 this.provider = provider;

	public void Configure(SwaggerGenOptions options)
	{
		foreach (var description in provider.ApiVersionDescriptions)
		{
			options.SwaggerDoc(
				$"OpenAPI_v{description.GroupName}",
				new OpenApiInfo()
				{
					Title = $"Doc for API v{description.ApiVersion}",
					Description = "",
					Version = $"v{description.ApiVersion.ToString()}",
				});
		}
		var xmlCommentsPath = Path.Combine(
			System.AppContext.BaseDirectory,
			"ApiComments.xml"
		);
		options.IncludeXmlComments(xmlCommentsPath, true);
	}
}

The Swagger section of the settings just sets:

"Swagger": {
    "RouteTemplate": "docs/{documentName}/openapi.json"
  }
@Havunen
Copy link

Havunen commented Feb 18, 2024

Have you tested with DotSwashbuckle and .Net8 that has most likely this issue resolved

@martincostello
Copy link
Collaborator

Duplicate of #2290

@martincostello martincostello marked this as a duplicate of #2290 Apr 14, 2024
@martincostello martincostello closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants