Skip to content

[V3] Incorrect reference types for classes with more than one collection properties of the same reference type #2254

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

Closed
asankaf opened this issue Mar 13, 2025 · 6 comments

Comments

@asankaf
Copy link

asankaf commented Mar 13, 2025

Describe the bug
For response models that contain multiple collection properties referring to the same collection type, the generated document contains invalid schema references for all but one of those properties.

OpenApi File To Reproduce
SampleOpenApi_v1.0.json

Expected behavior
All the properties should point to the correct schema reference type

Screenshots/Code Snippets

/* Response Models */
public class Account
{
    public int Id { get; init; }
    public required string Name { get; init; }
}

public class LegalEntity
{
    public required ICollection<Account> Balances { get; init; }
    public required ICollection<Account> Assets { get; init; }
    public required ICollection<Account> Equities { get; init; }
    public required ICollection<int> DeletedAccounts { get; init; }
}

/* Api */
[Route("api/v{version:apiVersion}/accounts")]
public class LegalEntitiesController : ApiControllerBase
{
    /// <summary>
    /// Get legal entities
    /// </summary>
    /// <returns>The <see cref="LegalEntity"/></returns>
    [ApiVersion("1.0")]
    [ApiVersion("2.0")]
    [HttpGet(Name = nameof(GetLegalEntityAccounts))]
    [ProducesResponseType(typeof(IEnumerable<LegalEntity>), StatusCodes.Status200OK)]
    [ProducesDefaultResponseType]
    public IEnumerable<LegalEntity> GetLegalEntityAccounts()
    {
        return Enumerable.Range(1, 5).Select(index => new LegalEntity
        {
            Balances =
            [
                new Account { Id = index + 1, Name = "Balance 1" },
                new Account { Id = index + 2, Name = "Balance 2" }
            ],
            Assets =
            [
                new Account { Id = index + 3, Name = "Asset 1" },
                new Account { Id = index + 4, Name = "Asset 2" }
            ],
            Equities =
            [
                new Account { Id = index + 5, Name = "Equity 1" },
                new Account { Id = index + 6, Name = "Equity 2" }
            ],
            DeletedAccounts = [index + 7, index + 8]
        });
    }
}

Invalid schema reference types in the generated open api document

{
  ...
  "components": {
    "schemas": {
     ...
     "LegalEntity": {
       "required": [
         "balances",
         "assets",
         "equities",
         "deletedAccounts"
       ],
       "type": "object",
       "properties": {
         "balances": {
           "type": "array",
           "items": {
             "$ref": "#/components/schemas/Account"
           }
         },
         "assets": {
           "type": "array",
           "items": {
             "$ref": "#/components/schemas/#/items/properties/balances/items"
           }
         },
         "equities": {
           "type": "array",
           "items": {
             "$ref": "#/components/schemas/#/items/properties/balances/items"
           }
         },
         "deletedAccounts": {
           "type": "array",
           "items": {
             "type": "integer",
             "format": "int32"
           }
         }
       }
     },
    }
  }
}

I have created a sample project here to reproduce the issue.

@asankaf asankaf changed the title [V3] Incorrect reference types for classes with more than one collection properties that points to the same type [V3] Incorrect reference types for classes with more than one collection properties of the same reference type Mar 13, 2025
@darrelmiller
Copy link
Member

@captainsafia We looked through this and we are not sure where the source of this error is coming from. It appears that the $ref in balances is generated correctly, but assets and equities is wrong. It feels like the wrong Id value is being provided to the OpenAPIReference when being constructed.

@asankaf
Copy link
Author

asankaf commented Mar 17, 2025

@darrelmiller as you said, 'balances' points to the correct schema type while the others that are of the same reference type do not. If I use Swashbuckle.AspNetCore package, it generates the $ref correctly for all 3 of them.

@asankaf
Copy link
Author

asankaf commented Mar 26, 2025

@darrelmiller any timeline on a fix to this issue? It seems like a very easy to recreate bug to me and if you look at the source I attached, it's quite basic and nothing complicated going on there. So if I am guessing, this seems like an issue in "Microsoft.AspNetCore.OpenApi" package.

@MaggieKimani1
Copy link
Contributor

@captainsafia can you please help confirm if this is an AspNetCore.OpenApi package issue?

@captainsafia
Copy link
Member

@MaggieKimani1 This came up in one of our online syncs a while back. TL;DR: this is an issue in Microsoft.AspNetCore.OpenApi for .NET 9 that was resolved in servicing. However, variants of this bug still exist with the new reference resolution in .NET 10. That's tracked in #2062.

@RachitMalik12
Copy link
Contributor

RachitMalik12 commented Apr 2, 2025

Closing as it is fixed in 9.0.400 release of ASP.NET Core: dotnet/aspnetcore#60381
OpenAPI.NET will use #2062 to track other reference related issues for future releases.
cc: @captainsafia

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

5 participants