Skip to content

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

@asankaf

Description

@asankaf

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions