Skip to content

Automatically describe as an Open API 'string' types that implement TryParse instead of as 'object' #61122

Open
@jscarle

Description

@jscarle

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Similar to the issue asking to automatically create OpenApiParameters when using BindAsync in Minimal API, implementing TryParse for Minimal API custom binding will describe the parameter as object.

Describe the solution you'd like

If a type implements TryParse, at a minimum, the parameter should be described as a string.

Additional context

The workaround currently requires the use of a custom schema transformer such as this:

builder.Services.AddOpenApi(options =>
{
    options.AddSchemaTransformer((schema, context, _) =>
    {
        var isParsable = context.JsonTypeInfo.Type.GetInterfaces()
            .Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IParsable<>)); 

        if (isParsable)
            schema.Type = "string";
        
        return Task.CompletedTask;
    });
});

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