Bug Report Checklist
Description
An endpoint which returns binary data defined as
schema:
type: string
format: binary
Will fail in the following ways:
- Declare the function's return type to be
MultipartFile, which is only really valid for file upload, not for file download.
- Attempt to read the response data via the
Response.Body field, which defaults to attempting to deserialize as a string. For true binary data, this isn't valid. This will throw an exception at runtime.
As a result, true binary data response endpoints (as in, not b64 encoded responses), are unrepresentable in the current Dart generator.
see also: https://swagger.io/docs/specification/data-models/data-types/#file
openapi-generator version
6.0.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
version: "1.1"
title: Dart Uint8list Demo
servers:
- url: 'localhost'
variables:
host:
default: localhost
paths:
/item:
get:
operationId: GetItem
responses:
"200":
description: Binary data
content:
application/octet-stream:
schema:
type: string
format: binary
The generated method looks like this:
Future<MultipartFile?> getItem() async {
final response = await getItemWithHttpInfo();
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
}
// When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'MultipartFile',) as MultipartFile;
}
return null;
}
Steps to reproduce
java -jar openapi-generator-cli.jar generate -i ./spec.yaml -g dart -o uint
- use the getItem() method
Related issues/PRs
#8878
Suggest a fix
- Response types of
application/octet-stream and schema declarations of type: string, format: binary should use the UInt8List datatype, not the MultipartFile. Request types should remain unchanged.
- Responses should be deserialized with
request.bodyBytes, not request.Body, to avoid decoding exceptions
It also looks like the dart-dio-next generator handles this fine -- using -g dart-dio-next I get Response<Uint8List> instead of Response<MultipartFile>
Bug Report Checklist
Description
An endpoint which returns binary data defined as
Will fail in the following ways:
MultipartFile, which is only really valid for file upload, not for file download.Response.Bodyfield, which defaults to attempting to deserialize as a string. For true binary data, this isn't valid. This will throw an exception at runtime.As a result, true binary data response endpoints (as in, not b64 encoded responses), are unrepresentable in the current Dart generator.
see also: https://swagger.io/docs/specification/data-models/data-types/#file
openapi-generator version
6.0.0
OpenAPI declaration file content or url
The generated method looks like this:
Steps to reproduce
java -jar openapi-generator-cli.jar generate -i ./spec.yaml -g dart -o uintRelated issues/PRs
#8878
Suggest a fix
application/octet-streamand schema declarations oftype: string, format: binaryshould use theUInt8Listdatatype, not the MultipartFile. Request types should remain unchanged.request.bodyBytes, notrequest.Body, to avoid decoding exceptionsIt also looks like the dart-dio-next generator handles this fine -- using
-g dart-dio-nextI getResponse<Uint8List>instead ofResponse<MultipartFile>