Skip to content

[OpenAPI] Nullable struct doesn't get description in schema #69101

Description

@dnperfors

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a project I use some nullable structs in my API with XML Documentation to give it a description, but this description is not shown in the openapi specification:

  "components": {
    "schemas": {
      "MyClass": {
        "type": "object",
        "description": "My class for demonstration purposes."
      },
      "MyStruct": {
        "type": "object"
      },
      "MyStruct2": {
        "type": "object",
        "description": "My second struct for demonstration purposes."
      }
    }
  },

Expected Behavior

I expect that the XmlCommentSchemaTransformer ignores the Nullable<> type when trying to find the description:

  "components": {
    "schemas": {
      "MyClass": {
        "type": "object",
        "description": "My class for demonstration purposes."
      },
      "MyStruct": {
        "type": "object",
        "description": "My struct for demonstration purposes."
      },
      "MyStruct2": {
        "type": "object",
        "description": "My second struct for demonstration purposes."
      }
    }
  },

Steps To Reproduce

The following Program.cs in a simple webapi project will reproduce this problem:

using Microsoft.AspNetCore.Http.HttpResults;

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddOpenApi();

var app = builder.Build();

app.MapOpenApi();

app.MapGet("/nullable-struct", Ok<MyStruct?> () => TypedResults.Ok<MyStruct?>(null))
    .WithName("GetMyStruct");

app.MapGet("/not-nullable-struct", Ok<MyStruct2> () => TypedResults.Ok<MyStruct2>(default))
    .WithName("GetMyStruct2");

app.MapGet("/class", Ok<MyClass?> () => TypedResults.Ok<MyClass?>(null))
    .WithName("GetMyClass");

app.Run();

/// <summary>
/// My struct for demonstration purposes.
/// </summary>
internal struct MyStruct() { }

/// <summary>
/// My second struct for demonstration purposes.
/// </summary>
internal struct MyStruct2() { }

/// <summary>
/// My class for demonstration purposes.
/// </summary>
internal class MyClass() { }

Exceptions (if any)

No response

.NET Version

10.0.400

Anything else?

After some debugging I found that in the 'XmlCommentGenerator.Emitter.cs' the generated code for DocumentationCommentIdHelper doesn't request the underlying type from a nullable type, therefore it will never generate the correct Id.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions