dotnet / dotnet/aspnetcore

`UsePathBase()` not being taking into account when generating OpenApi document with `AddOpenApi()`

Open
#61,486 2 comments 3 reactions 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I have currently an ASP.NET Web API in .NET 9.0.4 (SDK 9.0.203) in which I use the OpenApi document generator calling `AddOpenApi()` in the services pipeline (with `Microsoft.AspNetCore.OpenApi`).

After calling the method `AddOpenApi()` I map a custom base path via `app.UsePathBase("/myapi");` and after that I call `app.MapOpenApi();` to map the path to serve the OpenApi document.

Nevertheless, It does not take this `PathBase` into account in the generated OpenApi doc (served in my case in `/openapi/v1.json`) in the servers array in the URL field. For example:

```json
{
"openapi": "3.0.1",
"info": {
"title": "Api | v1",
"version": "1.0.0"
},
"servers": [
{
"url": "https://example.org/"
}
],
"paths": {
"/v1/test": {
}
}
...
}
```

### Expected Behavior

The expected behaviour would be that the `servers.url` field would contain that base path if specified (since `basePath` property in openapi is deprecated in its version 3.x).

### Steps To Reproduce

Simply by following the given example here: https://devblogs.microsoft.com/dotnet/dotnet9-openapi/ and adding a call to establish a path base like: `app.UsePathBase("/myapi");`

### Exceptions (if any)

_No response_

### .NET Version

9.0.203

### Anything else?

ASP.NET Core version: Runtime 9.0.4
Current IDE: Visual Studio 2022 (Professional)
`dotnet --info` output:

```
SDK DE .NET:
Version: 9.0.203
Commit: dc7acfa194
Workload version: 9.0.200-manifests.12d79ccf
MSBuild version: 17.13.20+a4ef1e90f

Entorno de tiempo de ejecución:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.203\

Cargas de trabajo de .NET instaladas:
No hay cargas de trabajo instaladas para mostrar.
Configurado para usar loose manifests al instalar nuevos manifiestos.

Host:
Version: 9.0.4
Architecture: x64
Commit: f57e6dc747

.NET SDKs installed:
9.0.203 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the documented AddOpenApi(), UsePathBase("/myapi"), and MapOpenApi() setup from the issue. Trace how the generated document sets servers.url, then verify that the result includes /myapi while preserving the existing OpenAPI paths and document endpoint behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.