Skip to content

[FromForm] Complex objects with MinimalApi and AOTΒ #61382

@kevin-fernandez-dgmx

Description

@kevin-fernandez-dgmx

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have been trying to generate an api that receives a complete object for a Form, however every time I add the attribute [FromForm] to my endpoint method with minimal API, it starts throwing errors in the source generator. Is there a solution for this?

public static class PdfGeneratorApi
{
  public static IEndpointRouteBuilder MapPdfGeneratorApi(this IEndpointRouteBuilder app)
  {
    var api = app.MapGroup("api/pdfGenerator");

    api.MapPost("/", GeneratePdf);

    return app;
  }

  private static async Task<IResult> GeneratePdf([FromForm] GeneratePdfModel model)
  {
    var launchOptions = new LaunchOptions
    {
      Headless = true,
      ExecutablePath = "/opt/chrome-headless-shell-linux64/chrome-headless-shell",
      Args = ["--no-sandbox", "--disable-gpu", "--single-process", "--no-zygote"]
    };
    
    await using var browser = await Puppeteer.LaunchAsync(launchOptions);
    await using var page = await browser.NewPageAsync();
    
    var originalString = string.Empty;
    
    using var streamFile = new MemoryStream();
    await model.File.CopyToAsync(streamFile);
    streamFile.Position = 0;
    
    using (var reader = new StreamReader(streamFile))
    { 
      originalString = await reader.ReadToEndAsync();
    }

    await page.SetContentAsync(originalString);

    var pdfFile = await page.PdfStreamAsync(new PdfOptions()
    {
      PrintBackground = true
    });

    return Results.File(pdfFile, "application/pdf", $"{model.Nombre}.pdf");
  }
}

This is my class:

public class GeneratePdfModel
{
  public IFormFile File { get; set; }
  
  public string Nombre { get; set; }
}

And this is the error I'm getting:
GeneratedRouteBuilderExtensions.g.cs(123,83): Error CS0029 : Cannot implicitly convert type 'string' to 'PdfGeneratorApi.Models.GeneratePdfModel'

Expected Behavior

Allow complex objects in a minimal api when using [FromForm] attribute in NativeAot

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.407

Anything else?

I'm using Rider

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    NativeAOTarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions