Skip to content

OpenAPI documentation is incorrect when using custom TryParse() binding with Minimal APIs #58318

@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have used the same title of #36412 because the same issue happens today using the new OpenAPI documentation generator that is built-in in .NET 9.0.

With this code:

app.MapGet("/student/{student}", (Student student) => $"Hi {student.Name}");

public record Student(string Name)
{
    public static bool TryParse(string value, out Student? result)
    {
        if (value is null)
        {
            result = null;
            return false;
        }

        result = new Student(value);
        return true;
    }
}

OpenAPI documentation is wrongly created as the following:

"parameters": [
    {
        "name": "student",
        "in": "query",
        "required": true,
        "schema": {
            "$ref": "#/components/schemas/Student"
        }
    }
],
// ...
"components": {
    "schemas": {
        "Student": {
            "required": [
                "name"
            ],
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        }
    }
},

Expected Behavior

Parameters that use a TryParse custom binding should be configured as string in the openapi/v1.json file:

"parameters": [
    {
        "name": "student",
        "in": "query",
        "required": true,
        "schema": {
            "type": "string"
        }
    }
],

Steps To Reproduce

Minimal repro: https://github.com/marcominerva/TryParseOpenApiIssue

Exceptions (if any)

No response

.NET Version

9.0.0-rc.2.24474.3

Anything else?

No response

Metadata

Metadata

Assignees

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