-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[release/9.0] Fix ModelMetadata for TryParse-parameters in ApiExplorer #58372
Conversation
Approved via email. |
@captainsafia can you resolve the merge conflict? |
* Fix ModelMetadata for TryParse-parameters in ApiExplorer * Add tests and update code comments * Update src/OpenApi/src/Services/OpenApiDocumentService.cs
cb2c856
to
bf2014c
Compare
@wtgodbe Done! @BrennanConroy Can you take a look at this backport? Thanks! |
could this be the solution for issue where unbound route parameter changes schema type of other action after caching it? in my sample, it has nothing to do with using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi();
app.MapControllers();
app.MapDefaultControllerRoute();
app.Run();
[ApiController]
[Route("api/test")]
public class TestController : ControllerBase
{
/// <summary>
/// method that's defined properly.
/// but open api generates parameter value as long (from broken method) instead of string
/// only AFTER first call to openapi explorer. next time OpenApiSchemaStore returns incorrect cached value
/// NOTE: if parameter in this method was not a complex object, it would have been fine.
/// </summary>
[HttpGet("search")]
public IActionResult Search([FromQuery] SearchQuery query) => Ok();
/// <summary>
/// this is the method that breaks other actions.
/// it defines a route parameter {id:long} in template but does not have parameter to bind it to.
/// NOTE: not specifying type of id will not break other actions.
/// </summary>
[HttpGet("{id:long}/broken-method")]
public IActionResult BrokenMethod() => Ok();
}
public class SearchQuery
{
public string Term { get; init; }
} |
@Phalelashvili I think what you're seeing is a different issue. I believe there's a difference in behavior between MVC and Minimal APIs where route parameters not associated with a method parameter don't materialize in the ApiExplorer model. Can you file an issue for this? |
Description
This PR fixes the handling for schemas associated with types that implement a custom TryParse and are consumed from either the route or query string.
Fixes #58318
Customer Impact
Without this change, APIs that bind parameters from the query string or URL will produce invalid schemas if the type being bound to implements a
TryParse
method or theIParsable
interface.There are workarounds for this scenario (using schema transformers) but it requires users add a fair bit of code in their own applications and this fix is small enough to warrant improving the QoL for this.
Example of impacted scenario
Regression?
Risk
Change localized to OpenAPI support + TryParsable parameters in the route query.
Verification
Packaging changes reviewed?