2

I have an Entity that contain some images and I want to update it using HttpPatch method.

To create a new Style I use this method:

[HttpPost]
public async Task<IActionResult> CreateStyleAsync([FromForm] StyleFiles styleFiles, [FromForm] StyleDTO style)

Now I'm trying to create a method to update this Style using the HttpPatch method. I tried this but there is no option to upload the file on Swagger:

[HttpPatch("{styleId}")]
public async Task<IActionResult> PatchStyleAsync([FromForm] StyleFiles styleFiles, Guid styleId, [FromBody] JsonPatchDocument styleDto)

This is what I see on Swagger: Patch method on Swagger

This is the DTO:

public class StyleDTO
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string PreviewImage { get; set; }
}

And this the StyleFiles class:

public class StyleFiles
{
    public IFormFile Image { get; set; }
}

I'm thinking to make two separate endpoints, one for updating the File and another one for updating the entity itself. But I would like to not do that.

Karney.
  • 4,803
  • 2
  • 7
  • 11
  • I wouldn't expect swaggerui to render anything for uploading a file, regardless the http method. Have you considered using a more advanced rest tool such as [postman](https://stackoverflow.com/questions/39037049/how-to-upload-a-file-and-json-data-in-postman)? – Caius Jard Mar 03 '21 at 22:55

1 Answers1

1

[FromForm] and [FromBody] cannot be used as a parameter at the same time. Because their content-type is different. [FromForm] will serialize the values in the form, but [FromBody] pass the json data. Therefore, you have to make two separate endpoints.

I don't know whether there is a conflict between StyleFiles and StyleDTO. You can also create separate class to integrate them.

public class ViewModel
{
    public StyleDTO styleDTO { get; set; }
    public StyleFiles styleFiles { get; set; }
}

controller

    [HttpPatch("{styleId}")]
    public async Task<IActionResult> PatchStyleAsync([FromForm] ViewModel viewModel)
    {
        return Ok();
    }

enter image description here

Karney.
  • 4,803
  • 2
  • 7
  • 11
  • That approach does not allow partial updates using JsonPatchDocument. ` { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] } ` I will have to go with two different endpoints – Javier Dombronsky Mar 04 '21 at 11:25