Forum Discussion

DieterRogiest's avatar
DieterRogiest
Occasional Visitor
4 years ago

Improve documentation: a response with a pdf file needs schema format 'byte' to get a byte array

frantuma ,
I guess as a staff member you can maybe add to or correct these documentation pages?
Our rest api call can return a pdf file, an html file or a txt file (with content-type header "application/pdf" in case of a pdf file).
From https://swagger.io/docs/specification/data-models/data-types/#file and https://swagger.io/docs/specification/describing-responses/#response-that-returns-a-file I wrongly thought that the openapi.yml of our swaggerhub should be
schema:
type: "string"
format: "binary"
but the java code generated by a derivative of swagger-maven-plugin was then
public ResponseEntity<File> renderDocumentZonderBewarenWithHttpInfo
which made our test fail with:
UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.File] and content type [application/pdf]

The solution was on https://stackoverflow.com/questions/62794949/how-to-define-byte-array-in-openapi-3-0 :
You must set type: string and format: byte
So now with
schema:
type: "string"
format: "byte"
the codegen yields a public ResponseEntity<byte[]> method with which we can download the pdf fine.


Regards,

Dieter R.

 

Step failed
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.File] and content type [application/pdf]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:651)
at be.xxx.yyy.qa.rest.internal.v3.api.ApiClient.invokeAPI(ApiClient.java:585)