spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

Feign Form JsonFormWriter not working correctly

Open kampaii53 opened this issue 1 year ago • 3 comments

Hi. During implementation found out that default behaviour does not work correctly I am trying to send dto and byte array in one request @PostMapping(path = "/stream", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) void testStreamUpload( @RequestPart("dto") TestDto testDto, @RequestPart("file") FormData file );

though, it does not add dto correctly until I add custom configuration for Feign client:

class Configuration { @Bean public AbstractFormWriter jsonFormWriter() { return new JsonFormWriter(); } }

It starts working BUT all remaining form writers are now skipped in MultipartFormContentProcessor because JsonFormWriter becomes the only suitable writer. изображение

My possible solution is to reimplement JsonFormWriter to work only with my testDto.

full example

kampaii53 avatar Aug 12 '22 14:08 kampaii53

Hello, @kampaii53 feign-form package is not maintained by us. What we do is just add the writer you instantiate as a bean as the first writer in the hierarchy that is being processed. If you do not want to process all the inputs as JSON, you might need to create your own implementation of JsonFormWriter on top of the one provided by us where you will implement the isApplicable method to make sure only the input that matches your conditions is processed as JSON.

OlgaMaciaszek avatar Sep 05 '22 14:09 OlgaMaciaszek

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Sep 12 '22 14:09 spring-cloud-issues

Hi @OlgaMaciaszek! Sorry I confused you, I was talking about this JsonFormWriter. The issue reproduced here is me trying to send form, which contains both file and json. So, by default(without JsonFormWriter), if the file is attached, JSON is not being sent. If I add JsonFormWriter to a list of applicable writers, it overrides a byte[] writer which is an issue I'm trying to describe

kampaii53 avatar Sep 12 '22 14:09 kampaii53

@kampaii53 Can you also provide a sample multipart file that can be used to reproduce the issue?

OlgaMaciaszek avatar Feb 21 '23 14:02 OlgaMaciaszek

@kampaii53 Can you also provide a sample multipart file that can be used to reproduce the issue?

@OlgaMaciaszek Hi! I have updated example with files.

how-to:

  1. start Application
  2. start ClientApplication
  3. use postman/test-form.postman_collection.json requests(imort via postman, needed files are in the same directory)

crutch solution is described in ApplicationClient.Configuration. You need to comment jsonFormWriter bean and uncomment enhancedFormWriter. ALSO: multipart+dto is still failing even in crutched solution(file is passed, dto is not)

kampaii53 avatar Feb 26 '23 07:02 kampaii53

@kampaii53 I have updated the sample so that the Feign call gets propagated to a server-side controller so it's easier to test. I have run some tests on the updated sample. I don't really use Postman much, so I've instead just run httpie commands with the files you've provided (find in README and update file paths before running). Everything works fine for me just with the standard JsonFormWriter.

OlgaMaciaszek avatar Mar 03 '23 14:03 OlgaMaciaszek

retried and checked via tests. seems that was my fault annotating controller method with Param instead of RequestPart. sorry for taking your time

kampaii53 avatar Mar 04 '23 00:03 kampaii53

No problem, @kampaii53. I'm happy the issue was solved.

OlgaMaciaszek avatar Mar 06 '23 11:03 OlgaMaciaszek