Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Calling API in Swagger/UI acts differently than when calling from unit test #11719


User avatar
0
JapNolt created

The question centers around the Range attribute on the property MaxResultCount. I want to be able to "customize" this MaxResultCount property per edition/tenant. I plan to do this using a feature.

I used the keyword new to hide the base class property and cover it with the new property declared here. I used the ICustomValidate interface to implement my own custom feature lookup and validation logic. See below.

public class GetWrapupCallsInputBase : PagedAndSortedInputDto, IShouldNormalize, ICustomValidate
{
    // used the keyword new to hide the base class property and cover it with the new property declared here.
    new public int MaxResultCount { get; set; }

    public void AddValidationErrors(CustomValidationContext context)
    {
        new ExportRowCountValidator(context, MaxResultCount, AppConsts.WrapupDetailsRowCountFeatureKey).Validate();
    }
// ...
}

Here's the validate logic.

public void Validate()
{
    var featureChecker = _context.IocResolver.Resolve<IFeatureChecker>();
    int limit = 1000;

    if (featureChecker.IsEnabled(AppConsts.EnableExportRowCountFeatureKey))
    {
        limit = featureChecker.GetValue(_field).To<int>();
    }

    if (_maxResultCount < 1 || _maxResultCount > limit)
    {
        var message = _context.Localize(O365ContactCenterConsts.LocalizationSourceName, "ExportRowCountError");
        message = string.Format(message, _maxResultCount, 1, limit);
        _context.Results.Add(new ValidationResult(message));
    }
}

My problem is that the behavior is different when calling the API from Swagger/UI versus from unit tests. When I took this code, launched the program and tried it out in the UI, everything worked perfectly. I was able to log in as the Host Admin user and customize the Feature values for the WrapupDetailsRowCountFeatureKey. Then, when I tested requesting values from the API, I was able to exceed the AspNetZero default value of 1000 that is hard-coded into the base class's MaxResultCount Range attribute value, but my Validate code perfectly threw the error that I wanted it to when I exceeded my own custom maximum value.

On the other hand, when I tried this in unit tests, my tests failed. When I used the debugger to step through it, I realized that it was correctly running the Validate logic that my covering property was supposed to have, and it passed through the method with no errors, the requested row count value was within the range specified by the custom feature value. But as soon as control left my code, the AspNetZero attribute validation (which I'm unable to step through in the debugger, since it's not actually in my project) threw the error that I was trying to override -- "You may not have a maximum value outside the range 1 to 1000."


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @japnolt

    This seems like a strange problem, could you create an issue on https://github.com/aspnetzero/aspnet-zero-core ? We will check this problem and fix it.

  • User Avatar
    0
    JapNolt created

    Github issue: https://github.com/aspnetzero/aspnet-zero-core/issues/5009

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks, we will check it.