Ask a Question

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

DieterRogiest
Occasional Visitor

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)

1 REPLY 1
ponelat
Staff

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

I could be mistaken, but I believe your original thinking was correct.

The specification clearly says that `format: byte` is for base64 encoded data, https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types. Which i personally wouldn't interpret as byte[] in Java. Something is fishy here.

 

But maybe @frantuma knows more.

cancel
Showing results for 
Search instead for 
Did you mean: