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

Revert "BP-761: Validation exception when sort by or sort direction are empty in the request." #58

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

msljivic
Copy link
Contributor

@msljivic msljivic commented Nov 29, 2023

Should we revert #57?

Issue

Non-nullable reference fields are marked as Required by default.

Issue was suppressed on projects by setting the SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true

This suppress was not added to Blueprint template.

Request:

/Users?PageNumber=1&PageSize=20&SortBy=emailAddress&SortDirection=

[PublicAPI]
public class Request : PagedRequest<Response.Item>
{
    public string? Name { get; set; }
}

Error:

{
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-22b69d3187027b178a5caff39b13172c-87bf17a84c5c6f2b-00",
    "errors": {
        "SortDirection": [
            "The SortDirection field is required."
        ]
    }
}

Exception occurs when query params in HTTP request contains empty/missing value sortBy/sortDirection.

Option 1: Make SortBy and SortDirection nullable fields.

Introduced by: #57

Potential breaking changes?

After packages are updated in a project, if SortBy or SortDirection are used directly in code e.g. var isPriceWithVatSort = request.SortBy.Equals("unitPriceWithVat") -> this would lead to compile time warning CS8602 Dereference of a possibly null reference

Option 2: SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true

Testing behavior of model validation and model binding, example:

[PublicAPI]
public class Request : PagedRequest<Response.Item>
{
    public string Name { get; set; } = String.Empty;
    public string? Email { get; set; } = String.Empty;
}

False (default): Non-nullable reference fields are marked as Required by default.

For request /Users?Name=&PageNumber=1&PageSize=20

public string Name { get; set; } = String.Empty; => Validation error 400: Name is required
public string? Name { get; set; } => OK: Model binding will set Name = null
public string? Name { get; set; } = String.Empty; => OK: Model binding will set Name = null (!?)
public string? Email { get; set; } = String.Empty; => OK: Model binding will set Email = "";

True: This required behavior is suppressed

For request /Users?Name=&PageNumber=1&PageSize=20

public string Name { get; set; } = String.Empty; => OK: Model binding will set Name = null (!!? seems hazardous)
public string? Name { get; set; } => OK: Model binding will set Name = null
public string? Name { get; set; } = String.Empty; => OK: Model binding will set Name = null (!?)
public string? Email { get; set; } = String.Empty; => OK: Model binding will set Email = "";

@vsljivic
Copy link
Contributor

vsljivic commented Nov 30, 2023

Should we revert #57?

In my view this is not a breaking change and it should not be reverted.
Paging request can come without SortBy and SortDirection and model binding would set these fields to null.
We just made it visible now, i.e. that these fields can be null.

Copy link
Contributor

@zsrdjan zsrdjan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root of the problem comes from how we construct api call from Angular to the Backend. The MVC erorr is valid. Non Null property is also valid. The problem is in the PagedQuery (https://github.com/enigmatry/entry-angular-building-blocks/pull/173/files). NSWag client does not have this problem since it is correctly constructing url.

E.g.
image

Please revert this since the alternative fix has been created which does not introduce breaking changes, nor requires changing default MVC setup (i.e. turning off SuppressImplicitRequiredAttributeForNonNullableReferenceTypes).

@msljivic msljivic merged commit f07b176 into master Dec 1, 2023
4 of 5 checks passed
@msljivic msljivic deleted the revert-57-features/BP-761 branch December 1, 2023 12:57
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

Successfully merging this pull request may close these issues.

3 participants