dotnet / dotnet/aspnetcore

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

Open
#61,122 3 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-openapi Needs: Attention :wave: Needs: Repro
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] 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](https://github.com/dotnet/aspnetcore/issues/51218), implementing `TryParse` for [Minimal API custom binding](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/parameter-binding#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:

```csharp
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;
});
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.