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

Net6.0 Minimal API projects without Startup, issue #3794 #3814

Merged
merged 1 commit into from
Jan 25, 2022

Conversation

bsurprised
Copy link
Contributor

Fixes missing controllers in the specification when generating Net6.0 Minimal API projects using CLI/MSBuild

Fixes missing controllers in the specification when generating Net6.0 Minimal API projects using CLI/MSBuild
@@ -140,7 +140,13 @@ void OnEntryPointExit(Exception exception)
try
{
// Get the IServiceProvider from the host
#if NET6_0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NET6_0_OR_GEATER or else it will be broken in NET 7 again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.
From my understanding, NSwag style has been code tailored to each dotnet version and I made it NET6 specific. DotNet has had many breaking changes in recent versions, so I do hope this works for NET7 as well. :-)

@vpetrusevici
Copy link

Hello @RicoSuter
Can you say please when this fix will be released?

@lahma
Copy link
Collaborator

lahma commented Jan 31, 2022

@vpetrusevici There's now a new release on NuGet

@vip32
Copy link

vip32 commented Feb 1, 2022

just tried it, works fine :) However when also setting up API versioning (Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer) with the following line in Program.cs. Note that the versioning is not really used in the minimal endpoints or controller.

builder.Services.AddVersionedApiExplorer(options => options.SubstituteApiVersionInUrl = true);

the minimal endpoints are not present in the swagger, the controller operations are present.

====================================================================
this is the whole sample (NSwag.Sample.NET60Minimal) is used:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.DependencyInjection;
using System;

var builder = WebApplication.CreateBuilder(args);

// Optional: Use controllers
builder.Services.AddControllers();

builder.Services.AddApiVersioning(options => ConfigureApiVersioning(options));
builder.Services.AddVersionedApiExplorer(options => options.SubstituteApiVersionInUrl = true);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(settings =>
{
    settings.Title = "Minimal API";
    settings.Version = "v1";
});

var app = builder.Build();
app.UseDeveloperExceptionPage();

app.UseOpenApi();
app.UseSwaggerUi3();

app.MapGet("/", (Func<string>)(() => "Hello World!"))
    .WithTags("General123");

app.MapGet("/sum/{a}/{b}", (Func<int, int, int>)((a, b) => a + b))
    .WithName("CalculateSum")
    .WithTags("Calculator");

// Optional: Use controllers
app.UseRouting();
app.MapControllers();

app.Run();

void ConfigureApiVersioning(ApiVersioningOptions options)
{
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ReportApiVersions = true;
}

// Optional: Use controllers
[ApiController]
[Route("api/examples")]
public class ExampleController : ControllerBase
{
	[HttpGet]
	public IActionResult Get()
	{
		return Ok("Examples");
	}
}

@RicoSuter
Copy link
Owner

However when also setting up API versioning (Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer) with the following line in Program.cs. Note that the versioning is not really used in the minimal endpoints or controller.

Seems that for some reason the minimal operations are not reported anymore by API Explorer... needs investigating.

@vip32
Copy link

vip32 commented Feb 18, 2022

do we have to wait for this (OpenAPI in .NET ), or is it more NSwag specific that operations stop being reported.

@RicoSuter
Copy link
Owner

Is this still a problem with v13.15.10?

@vip32
Copy link

vip32 commented Feb 23, 2022

yes, just tried again with 13.15.10. AddVersionedApiExplorer still causes minimal endpoints to be not present in the generated swagger json.

lahma pushed a commit to lahma/NSwag that referenced this pull request Dec 25, 2022
…coSuter#3814)

Fixes missing controllers in the specification when generating Net6.0 Minimal API projects using CLI/MSBuild
lahma pushed a commit to lahma/NSwag that referenced this pull request Jan 20, 2024
…coSuter#3814)

Fixes missing controllers in the specification when generating Net6.0 Minimal API projects using CLI/MSBuild
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

Successfully merging this pull request may close these issues.

6 participants