Skip to content

[release/9.0] Fix ModelMetadata for TryParse-parameters in ApiExplorer#58372

Merged
wtgodbe merged 2 commits into
release/9.0from
safia/backport-tryparse-bugfix
Oct 15, 2024
Merged

[release/9.0] Fix ModelMetadata for TryParse-parameters in ApiExplorer#58372
wtgodbe merged 2 commits into
release/9.0from
safia/backport-tryparse-bugfix

Conversation

@captainsafia

Copy link
Copy Markdown
Contributor

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 the IParsable 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

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;
    }
}

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Change localized to OpenAPI support + TryParsable parameters in the route query.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants