NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

FromForm FormData nullability doesn't handled correctly

Open DreadfulBot opened this issue 1 year ago • 3 comments

Hi, community :)

I have an api endpoint, which receives [FromForm] parameter:

    [HttpPost]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UpdateCanvasResponse))]
    public async Task<IActionResult> UpdateCanvas([FromForm] UpdateCanvasCommand command)
    {...

UpdateCanvasCommand has all optional (nullable) fields, except ID:

public class UpdateCanvasCommand : IRequest<ErrorOr<UpdateCanvasResponse>>
{
    public int Id { get; set; }

    // [JsonProperty(Required = Required.AllowNull, NullValueHandling = NullValueHandling.Include)]
    public string? Name { get; set; }

    public IFormFile? Icon { get; set; }
    public IEnumerable<string>? RemoveImages { get; set; }
    public bool? Publish { get; set; }
    public int? Areas { get; set; }
}

In nswag generated code I receive kinda strange checks, which don't allow me to pass null/undefined values for those optional fields:

updateCanvas(id: number | undefined, name: string | undefined, icon: FileParameter | undefined, removeImages: string[] | undefined, publish: boolean | undefined, areas: number | undefined, cancelToken?: CancelToken | undefined): Promise<UpdateCanvasResponse> {
        let url_ = this.baseUrl + "/api/Canvas/UpdateCanvas";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = new FormData();
        if (id === null || id === undefined)
            throw new Error("The parameter 'id' cannot be null.");
        else
            content_.append("Id", id.toString());
        if (name === null || name === undefined)
            throw new Error("The parameter 'name' cannot be null.");
        else
            content_.append("Name", name.toString());
        if (icon === null || icon === undefined)
            throw new Error("The parameter 'icon' cannot be null.");
        else

Attempts to fix:

  • I've tried to add JsonProperty attribute - not effect
  • SupportNonNullableReferenceTypes, UseAllOfToExtendReferenceSchemas - no effect

Thanks for future advices!

DreadfulBot avatar Oct 24 '23 03:10 DreadfulBot