Skip to content

[Dart] Add UInt8List support, allowing for type:string format:binary definitions #13451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class {{{classname}}} {
Future<{{#returnType}}{{{.}}}?{{/returnType}}{{^returnType}}void{{/returnType}}> {{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}}? {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
{{#returnType}}
// When a remote server returns no body with a status of 204, we shall not decode it.
Expand All @@ -173,17 +173,16 @@ class {{{classname}}} {
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
{{#native_serialization}}
{{#isArray}}
final responseBody = await _decodeBodyBytes(response);
return (await apiClient.deserializeAsync(responseBody, '{{{returnType}}}') as List)
return (await apiClient.deserializeAsync(response.bodyBytes, '{{{returnType}}}') as List)
.cast<{{{returnBaseType}}}>()
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(){{/uniqueItems}};
{{/isArray}}
{{^isArray}}
{{#isMap}}
return {{{returnType}}}.from(await apiClient.deserializeAsync(await _decodeBodyBytes(response), '{{{returnType}}}'),);
return {{{returnType}}}.from(await apiClient.deserializeAsync(response.bodyBytes, '{{{returnType}}}'),);
{{/isMap}}
{{^isMap}}
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), '{{{returnType}}}',) as {{{returnType}}};
return await apiClient.deserializeAsync(response.bodyBytes, '{{{returnType}}}',) as {{{returnType}}};
{{/isMap}}{{/isArray}}{{/native_serialization}}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,25 @@ class ApiClient {
}
{{#native_serialization}}

Future<dynamic> deserializeAsync(String json, String targetType, {bool growable = false,}) async =>
Future<dynamic> deserializeAsync(Uint8List responseBytes, String targetType, {bool growable = false,}) async =>
// ignore: deprecated_member_use_from_same_package
deserialize(json, targetType, growable: growable);
deserialize(responseBytes, targetType, growable: growable);

@Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use deserializeAsync() instead.')
dynamic deserialize(String json, String targetType, {bool growable = false,}) {
dynamic deserialize(Uint8List responseBytes, String targetType, {bool growable = false,}) {
// Remove all spaces. Necessary for regular expressions as well.
targetType = targetType.replaceAll(' ', ''); // ignore: parameter_assignments

// if the expected target type is a byte-array, then just return the response bytes as-is
if (targetType == 'Uint8List') {
return responseBytes;
}

final decodedString = const Utf8Decoder().convert(responseBytes);
// If the expected target type is String, nothing to do...
return targetType == 'String'
? json
: _deserialize(jsonDecode(json), targetType, growable: growable);
? decodedString
: _deserialize(jsonDecode(decodedString), targetType, growable: growable);
}
{{/native_serialization}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ String parameterToString(dynamic value) {
return value.toString();
}

/// Returns the decoded body as UTF-8 if the given headers indicate an 'application/json'
/// content type. Otherwise, returns the decoded body as decoded by dart:http package.
Future<String> _decodeBodyBytes(Response response) async {
final contentType = response.headers['content-type'];
return contentType != null && contentType.toLowerCase().startsWith('application/json')
? response.bodyBytes.isEmpty ? '' : utf8.decode(response.bodyBytes)
: response.body;
/// Decodes the response body for error reporting purposes.
/// Decodes as UTF-8 but allows for malformed bytes to avoid crashing.
String decodeError(Uint8List responseBytes) {
return const Utf8Decoder(allowMalformed: true).convert(responseBytes);
}

/// Returns a valid [T] value found at the specified Map [key], null otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart';
import 'package:intl/intl.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ library openapi.api;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart';
import 'package:intl/intl.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ class PetApi {
Future<Pet?> addPet(Pet pet,) async {
final response = await addPetWithHttpInfo(pet,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'Pet',) as Pet;
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;

}
return null;
Expand Down Expand Up @@ -129,7 +129,7 @@ class PetApi {
Future<void> deletePet(int petId, { String? apiKey, }) async {
final response = await deletePetWithHttpInfo(petId, apiKey: apiKey, );
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -181,14 +181,13 @@ class PetApi {
Future<List<Pet>?> findPetsByStatus(List<String> status,) async {
final response = await findPetsByStatusWithHttpInfo(status,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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) {
final responseBody = await _decodeBodyBytes(response);
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
return (await apiClient.deserializeAsync(response.bodyBytes, 'List<Pet>') as List)
.cast<Pet>()
.toList();

Expand Down Expand Up @@ -244,14 +243,13 @@ class PetApi {
Future<List<Pet>?> findPetsByTags(List<String> tags,) async {
final response = await findPetsByTagsWithHttpInfo(tags,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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) {
final responseBody = await _decodeBodyBytes(response);
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
return (await apiClient.deserializeAsync(response.bodyBytes, 'List<Pet>') as List)
.cast<Pet>()
.toList();

Expand Down Expand Up @@ -306,13 +304,13 @@ class PetApi {
Future<Pet?> getPetById(int petId,) async {
final response = await getPetByIdWithHttpInfo(petId,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'Pet',) as Pet;
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;

}
return null;
Expand Down Expand Up @@ -364,13 +362,13 @@ class PetApi {
Future<Pet?> updatePet(Pet pet,) async {
final response = await updatePetWithHttpInfo(pet,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'Pet',) as Pet;
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;

}
return null;
Expand Down Expand Up @@ -441,7 +439,7 @@ class PetApi {
Future<void> updatePetWithForm(int petId, { String? name, String? status, }) async {
final response = await updatePetWithFormWithHttpInfo(petId, name: name, status: status, );
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -518,13 +516,13 @@ class PetApi {
Future<ApiResponse?> uploadFile(int petId, { String? additionalMetadata, MultipartFile? file, }) async {
final response = await uploadFileWithHttpInfo(petId, additionalMetadata: additionalMetadata, file: file, );
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'ApiResponse',) as ApiResponse;
return await apiClient.deserializeAsync(response.bodyBytes, 'ApiResponse',) as ApiResponse;

}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class StoreApi {
Future<void> deleteOrder(String orderId,) async {
final response = await deleteOrderWithHttpInfo(orderId,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -103,13 +103,13 @@ class StoreApi {
Future<Map<String, int>?> getInventory() async {
final response = await getInventoryWithHttpInfo();
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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 Map<String, int>.from(await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Map<String, int>'),);
return Map<String, int>.from(await apiClient.deserializeAsync(response.bodyBytes, 'Map<String, int>'),);

}
return null;
Expand Down Expand Up @@ -162,13 +162,13 @@ class StoreApi {
Future<Order?> getOrderById(int orderId,) async {
final response = await getOrderByIdWithHttpInfo(orderId,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'Order',) as Order;
return await apiClient.deserializeAsync(response.bodyBytes, 'Order',) as Order;

}
return null;
Expand Down Expand Up @@ -220,13 +220,13 @@ class StoreApi {
Future<Order?> placeOrder(Order order,) async {
final response = await placeOrderWithHttpInfo(order,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'Order',) as Order;
return await apiClient.deserializeAsync(response.bodyBytes, 'Order',) as Order;

}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class UserApi {
Future<void> createUser(User user,) async {
final response = await createUserWithHttpInfo(user,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ class UserApi {
Future<void> createUsersWithArrayInput(List<User> user,) async {
final response = await createUsersWithArrayInputWithHttpInfo(user,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -162,7 +162,7 @@ class UserApi {
Future<void> createUsersWithListInput(List<User> user,) async {
final response = await createUsersWithListInputWithHttpInfo(user,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ class UserApi {
Future<void> deleteUser(String username,) async {
final response = await deleteUserWithHttpInfo(username,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -264,13 +264,13 @@ class UserApi {
Future<User?> getUserByName(String username,) async {
final response = await getUserByNameWithHttpInfo(username,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'User',) as User;
return await apiClient.deserializeAsync(response.bodyBytes, 'User',) as User;

}
return null;
Expand Down Expand Up @@ -331,13 +331,13 @@ class UserApi {
Future<String?> loginUser(String username, String password,) async {
final response = await loginUserWithHttpInfo(username, password,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
// 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), 'String',) as String;
return await apiClient.deserializeAsync(response.bodyBytes, 'String',) as String;

}
return null;
Expand Down Expand Up @@ -379,7 +379,7 @@ class UserApi {
Future<void> logoutUser() async {
final response = await logoutUserWithHttpInfo();
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}

Expand Down Expand Up @@ -436,7 +436,7 @@ class UserApi {
Future<void> updateUser(String username, User user,) async {
final response = await updateUserWithHttpInfo(username, user,);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
}
}
}
Loading