dotnet / dotnet/aspnetcore

Microsoft.OpenAPI bad schema refs when involving collections

Open
#64,193 4 comments 4 reactions 0 assignees View on GitHub
area-minimal feature-openapi investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Invalid schema references between schemas given these conditions:

1. ModelA has a collection of anything
2. ModelB has > 1 collection of ModelA

### Expected Behavior

All references to ModelA can be resolved.

### Steps To Reproduce

.csproj:
```


net10.0
enable
enable
CS8618




all
runtime; build; native; contentfiles; analyzers; buildtransitive


```

Program.cs:
```
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddOpenApi();

WebApplication app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
```

Models / DTOs:
```
public class ApiResponse
{
public Person Person { get; set; }

public ICollection ICollectionPersons { get; set; }

public IList IListPersons { get; set; }
}

public class Person
{
public Guid Id { get; set; }

public string Name { get; set; } = string.Empty;

public ICollection ICollectionAlbums { get; set; }
}

public class Album
{
public Guid Id { get; set; }

public string Artist { get; set; }
}
```

Controller:
```
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public ActionResult GetApiResponse()
{
return new ActionResult(new ApiResponse());
}
}
```

Result:
```
{
"openapi": "3.1.1",
"info": {
"title": "BadSchemaRefs | v1",
"version": "1.0.0"
},
"paths": {
"/api/Test": {
"get": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ApiResponse"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApiResponse"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ApiResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Album": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"artist": {
"type": "string"
}
}
},
"ApiResponse": {
"type": "object",
"properties": {
"person": {
"$ref": "#/components/schemas/Person"
},
"iCollectionPersons": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Person"
}
},
"iListPersons": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Person"
}
}
}
},
"Person": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string"
},
"iCollectionAlbums": {
"$ref": "#/components/schemas/ApiResponse/properties/person/properties/iCollectionAlbums"
}
}
}
}
},
"tags": [
{
"name": "Test"
}
]
}
```

### Exceptions (if any)

_No response_

### .NET Version

10.0.0-rc.2.25502.107

### Anything else?

Related issues:

- https://github.com/dotnet/aspnetcore/issues/64048
- https://github.com/dotnet/aspnetcore/issues/60931

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.