Skip to content

[OpenAPI] generates invalid reference for a type #61039

Closed as duplicate of#60339
Closed as duplicate of#60339
@petarvukmirovic

Description

@petarvukmirovic

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Given these two classes:

public class NestedPropertyClass
{
    public required string PropertyA { get; init; }
    public required string PropertyB { get; init; }
}

public class TopLevelPropertyClass
{
    public required int IntProp { get; init; }
    public required string StringPropA { get; init; }
    public required string StringPropB { get; init; }
    public required IEnumerable<NestedPropertyClass> NestedPropA { get; init; }
    public required IEnumerable<NestedPropertyClass> NestedPropB { get; init; }
}

OpenAPI will generate the following type definition:

...
"TopLevelPropertyClass": {
      "required": [
        "intProp",
        "stringPropA",
        "stringPropB",
        "nestedPropA",
        "nestedPropB"
      ],
      "type": "object",
      "properties": {
        "intProp": {
          "format": "int32",
          "type": "integer"
        },
        "stringPropA": {
          "type": "string"
        },
        "stringPropB": {
          "type": "string"
        },
        "nestedPropA": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/NestedPropertyClass"
          }
        },
        "nestedPropB": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/#/properties/nestedPropA/items"
          }
        }
      }
    },
...

Notably, nestedPropB will have an inavlid reference string which is different
from the one generated for nestedPropA, and different from expected type
#/definitions/NestedPropertyClass.

Is there a workaround to force OpenAPI to generate
type reference #/definitions/NestedPropertyClass?

Expected Behavior

I expect nestedPropB to have the same type as nestedPropA (#/definitions/NestedPropertyClass).

Steps To Reproduce

Use these classes, add them to a controller and request OpenAPI generation endpoint.

Exceptions (if any)

N/A

.NET Version

9.0.1

Anything else?

.NET SDK:
Version: 9.0.102
Commit: cb83cd4923
Workload version: 9.0.100-manifests.4a54b1a6
MSBuild version: 17.12.18+ed8c6aec5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.102\

.NET workloads installed:
[aspire]
Installation Source: VS 17.12.35707.178
Manifest Version: 8.2.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Install Type: Msi

Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.1
Architecture: x64
Commit: c8acea2262

.NET SDKs installed:
6.0.428 [C:\Program Files\dotnet\sdk]
8.0.406 [C:\Program Files\dotnet\sdk]
9.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.1 [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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions