Base solution for your next web application
Open Closed

nwsag does not consider nullable parameters #12328


User avatar
0
Hammer created

Hello, I'm running ASP Net Zero dot new core + angular 13.0
nswag does not appeat to take optional fields into consideration when creating the proxy services.

public class GetAllContractsInput : PagedAndSortedResultRequestDto
{
    public string Filter { get; set; }

    public long? MaxNumberFilter { get; set; }
    public long? MinNumberFilter { get; set; }

    public DateTime? MaxSignedFilter { get; set; }
    public DateTime? MinSignedFilter { get; set; }

    public int? StatusFilter { get; set; }

    public string CustomerLastNameFilter { get; set; }

    public string DocumentURLFilter { get; set; }

}

Results in :

/**
     * @param filter (optional) 
     * @param maxNumberFilter (optional) 
     * @param minNumberFilter (optional) 
     * @param maxSignedFilter (optional) 
     * @param minSignedFilter (optional) 
     * @param statusFilter (optional) 
     * @param customerLastNameFilter (optional) 
     * @param documentURLFilter (optional) 
     * @param sorting (optional) 
     * @param skipCount (optional) 
     * @param maxResultCount (optional) 
     * @return Success
     */
    getAll(filter: string | undefined, maxNumberFilter: number | undefined, minNumberFilter: number | undefined, maxSignedFilter: DateTime | undefined, minSignedFilter: DateTime | undefined, statusFilter: number | undefined, customerLastNameFilter: string | undefined, documentURLFilter: string | undefined, sorting: string | undefined, skipCount: number | undefined, maxResultCount: number | undefined): Observable {
        let url_ = this.baseUrl + "/api/services/app/Contracts/GetAll?";
        if (filter === null)
            throw new Error("The parameter 'filter' cannot be null.");
        else if (filter !== undefined)
            url_ += "Filter=" + encodeURIComponent("" + filter) + "&";
        if (maxNumberFilter === null)
            throw new Error("The parameter 'maxNumberFilter' cannot be null.");
        else if (maxNumberFilter !== undefined)
            url_ += "MaxNumberFilter=" + encodeURIComponent("" + maxNumberFilter) + "&";
        if (minNumberFilter === null)
            throw new Error("The parameter 'minNumberFilter' cannot be null.");
        else if (minNumberFilter !== undefined)
            url_ += "MinNumberFilter=" + encodeURIComponent("" + minNumberFilter) + "&";
        if (maxSignedFilter === null)
            throw new Error("The parameter 'maxSignedFilter' cannot be null.");
        else if (maxSignedFilter !== undefined)
            url_ += "MaxSignedFilter=" + encodeURIComponent(maxSignedFilter ? "" + maxSignedFilter.toString() : "") + "&";
        if (minSignedFilter === null)
            throw new Error("The parameter 'minSignedFilter' cannot be null.");
        else if (minSignedFilter !== undefined)
            url_ += "MinSignedFilter=" + encodeURIComponent(minSignedFilter ? "" + minSignedFilter.toString() : "") + "&";
        if (statusFilter === null)
            throw new Error("The parameter 'statusFilter' cannot be null.");
        else if (statusFilter !== undefined)
            url_ += "StatusFilter=" + encodeURIComponent("" + statusFilter) + "&";
        if (customerLastNameFilter === null)
            throw new Error("The parameter 'customerLastNameFilter' cannot be null.");
        else if (customerLastNameFilter !== undefined)
            url_ += "CustomerLastNameFilter=" + encodeURIComponent("" + customerLastNameFilter) + "&";
        if (documentURLFilter === null)
            throw new Error("The parameter 'documentURLFilter' cannot be null.");
        else if (documentURLFilter !== undefined)
            url_ += "DocumentURLFilter=" + encodeURIComponent("" + documentURLFilter) + "&";
        if (sorting === null)
            throw new Error("The parameter 'sorting' cannot be null.");
        else if (sorting !== undefined)
            url_ += "Sorting=" + encodeURIComponent("" + sorting) + "&";
        if (skipCount === null)
            throw new Error("The parameter 'skipCount' cannot be null.");
        else if (skipCount !== undefined)
            url_ += "SkipCount=" + encodeURIComponent("" + skipCount) + "&";
        if (maxResultCount === null)
            throw new Error("The parameter 'maxResultCount' cannot be null.");
        else if (maxResultCount !== undefined)
            url_ += "MaxResultCount=" + encodeURIComponent("" + maxResultCount) + "&";

I would expect nullable parameters to be considered optional in the service proxy.

I see a similar problem face faced previously (https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1528), but it appears to have reappeared.


1 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Hammer

    Would you apply the changes made in this pr? If you set the "generateOptionalParameters" value to true in the nswag config file, the optional parameters will be marked nullable.