Skip to content

OpenAPI Document Generation repeated types across multiple unrelated records not generating type in schema #61527

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

Open
1 task done
Lachlan212 opened this issue Apr 17, 2025 · 1 comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@Lachlan212
Copy link

Lachlan212 commented Apr 17, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When properties of two unrelated types share the same type, OpenAPI document generation does not assign types to one of them. This applies if both unrelated types are returned from the same minimal api endpoint. For example: If there was a record MapNode, and record MapLink, and two different records had Collections of each, regardless of what the property name is, one of the two different records will not generate correctly.

This does not happen if MapNode and MapLink are not in Collections. I have tested with Lists and Arrays.

To show:

public record MapNode
{
    public required string Name { get; init; }
}

public record MapLink
{
    public required int Source { get; init; }
    public required int Target { get; init; }
    public required decimal Value { get; init; }
}

public record OverallDataContainer
{
    public DataContainer1? Container1 { get; init; }
    public DataContainer2? Container2 { get; init; }
}

public record DataContainer1
{
    public required List<MapNode> Container1MapNodes { get; init; }
    public required List<MapLink> Container1MapLinks { get; init; }
}

public record DataContainer2
{
    public required List<MapNode> Container2MapNodes { get; init; }
    public required List<MapLink> Container2MapLinks { get; init; }
}

If OverallDataContainer was returned by an endpoint, DataContainer1 generates an openApi schema correctly, while DataContainer2 does not:

"DataContainer1": {
        "required": [
          "container1MapNodes",
          "container1MapLinks"
        ],
        "type": "object",
        "properties": {
          "container1MapNodes": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapNode"
            }
          },
          "container1MapLinks": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapLink"
            }
          }
        },
        "nullable": true
      },
      "DataContainer2": {
        "required": [
          "container2MapNodes",
          "container2MapLinks"
        ],
        "type": "object",
        "properties": {
          "container2MapNodes": {
            "type": "array",
            "items": { }
          },
          "container2MapLinks": {
            "type": "array",
            "items": { }
          }
        },
        "nullable": true
      },

Current workaround: Split MapNode and MapLink into different types per record (Container1MapNode, Container1MapLink, Container2MapNode, and Container2MapLink)

Expected Behavior

Expected behaviour would be that container2MapNodes and container2MapLinks share the same properties as container1MapNodes and containter1MapLinks:

"DataContainer1": {
        "required": [
          "container1MapNodes",
          "container1MapLinks"
        ],
        "type": "object",
        "properties": {
          "container1MapNodes": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapNode"
            }
          },
          "container1MapLinks": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapLink"
            }
          }
        },
        "nullable": true
      },
      "DataContainer2": {
        "required": [
          "container2MapNodes",
          "container2MapLinks"
        ],
        "type": "object",
        "properties": {
          "container2MapNodes": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapNode"
            }
          },
          "container2MapLinks": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MapLink"
            }
          }
        },
        "nullable": true
      },

Steps To Reproduce

Github repo which reproduces the issue

Exceptions (if any)

No response

.NET Version

9.0.203

Anything else?

Microsoft.AspNetCore.OpenApi version 9.0.4 - This is also an issue when downgraded to version 9.0

Scalar.AspNetCore version 2.1.16

Running on Visual Studio Community 2022 v17.13.6

@ghost ghost added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Apr 17, 2025
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Apr 17, 2025
@martincostello
Copy link
Member

This looks like it might be a duplicate of #61407.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

2 participants