Created by: 0xNF
Summary
tl;dr UInt8List
support, which Dart-Dio has, has been added to the regular Dart2 generator.
Fixes #12161.
Details
Changes to the Generators:
To support UInt8List in the generator, Typed Data imports had to be added. Additionally, some OpenAPI data types have been switched to Uint8List.
- [AbstractDartCodeGen.java]: added
Uint8List/dart:typed_data
to the imports. - [AbstractDartCodeGen.java]: Changed
typeMapping
of {file, binary} toUint8List
away fromMultiPartFile
- [AbstractDartCodeGen.java]: Copied Multipart/Uint8list differentiation in
postProcessOperationsWithModels
from DartDioClientCodegen.java - [dart2/apilib.mustache]: Added
import 'dart:typed_data';
Changes to the API classes
To support Uint8List in the generated API output, parts of the *api.dart files had to be refactored. Presently Dart2 relies on passing utf8 decoded response byte strings through its API calls, but this leads to errors because not all response objects are json strings. To handle this, I refactored many of the calls to take Uint8List's straight from response.bodyBytes
. Decoding into a String
object doesn't happen until it needs to, which is right before passing the object down into the Json Decoding section.
- [api_client.dart]: deserialize and deserializeAsync now take Uint8List as their default input
- [api_helper.dart]: removed _decodeBodyBytes in favor of just using
response.bodyBytes
- [api_helper.dart]: added
decodeError
method to deal with the byte response, which decodes the resposne into a utf-8 String.
To confirm this change, use the following specfile:
specfile
openapi: 3.0.3
info:
version: "1.1"
title: Dart Uint8list Demo
servers:
- url: "localhost"
variables:
host:
default: localhost
paths:
/getData:
get:
operationId: GetData
description: "Should return a Uint8List of bytes"
responses:
"200":
description: Binary data
content:
application/octet-stream:
schema:
type: string
format: binary
put:
operationId: PutData
description: "Should push a Multipart File"
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
fileContent:
type: string
format: binary
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
/item:
get:
operationId: GetItem
description: "Should return an Item"
responses:
"200":
description: items
content:
application/json:
schema:
$ref: "#components/schemas/item"
components:
schemas:
Item:
type: object
description: "Some json thing"
required:
- intField
- doubleField
- stringField
- dateField
- timeField
- dateTimeField
- boolField
- b64Field
- intArrayField
properties:
intField:
type: integer
format: int32
doubleField:
type: number
format: float
stringField:
type: string
dateField:
type: string
format: date
timeField:
type: string
format: time
dateTimeField:
type: string
format: date-time
boolField:
type: boolean
b64Field:
type: string
format: byte
intArrayField:
type: array
items:
type: integer
format: int32
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: -
File the PR against the correct branch: master
(6.1.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks) -
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@jaumard @josh-burton @amondnet @sbu-WBT @kuhnroyal @agilob @ahmednfwela