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

Ensure REST query parameters of type string are bound as empty strings instead of null #1098

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Threading.Tasks;


namespace GeneXus.Utils
Expand All @@ -29,6 +32,36 @@ public void OnActionExecuting(ActionExecutingContext context)
(context.Controller as GxRestService).Initialize();
}
}

public class QueryStringModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context.BindingInfo.BindingSource == BindingSource.Query &&
context.Metadata.ModelType == typeof(string))
{
return new BinderTypeModelBinder(typeof(CustomQueryStringBinder));
}
return null;
}
}
public class CustomQueryStringBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!bindingContext.BindingSource.CanAcceptDataFrom(BindingSource.Query))
{
return Task.CompletedTask;
}
if (bindingContext.ModelType != typeof(string))
{
return Task.CompletedTask;
}
string value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue;
bindingContext.Result = ModelBindingResult.Success(value ?? string.Empty);
return Task.CompletedTask;
}
}
[TypeFilter(typeof(CustomActionFilter))]
public class GxRestService : ControllerBase
{
Expand Down
8 changes: 6 additions & 2 deletions dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,13 @@ public void ConfigureServices(IServiceCollection services)
private void RegisterControllerAssemblies(IMvcBuilder mvcBuilder)
{

if (RestAPIHelpers.ServiceAsController() && !string.IsNullOrEmpty(VirtualPath))
if (RestAPIHelpers.ServiceAsController())
{
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new SetRoutePrefix(new RouteAttribute(VirtualPath))));
mvcBuilder.AddMvcOptions(options => options.ModelBinderProviders.Insert(0, new QueryStringModelBinderProvider()));
if (!string.IsNullOrEmpty(VirtualPath))
{
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new SetRoutePrefix(new RouteAttribute(VirtualPath))));
}
}

if (RestAPIHelpers.JsonSerializerCaseSensitive())
Expand Down
Loading