Skip to content
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

Scaffolding minimal api should use lower case parameter names for generated lambdas #1954

Closed
geometrikal opened this issue Jul 6, 2022 · 1 comment

Comments

@geometrikal
Copy link

Scaffolding minimal api from class with primary key Id capitalises method parameter but uses lower case for route pattern. This causes Connect Services OpenAPI code generation (NSwag) to generate erroneous code.

Steps to reproduce

  1. Create simple dto
public class Dto
{
    public string Id { get; set; }
}
  1. Scaffold minimal api with read/write endpoints using Entity Framework and OpenAPI enabled. Code generated will look like this:
routes.MapGet("/api/Dto/{id}", async (string Id, PerceptionAnnotationServerContext db) =>
        {
            return await db.Dto.FindAsync(Id)
                is Dto model
                    ? Results.Ok(model)
                    : Results.NotFound();
        })
        .WithName("GetDtoById")
        .Produces<Dto>(StatusCodes.Status200OK)
        .Produces(StatusCodes.Status404NotFound);

Note the parameter name Id instead of id. If scaffolding using controllers, the lower case version is used.

Using dotnet swagger to generate the open api json results in route with lower case, but parameter name capitalised. This in turn causes NSwag to generate incorrect code, where they try to replace {Id} but should be replacing {id}

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/Dto/{id}");
urlBuilder_.Replace("{Id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));

Microsoft.VisualStudio.Web.CodeGeneration.Design/Microsoft.DotNet.MSIdentity version: 6.0.6
Target framework: .NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022

@vijayrkn
Copy link
Contributor

I see that this change is already merged. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants