admin管理员组文章数量:1345576
I have a C# / ASP.NET Core Minimal API app with Swagger generating an OpenAPI spec. In order to match the publish yml, I need to add a server with path and remove the path elements from individual groups.
In Program.cs
I configure the generator with this code:
.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "2.0",
});
c.AddServer(new OpenApiServer
{
Url = ";,
Description = "Production"
});
c.AddServer(new OpenApiServer
{
Url = "https://localhost:{port}/api/v2",
Description = "Local",
Variables =
{
new ("port", new OpenApiServerVariable
{
Default = "7147",
}),
},
});
})
Then later map an endpoint in a group like:
var builder = app.MapGroup("/needs");
builder .MapGet("/", async (CancellationToken cancellationToken) => { ... });
When I try to test this with the Swagger UI, I get a 404 response. But if I move the path from the server to the group it works just fine.
.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "2.0",
});
c.AddServer(new OpenApiServer
{
Url = "/",
Description = "Production"
});
c.AddServer(new OpenApiServer
{
Url = "https://localhost:{port}/",
Description = "Local",
Variables =
{
new ("port", new OpenApiServerVariable
{
Default = "7147",
}),
},
});
})
// ...
var builder = app.MapGroup("/api/v2/needs");
builder .MapGet("/", async (CancellationToken cancellationToken) => { ... });
I've tried lots of combinations but can't figure out what is wrong. Does this just happen to be part of the spec that isn't implemented correctly or is there a way to make it work?
Ultimately the generated definition needs to looks like
openapi: 3.0.1
info:
title: My API
version: '2.0'
servers:
- url:
description: Production
- url: 'https://localhost:{port}'
description: Local
variables:
port:
default: '7147'
paths:
/needs:
get:
...
I have a C# / ASP.NET Core Minimal API app with Swagger generating an OpenAPI spec. In order to match the publish yml, I need to add a server with path and remove the path elements from individual groups.
In Program.cs
I configure the generator with this code:
.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "2.0",
});
c.AddServer(new OpenApiServer
{
Url = "https://api.myapi..uk/api/v2",
Description = "Production"
});
c.AddServer(new OpenApiServer
{
Url = "https://localhost:{port}/api/v2",
Description = "Local",
Variables =
{
new ("port", new OpenApiServerVariable
{
Default = "7147",
}),
},
});
})
Then later map an endpoint in a group like:
var builder = app.MapGroup("/needs");
builder .MapGet("/", async (CancellationToken cancellationToken) => { ... });
When I try to test this with the Swagger UI, I get a 404 response. But if I move the path from the server to the group it works just fine.
.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "2.0",
});
c.AddServer(new OpenApiServer
{
Url = "https://api.myapi..uk/",
Description = "Production"
});
c.AddServer(new OpenApiServer
{
Url = "https://localhost:{port}/",
Description = "Local",
Variables =
{
new ("port", new OpenApiServerVariable
{
Default = "7147",
}),
},
});
})
// ...
var builder = app.MapGroup("/api/v2/needs");
builder .MapGet("/", async (CancellationToken cancellationToken) => { ... });
I've tried lots of combinations but can't figure out what is wrong. Does this just happen to be part of the spec that isn't implemented correctly or is there a way to make it work?
Ultimately the generated definition needs to looks like
openapi: 3.0.1
info:
title: My API
version: '2.0'
servers:
- url: https://myapi..uk/api/v2
description: Production
- url: 'https://localhost:{port}'
description: Local
variables:
port:
default: '7147'
paths:
/needs:
get:
...
Share
Improve this question
edited yesterday
Jason Pan
22.4k2 gold badges22 silver badges45 bronze badges
asked 2 days ago
JamesJames
4554 silver badges14 bronze badges
1 Answer
Reset to default 1We should use app.UsePathBase
to fix the issue. Here is my test code.
Microsoft.AspNetCore.OpenApi
package is needed.
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.OpenApi;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "2.0",
});
c.AddServer(new OpenApiServer
{
Url = "https://api.myapi..uk/api/v2",
Description = "Production"
});
c.AddServer(new OpenApiServer
{
Url = "https://localhost:{port}/api/v2",
Description = "Local",
Variables =
{
["port"] = new OpenApiServerVariable
{
Default = "7147",
}
}
});
});
var app = builder.Build();
app.UsePathBase("/api/v2");
app.UseRouting();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1");
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
var needsGroup = app.MapGroup("/needs");
needsGroup.MapGet("/", () => "Hello from Needs API!")
.WithOpenApi();
app.Run();
本文标签:
版权声明:本文标题:c# - How to include path in server url element of OpenAPI spec generated from ASP.NET Core Minimal API application? - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743814867a2543726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论