Skip to content

Swagger in unable to produce example values for nullable properties with OpenAPI in .NET 10 #61210

@ctyar

Description

@ctyar

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The following model with .NET 10, Microsoft.AspNetCore.OpenApi and Swagger

public class TodoRequest
{
    [Required]
    public string? Description { get; set; } = null!;
    public DateTimeOffset? DueDate { get; set; }
    [Required]
    public Priority? Priority { get; set; }
    public List<string>? Tags { get; set; } = [];
}

Would result in the following example value:
Image

Note the nulls for Description and DueDate properties in the Example Value section of the screenshot.

Expected Behavior

This was working in .NET 9 and would result in the following example value:

Image

Steps To Reproduce

Clone the following repo and run the project.
You can also change the TargetFramework to net9.0 to see the previous behaviour.
https://github.com/ctyar/Playground/tree/dotnet_10_openapi

Exceptions (if any)

No response

.NET Version

10.0.100-preview.3.25177.4

Anything else?

I believe this is happening because of the move from openapi 3.0.1 to openapi 3.1.1.
It seems the new specification handles nulls differently, so there was a change in the openapi JSON that Microsoft.AspNetCore.OpenApi produces.

For example, the sample above produces the following JSON specification for the Description property:

"description": {
            "type": [
              "null",
              "string"
            ]
          },

If we change the order of members in the type array to:

"description": {
            "type": [
              "string",
              "null"
            ]
          },

Swagger is able to produce the correct example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-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