Skip to content

Commit d5ed19f

Browse files
committed
[Dart] Update samples
1 parent f33ff15 commit d5ed19f

File tree

16 files changed

+124
-120
lines changed

16 files changed

+124
-120
lines changed

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ library openapi.api;
1313
import 'dart:async';
1414
import 'dart:convert';
1515
import 'dart:io';
16+
import 'dart:typed_data';
1617

1718
import 'package:http/http.dart';
1819
import 'package:intl/intl.dart';

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api/pet_api.dart

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class PetApi {
6262
Future<Pet?> addPet(Pet pet,) async {
6363
final response = await addPetWithHttpInfo(pet,);
6464
if (response.statusCode >= HttpStatus.badRequest) {
65-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
65+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
6666
}
6767
// When a remote server returns no body with a status of 204, we shall not decode it.
6868
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
6969
// FormatException when trying to decode an empty string.
7070
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
71-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Pet',) as Pet;
71+
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;
7272

7373
}
7474
return null;
@@ -129,7 +129,7 @@ class PetApi {
129129
Future<void> deletePet(int petId, { String? apiKey, }) async {
130130
final response = await deletePetWithHttpInfo(petId, apiKey: apiKey, );
131131
if (response.statusCode >= HttpStatus.badRequest) {
132-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
132+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
133133
}
134134
}
135135

@@ -181,14 +181,13 @@ class PetApi {
181181
Future<List<Pet>?> findPetsByStatus(List<String> status,) async {
182182
final response = await findPetsByStatusWithHttpInfo(status,);
183183
if (response.statusCode >= HttpStatus.badRequest) {
184-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
184+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
185185
}
186186
// When a remote server returns no body with a status of 204, we shall not decode it.
187187
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
188188
// FormatException when trying to decode an empty string.
189189
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
190-
final responseBody = await _decodeBodyBytes(response);
191-
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
190+
return (await apiClient.deserializeAsync(response.bodyBytes, 'List<Pet>') as List)
192191
.cast<Pet>()
193192
.toList();
194193

@@ -244,14 +243,13 @@ class PetApi {
244243
Future<List<Pet>?> findPetsByTags(List<String> tags,) async {
245244
final response = await findPetsByTagsWithHttpInfo(tags,);
246245
if (response.statusCode >= HttpStatus.badRequest) {
247-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
246+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
248247
}
249248
// When a remote server returns no body with a status of 204, we shall not decode it.
250249
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
251250
// FormatException when trying to decode an empty string.
252251
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
253-
final responseBody = await _decodeBodyBytes(response);
254-
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
252+
return (await apiClient.deserializeAsync(response.bodyBytes, 'List<Pet>') as List)
255253
.cast<Pet>()
256254
.toList();
257255

@@ -306,13 +304,13 @@ class PetApi {
306304
Future<Pet?> getPetById(int petId,) async {
307305
final response = await getPetByIdWithHttpInfo(petId,);
308306
if (response.statusCode >= HttpStatus.badRequest) {
309-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
307+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
310308
}
311309
// When a remote server returns no body with a status of 204, we shall not decode it.
312310
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
313311
// FormatException when trying to decode an empty string.
314312
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
315-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Pet',) as Pet;
313+
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;
316314

317315
}
318316
return null;
@@ -364,13 +362,13 @@ class PetApi {
364362
Future<Pet?> updatePet(Pet pet,) async {
365363
final response = await updatePetWithHttpInfo(pet,);
366364
if (response.statusCode >= HttpStatus.badRequest) {
367-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
365+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
368366
}
369367
// When a remote server returns no body with a status of 204, we shall not decode it.
370368
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
371369
// FormatException when trying to decode an empty string.
372370
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
373-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Pet',) as Pet;
371+
return await apiClient.deserializeAsync(response.bodyBytes, 'Pet',) as Pet;
374372

375373
}
376374
return null;
@@ -441,7 +439,7 @@ class PetApi {
441439
Future<void> updatePetWithForm(int petId, { String? name, String? status, }) async {
442440
final response = await updatePetWithFormWithHttpInfo(petId, name: name, status: status, );
443441
if (response.statusCode >= HttpStatus.badRequest) {
444-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
442+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
445443
}
446444
}
447445

@@ -518,13 +516,13 @@ class PetApi {
518516
Future<ApiResponse?> uploadFile(int petId, { String? additionalMetadata, MultipartFile? file, }) async {
519517
final response = await uploadFileWithHttpInfo(petId, additionalMetadata: additionalMetadata, file: file, );
520518
if (response.statusCode >= HttpStatus.badRequest) {
521-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
519+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
522520
}
523521
// When a remote server returns no body with a status of 204, we shall not decode it.
524522
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
525523
// FormatException when trying to decode an empty string.
526524
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
527-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'ApiResponse',) as ApiResponse;
525+
return await apiClient.deserializeAsync(response.bodyBytes, 'ApiResponse',) as ApiResponse;
528526

529527
}
530528
return null;

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api/store_api.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class StoreApi {
6363
Future<void> deleteOrder(String orderId,) async {
6464
final response = await deleteOrderWithHttpInfo(orderId,);
6565
if (response.statusCode >= HttpStatus.badRequest) {
66-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
66+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
6767
}
6868
}
6969

@@ -103,13 +103,13 @@ class StoreApi {
103103
Future<Map<String, int>?> getInventory() async {
104104
final response = await getInventoryWithHttpInfo();
105105
if (response.statusCode >= HttpStatus.badRequest) {
106-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
106+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
107107
}
108108
// When a remote server returns no body with a status of 204, we shall not decode it.
109109
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
110110
// FormatException when trying to decode an empty string.
111111
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
112-
return Map<String, int>.from(await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Map<String, int>'),);
112+
return Map<String, int>.from(await apiClient.deserializeAsync(response.bodyBytes, 'Map<String, int>'),);
113113

114114
}
115115
return null;
@@ -162,13 +162,13 @@ class StoreApi {
162162
Future<Order?> getOrderById(int orderId,) async {
163163
final response = await getOrderByIdWithHttpInfo(orderId,);
164164
if (response.statusCode >= HttpStatus.badRequest) {
165-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
165+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
166166
}
167167
// When a remote server returns no body with a status of 204, we shall not decode it.
168168
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
169169
// FormatException when trying to decode an empty string.
170170
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
171-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Order',) as Order;
171+
return await apiClient.deserializeAsync(response.bodyBytes, 'Order',) as Order;
172172

173173
}
174174
return null;
@@ -220,13 +220,13 @@ class StoreApi {
220220
Future<Order?> placeOrder(Order order,) async {
221221
final response = await placeOrderWithHttpInfo(order,);
222222
if (response.statusCode >= HttpStatus.badRequest) {
223-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
223+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
224224
}
225225
// When a remote server returns no body with a status of 204, we shall not decode it.
226226
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
227227
// FormatException when trying to decode an empty string.
228228
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
229-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'Order',) as Order;
229+
return await apiClient.deserializeAsync(response.bodyBytes, 'Order',) as Order;
230230

231231
}
232232
return null;

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api/user_api.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class UserApi {
6262
Future<void> createUser(User user,) async {
6363
final response = await createUserWithHttpInfo(user,);
6464
if (response.statusCode >= HttpStatus.badRequest) {
65-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
65+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
6666
}
6767
}
6868

@@ -112,7 +112,7 @@ class UserApi {
112112
Future<void> createUsersWithArrayInput(List<User> user,) async {
113113
final response = await createUsersWithArrayInputWithHttpInfo(user,);
114114
if (response.statusCode >= HttpStatus.badRequest) {
115-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
115+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
116116
}
117117
}
118118

@@ -162,7 +162,7 @@ class UserApi {
162162
Future<void> createUsersWithListInput(List<User> user,) async {
163163
final response = await createUsersWithListInputWithHttpInfo(user,);
164164
if (response.statusCode >= HttpStatus.badRequest) {
165-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
165+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
166166
}
167167
}
168168

@@ -213,7 +213,7 @@ class UserApi {
213213
Future<void> deleteUser(String username,) async {
214214
final response = await deleteUserWithHttpInfo(username,);
215215
if (response.statusCode >= HttpStatus.badRequest) {
216-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
216+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
217217
}
218218
}
219219

@@ -264,13 +264,13 @@ class UserApi {
264264
Future<User?> getUserByName(String username,) async {
265265
final response = await getUserByNameWithHttpInfo(username,);
266266
if (response.statusCode >= HttpStatus.badRequest) {
267-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
267+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
268268
}
269269
// When a remote server returns no body with a status of 204, we shall not decode it.
270270
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
271271
// FormatException when trying to decode an empty string.
272272
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
273-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'User',) as User;
273+
return await apiClient.deserializeAsync(response.bodyBytes, 'User',) as User;
274274

275275
}
276276
return null;
@@ -331,13 +331,13 @@ class UserApi {
331331
Future<String?> loginUser(String username, String password,) async {
332332
final response = await loginUserWithHttpInfo(username, password,);
333333
if (response.statusCode >= HttpStatus.badRequest) {
334-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
334+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
335335
}
336336
// When a remote server returns no body with a status of 204, we shall not decode it.
337337
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
338338
// FormatException when trying to decode an empty string.
339339
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
340-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'String',) as String;
340+
return await apiClient.deserializeAsync(response.bodyBytes, 'String',) as String;
341341

342342
}
343343
return null;
@@ -379,7 +379,7 @@ class UserApi {
379379
Future<void> logoutUser() async {
380380
final response = await logoutUserWithHttpInfo();
381381
if (response.statusCode >= HttpStatus.badRequest) {
382-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
382+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
383383
}
384384
}
385385

@@ -436,7 +436,7 @@ class UserApi {
436436
Future<void> updateUser(String username, User user,) async {
437437
final response = await updateUserWithHttpInfo(username, user,);
438438
if (response.statusCode >= HttpStatus.badRequest) {
439-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
439+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
440440
}
441441
}
442442
}

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,25 @@ class ApiClient {
143143
);
144144
}
145145

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

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

155+
// if the expected target type is a byte-array, then just return the response bytes as-is
156+
if (targetType == 'Uint8List') {
157+
return responseBytes;
158+
}
159+
160+
final decodedString = const Utf8Decoder().convert(responseBytes);
155161
// If the expected target type is String, nothing to do...
156162
return targetType == 'String'
157-
? json
158-
: _deserialize(jsonDecode(json), targetType, growable: growable);
163+
? decodedString
164+
: _deserialize(jsonDecode(decodedString), targetType, growable: growable);
159165
}
160166

161167
// ignore: deprecated_member_use_from_same_package

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_helper.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ String parameterToString(dynamic value) {
5858
return value.toString();
5959
}
6060

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

7067
/// Returns a valid [T] value found at the specified Map [key], null otherwise.

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ library openapi.api;
1313
import 'dart:async';
1414
import 'dart:convert';
1515
import 'dart:io';
16+
import 'dart:typed_data';
1617

1718
import 'package:http/http.dart';
1819
import 'package:intl/intl.dart';

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api/another_fake_api.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class AnotherFakeApi {
6262
Future<ModelClient?> call123testSpecialTags(ModelClient modelClient,) async {
6363
final response = await call123testSpecialTagsWithHttpInfo(modelClient,);
6464
if (response.statusCode >= HttpStatus.badRequest) {
65-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
65+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
6666
}
6767
// When a remote server returns no body with a status of 204, we shall not decode it.
6868
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
6969
// FormatException when trying to decode an empty string.
7070
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
71-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'ModelClient',) as ModelClient;
71+
return await apiClient.deserializeAsync(response.bodyBytes, 'ModelClient',) as ModelClient;
7272

7373
}
7474
return null;

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api/default_api.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ class DefaultApi {
4545
Future<FooGetDefaultResponse?> fooGet() async {
4646
final response = await fooGetWithHttpInfo();
4747
if (response.statusCode >= HttpStatus.badRequest) {
48-
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
48+
throw ApiException(response.statusCode, decodeError(await response.bodyBytes));
4949
}
5050
// When a remote server returns no body with a status of 204, we shall not decode it.
5151
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
5252
// FormatException when trying to decode an empty string.
5353
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
54-
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'FooGetDefaultResponse',) as FooGetDefaultResponse;
54+
return await apiClient.deserializeAsync(response.bodyBytes, 'FooGetDefaultResponse',) as FooGetDefaultResponse;
5555

5656
}
5757
return null;

0 commit comments

Comments
 (0)