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

Sieve does not work with asp.net core minimal API's #181

Open
klyse opened this issue Aug 15, 2022 · 4 comments
Open

Sieve does not work with asp.net core minimal API's #181

klyse opened this issue Aug 15, 2022 · 4 comments

Comments

@klyse
Copy link

klyse commented Aug 15, 2022

Describe the bug
The new asp.net core minimal api's are not able to handle the SieveModel

To Reproduce
Steps to reproduce the behaviour:

  • Create a new project and use the minimal API.
  • Map any Get request: app.MapGet("/test", ([FromQuery] SieveModel model) => { });
  • Start the app and make the first request to the /test endpoint

Expected behaviour
The app starts and binds the SieveModel

Actual behvaiour
Exception is thrown:

[11:00:53 ERR] An unhandled exception has occurred while executing the request. => Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware
System.InvalidOperationException: No public static bool SieveModel.TryParse(string, out SieveModel) method found for model.
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.BindParameterFromValue(ParameterInfo parameter, Expression valueExpression, FactoryContext factoryContext, String source)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgument(ParameterInfo parameter, FactoryContext factoryContext)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArguments(ParameterInfo[] parameters, FactoryContext factoryContext)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateTargetableRequestDelegate(MethodInfo methodInfo, Expression targetExpression, FactoryContext factoryContext, Expression`1 targetFactory)
...

Desktop (please complete the following information):

  • OS: macos
  • dotnet 7.0.0-preview.7.22376.6
@brianebeling
Copy link

I was able to reproduce the issue. Is there an update on this issue?

@klyse
Copy link
Author

klyse commented Sep 12, 2022

Not to my knowledge. I worked around this problem by creating a wrapper class:

public class SieveWrapper
{
	[UsedImplicitly]
	[FromQuery]
	public string? Filters { get; set; }

	[UsedImplicitly]
	[FromQuery]
	public string? Sorts { get; set; }

	[UsedImplicitly]
	[FromQuery]
	public int? Page { get; set; }

	[UsedImplicitly]
	[FromQuery]
	public int? PageSize { get; set; }

	protected SieveModel GetSieve() => new()
	{
		Filters = Filters,
		Sorts = Sorts,
		Page = Page,
		PageSize = PageSize
	};
}

@brianebeling
Copy link

brianebeling commented Sep 12, 2022

Oh, that's smart. I tried writing a static extension method that would provide the missing TryParse. Unfortunately, .NET doesn't pick it up. I guess it was worth a try.

Now that I'm seeing your solution, I feel bad for not even thinking about just wrapping the class. Oof. Thanks for your response, I greatly appreciate a solution that doesn't require me to use FromBody haha

@joaquinalb
Copy link

As an update to this topic, starting with .NET version 7 preview 5, the [AsParameters] attribute can be passed in and the SieveModel works correctly in Minimal APIs.

image

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

No branches or pull requests

3 participants