diff --git a/bin/configs/dart-dio-binary-response.yaml b/bin/configs/dart-dio-binary-response.yaml new file mode 100644 index 00000000000..30ad33a740c --- /dev/null +++ b/bin/configs/dart-dio-binary-response.yaml @@ -0,0 +1,8 @@ +generatorName: dart-dio +outputDir: samples/openapi3/client/petstore/dart-dio/binary_response +inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml +templateDir: modules/openapi-generator/src/main/resources/dart/libraries/dio +additionalProperties: + hideGenerationTimestamp: "true" + enumUnknownDefaultCase: "true" + serializationLibrary: "json_serializable" diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java index 81414c5e03b..cf8338e47e3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java @@ -670,6 +670,17 @@ public class DartDioClientCodegen extends AbstractDartCodegen { op.imports.remove("Uint8List"); } + if (SERIALIZATION_LIBRARY_JSON_SERIALIZABLE.equals(library)) { + // built_value serialization uses Uint8List for all MultipartFile types + // in json_serialization, MultipartFile is used as the file parameter type, but + // MultipartFile isn't readable, instead we convert this to a Uin8List + if (op.isResponseFile) { + op.imports.add("Uint8List"); + op.returnType = "Uint8List"; + op.returnBaseType = "Uint8List"; + } + } + resultImports.addAll(rewriteImports(op.imports, false)); if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) { diff --git a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/deserialize.mustache b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/deserialize.mustache index f8666920780..d2762e131a8 100644 --- a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/deserialize.mustache +++ b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/deserialize.mustache @@ -1,2 +1,7 @@ final rawData = _response.data; -_responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true); \ No newline at end of file +{{#isResponseFile}} +_responseData = rawData == null ? null : rawData as {{{returnType}}}; +{{/isResponseFile}} +{{^isResponseFile}} +_responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true); +{{/isResponseFile}} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml new file mode 100644 index 00000000000..9e7b65142bb --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml @@ -0,0 +1,16 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: Binary response +paths: + /limits: + get: + operationId: binaryResponse + responses: + '200': + description: OK + content: + application/zip: + schema: + type: string + format: binary \ No newline at end of file diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/.gitignore b/samples/openapi3/client/petstore/dart-dio/binary_response/.gitignore new file mode 100644 index 00000000000..4298cdcbd1a --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/.gitignore @@ -0,0 +1,41 @@ +# See https://dart.dev/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.buildlog +.packages +.project +.pub/ +build/ +**/packages/ + +# Files created by dart2js +# (Most Dart developers will use pub build to compile Dart, use/modify these +# rules if you intend to use dart2js directly +# Convention is to use extension '.dart.js' for Dart compiled to Javascript to +# differentiate from explicit Javascript files) +*.dart.js +*.part.js +*.js.deps +*.js.map +*.info.json + +# Directory created by dartdoc +doc/api/ + +# Don't commit pubspec lock file +# (Library packages only! Remove pattern if developing an application package) +pubspec.lock + +# Don’t commit files and directories created by other development environments. +# For example, if your development environment creates any of the following files, +# consider putting them in a global ignore file: + +# IntelliJ +*.iml +*.ipr +*.iws +.idea/ + +# Mac +.DS_Store diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator-ignore b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator-ignore new file mode 100644 index 00000000000..7484ee590a3 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator-ignore @@ -0,0 +1,23 @@ +# OpenAPI Generator Ignore +# Generated by openapi-generator https://github.com/openapitools/openapi-generator + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/FILES b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/FILES new file mode 100644 index 00000000000..809d2862097 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/FILES @@ -0,0 +1,15 @@ +.gitignore +README.md +analysis_options.yaml +build.yaml +doc/DefaultApi.md +lib/openapi.dart +lib/src/api.dart +lib/src/api/default_api.dart +lib/src/auth/api_key_auth.dart +lib/src/auth/auth.dart +lib/src/auth/basic_auth.dart +lib/src/auth/bearer_auth.dart +lib/src/auth/oauth.dart +lib/src/deserialize.dart +pubspec.yaml diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/VERSION b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/VERSION new file mode 100644 index 00000000000..4c631cf217a --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/VERSION @@ -0,0 +1 @@ +7.14.0-SNAPSHOT diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/README.md b/samples/openapi3/client/petstore/dart-dio/binary_response/README.md new file mode 100644 index 00000000000..72b788d40d2 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/README.md @@ -0,0 +1,83 @@ +# openapi (EXPERIMENTAL) +No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + +This Dart package is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project: + +- API version: 1.0.0 +- Generator version: 7.14.0-SNAPSHOT +- Build package: org.openapitools.codegen.languages.DartDioClientCodegen + +## Requirements + +* Dart 2.15.0+ or Flutter 2.8.0+ +* Dio 5.0.0+ (https://pub.dev/packages/dio) +* JSON Serializable 6.1.5+ (https://pub.dev/packages/json_serializable) + +## Installation & Usage + +### pub.dev +To use the package from [pub.dev](https://pub.dev), please include the following in pubspec.yaml +```yaml +dependencies: + openapi: 1.0.0 +``` + +### Github +If this Dart package is published to Github, please include the following in pubspec.yaml +```yaml +dependencies: + openapi: + git: + url: https://github.com/GIT_USER_ID/GIT_REPO_ID.git + #ref: main +``` + +### Local development +To use the package from your local drive, please include the following in pubspec.yaml +```yaml +dependencies: + openapi: + path: /path/to/openapi +``` + +## Getting Started + +Please follow the [installation procedure](#installation--usage) and then run the following: + +```dart +import 'package:openapi/openapi.dart'; + + +final api = Openapi().getDefaultApi(); + +try { + final response = await api.binaryResponse(); + print(response); +} catch on DioException (e) { + print("Exception when calling DefaultApi->binaryResponse: $e\n"); +} + +``` + +## Documentation for API Endpoints + +All URIs are relative to *http://localhost* + +Class | Method | HTTP request | Description +------------ | ------------- | ------------- | ------------- +[*DefaultApi*](doc/DefaultApi.md) | [**binaryResponse**](doc/DefaultApi.md#binaryresponse) | **GET** /limits | + + +## Documentation For Models + + + +## Documentation For Authorization + +Endpoints do not require authorization. + + +## Author + + + diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/analysis_options.yaml b/samples/openapi3/client/petstore/dart-dio/binary_response/analysis_options.yaml new file mode 100644 index 00000000000..70524126e3f --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/analysis_options.yaml @@ -0,0 +1,10 @@ +analyzer: + language: + strict-inference: true + strict-raw-types: true + strict-casts: false + exclude: + - test/*.dart + - lib/src/model/*.g.dart + errors: + deprecated_member_use_from_same_package: ignore diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/build.yaml b/samples/openapi3/client/petstore/dart-dio/binary_response/build.yaml new file mode 100644 index 00000000000..89a4dd6e1c2 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/build.yaml @@ -0,0 +1,18 @@ +targets: + $default: + builders: + json_serializable: + options: + # Options configure how source code is generated for every + # `@JsonSerializable`-annotated class in the package. + # + # The default value for each is listed. + any_map: false + checked: true + create_factory: true + create_to_json: true + disallow_unrecognized_keys: true + explicit_to_json: true + field_rename: none + ignore_unannotated: false + include_if_null: false diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/doc/DefaultApi.md b/samples/openapi3/client/petstore/dart-dio/binary_response/doc/DefaultApi.md new file mode 100644 index 00000000000..3286dd380c8 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/doc/DefaultApi.md @@ -0,0 +1,51 @@ +# openapi.api.DefaultApi + +## Load the API package +```dart +import 'package:openapi/api.dart'; +``` + +All URIs are relative to *http://localhost* + +Method | HTTP request | Description +------------- | ------------- | ------------- +[**binaryResponse**](DefaultApi.md#binaryresponse) | **GET** /limits | + + +# **binaryResponse** +> Uint8List binaryResponse() + + + +### Example +```dart +import 'package:openapi/api.dart'; + +final api = Openapi().getDefaultApi(); + +try { + final response = api.binaryResponse(); + print(response); +} catch on DioException (e) { + print('Exception when calling DefaultApi->binaryResponse: $e\n'); +} +``` + +### Parameters +This endpoint does not need any parameter. + +### Return type + +[**Uint8List**](Uint8List.md) + +### Authorization + +No authorization required + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/zip + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/openapi.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/openapi.dart new file mode 100644 index 00000000000..2eeebdc9976 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/openapi.dart @@ -0,0 +1,14 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +export 'package:openapi/src/api.dart'; +export 'package:openapi/src/auth/api_key_auth.dart'; +export 'package:openapi/src/auth/basic_auth.dart'; +export 'package:openapi/src/auth/bearer_auth.dart'; +export 'package:openapi/src/auth/oauth.dart'; + + +export 'package:openapi/src/api/default_api.dart'; + + diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api.dart new file mode 100644 index 00000000000..8943da413f6 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api.dart @@ -0,0 +1,68 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'package:dio/dio.dart'; +import 'package:openapi/src/auth/api_key_auth.dart'; +import 'package:openapi/src/auth/basic_auth.dart'; +import 'package:openapi/src/auth/bearer_auth.dart'; +import 'package:openapi/src/auth/oauth.dart'; +import 'package:openapi/src/api/default_api.dart'; + +class Openapi { + static const String basePath = r'http://localhost'; + + final Dio dio; + Openapi({ + Dio? dio, + String? basePathOverride, + List? interceptors, + }) : + this.dio = dio ?? + Dio(BaseOptions( + baseUrl: basePathOverride ?? basePath, + connectTimeout: const Duration(milliseconds: 5000), + receiveTimeout: const Duration(milliseconds: 3000), + )) { + if (interceptors == null) { + this.dio.interceptors.addAll([ + OAuthInterceptor(), + BasicAuthInterceptor(), + BearerAuthInterceptor(), + ApiKeyAuthInterceptor(), + ]); + } else { + this.dio.interceptors.addAll(interceptors); + } + } + + void setOAuthToken(String name, String token) { + if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) { + (this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens[name] = token; + } + } + + void setBearerAuth(String name, String token) { + if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) { + (this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token; + } + } + + void setBasicAuth(String name, String username, String password) { + if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) { + (this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password); + } + } + + void setApiKey(String name, String apiKey) { + if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) { + (this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey; + } + } + + /// Get DefaultApi instance, base route and serializer can be overridden by a given but be careful, + /// by doing that all interceptors will not be executed + DefaultApi getDefaultApi() { + return DefaultApi(dio); + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api/default_api.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api/default_api.dart new file mode 100644 index 00000000000..9935152e290 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api/default_api.dart @@ -0,0 +1,91 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'dart:async'; + +// ignore: unused_import +import 'dart:convert'; +import 'package:openapi/src/deserialize.dart'; +import 'package:dio/dio.dart'; + +import 'dart:typed_data'; + +class DefaultApi { + + final Dio _dio; + + const DefaultApi(this._dio); + + /// binaryResponse + /// + /// + /// Parameters: + /// * [cancelToken] - A [CancelToken] that can be used to cancel the operation + /// * [headers] - Can be used to add additional headers to the request + /// * [extras] - Can be used to add flags to the request + /// * [validateStatus] - A [ValidateStatus] callback that can be used to determine request success based on the HTTP status of the response + /// * [onSendProgress] - A [ProgressCallback] that can be used to get the send progress + /// * [onReceiveProgress] - A [ProgressCallback] that can be used to get the receive progress + /// + /// Returns a [Future] containing a [Response] with a [Uint8List] as data + /// Throws [DioException] if API call or serialization fails + Future> binaryResponse({ + CancelToken? cancelToken, + Map? headers, + Map? extra, + ValidateStatus? validateStatus, + ProgressCallback? onSendProgress, + ProgressCallback? onReceiveProgress, + }) async { + final _path = r'/limits'; + final _options = Options( + method: r'GET', + responseType: ResponseType.bytes, + headers: { + ...?headers, + }, + extra: { + 'secure': >[], + ...?extra, + }, + validateStatus: validateStatus, + ); + + final _response = await _dio.request( + _path, + options: _options, + cancelToken: cancelToken, + onSendProgress: onSendProgress, + onReceiveProgress: onReceiveProgress, + ); + + Uint8List? _responseData; + + try { +final rawData = _response.data; +_responseData = rawData == null ? null : rawData as Uint8List; + + } catch (error, stackTrace) { + throw DioException( + requestOptions: _response.requestOptions, + response: _response, + type: DioExceptionType.unknown, + error: error, + stackTrace: stackTrace, + ); + } + + return Response( + data: _responseData, + headers: _response.headers, + isRedirect: _response.isRedirect, + requestOptions: _response.requestOptions, + redirects: _response.redirects, + statusCode: _response.statusCode, + statusMessage: _response.statusMessage, + extra: _response.extra, + ); + } + +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/api_key_auth.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/api_key_auth.dart new file mode 100644 index 00000000000..ee16e3f0f92 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/api_key_auth.dart @@ -0,0 +1,30 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + + +import 'package:dio/dio.dart'; +import 'package:openapi/src/auth/auth.dart'; + +class ApiKeyAuthInterceptor extends AuthInterceptor { + final Map apiKeys = {}; + + @override + void onRequest(RequestOptions options, RequestInterceptorHandler handler) { + final authInfo = getAuthInfo(options, (secure) => secure['type'] == 'apiKey'); + for (final info in authInfo) { + final authName = info['name'] as String; + final authKeyName = info['keyName'] as String; + final authWhere = info['where'] as String; + final apiKey = apiKeys[authName]; + if (apiKey != null) { + if (authWhere == 'query') { + options.queryParameters[authKeyName] = apiKey; + } else { + options.headers[authKeyName] = apiKey; + } + } + } + super.onRequest(options, handler); + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/auth.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/auth.dart new file mode 100644 index 00000000000..f7ae9bf3f11 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/auth.dart @@ -0,0 +1,18 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'package:dio/dio.dart'; + +abstract class AuthInterceptor extends Interceptor { + /// Get auth information on given route for the given type. + /// Can return an empty list if type is not present on auth data or + /// if route doesn't need authentication. + List> getAuthInfo(RequestOptions route, bool Function(Map secure) handles) { + if (route.extra.containsKey('secure')) { + final auth = route.extra['secure'] as List>; + return auth.where((secure) => handles(secure)).toList(); + } + return []; + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/basic_auth.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/basic_auth.dart new file mode 100644 index 00000000000..b65ccb5b71f --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/basic_auth.dart @@ -0,0 +1,37 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'dart:convert'; + +import 'package:dio/dio.dart'; +import 'package:openapi/src/auth/auth.dart'; + +class BasicAuthInfo { + final String username; + final String password; + + const BasicAuthInfo(this.username, this.password); +} + +class BasicAuthInterceptor extends AuthInterceptor { + final Map authInfo = {}; + + @override + void onRequest( + RequestOptions options, + RequestInterceptorHandler handler, + ) { + final metadataAuthInfo = getAuthInfo(options, (secure) => (secure['type'] == 'http' && secure['scheme']?.toLowerCase() == 'basic') || secure['type'] == 'basic'); + for (final info in metadataAuthInfo) { + final authName = info['name'] as String; + final basicAuthInfo = authInfo[authName]; + if (basicAuthInfo != null) { + final basicAuth = 'Basic ${base64Encode(utf8.encode('${basicAuthInfo.username}:${basicAuthInfo.password}'))}'; + options.headers['Authorization'] = basicAuth; + break; + } + } + super.onRequest(options, handler); + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/bearer_auth.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/bearer_auth.dart new file mode 100644 index 00000000000..8f46678761b --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/bearer_auth.dart @@ -0,0 +1,26 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'package:dio/dio.dart'; +import 'package:openapi/src/auth/auth.dart'; + +class BearerAuthInterceptor extends AuthInterceptor { + final Map tokens = {}; + + @override + void onRequest( + RequestOptions options, + RequestInterceptorHandler handler, + ) { + final authInfo = getAuthInfo(options, (secure) => secure['type'] == 'http' && secure['scheme']?.toLowerCase() == 'bearer'); + for (final info in authInfo) { + final token = tokens[info['name']]; + if (token != null) { + options.headers['Authorization'] = 'Bearer ${token}'; + break; + } + } + super.onRequest(options, handler); + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/oauth.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/oauth.dart new file mode 100644 index 00000000000..337cf762b0c --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/auth/oauth.dart @@ -0,0 +1,26 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +import 'package:dio/dio.dart'; +import 'package:openapi/src/auth/auth.dart'; + +class OAuthInterceptor extends AuthInterceptor { + final Map tokens = {}; + + @override + void onRequest( + RequestOptions options, + RequestInterceptorHandler handler, + ) { + final authInfo = getAuthInfo(options, (secure) => secure['type'] == 'oauth' || secure['type'] == 'oauth2'); + for (final info in authInfo) { + final token = tokens[info['name']]; + if (token != null) { + options.headers['Authorization'] = 'Bearer ${token}'; + break; + } + } + super.onRequest(options, handler); + } +} diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/deserialize.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/deserialize.dart new file mode 100644 index 00000000000..c16d0340f38 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/deserialize.dart @@ -0,0 +1,45 @@ + +final _regList = RegExp(r'^List<(.*)>$'); +final _regSet = RegExp(r'^Set<(.*)>$'); +final _regMap = RegExp(r'^Map$'); + + ReturnType deserialize(dynamic value, String targetType, {bool growable= true}) { + switch (targetType) { + case 'String': + return '$value' as ReturnType; + case 'int': + return (value is int ? value : int.parse('$value')) as ReturnType; + case 'bool': + if (value is bool) { + return value as ReturnType; + } + final valueString = '$value'.toLowerCase(); + return (valueString == 'true' || valueString == '1') as ReturnType; + case 'double': + return (value is double ? value : double.parse('$value')) as ReturnType; + default: + RegExpMatch? match; + + if (value is List && (match = _regList.firstMatch(targetType)) != null) { + targetType = match![1]!; // ignore: parameter_assignments + return value + .map((dynamic v) => deserialize(v, targetType, growable: growable)) + .toList(growable: growable) as ReturnType; + } + if (value is Set && (match = _regSet.firstMatch(targetType)) != null) { + targetType = match![1]!; // ignore: parameter_assignments + return value + .map((dynamic v) => deserialize(v, targetType, growable: growable)) + .toSet() as ReturnType; + } + if (value is Map && (match = _regMap.firstMatch(targetType)) != null) { + targetType = match![1]!.trim(); // ignore: parameter_assignments + return Map.fromIterables( + value.keys as Iterable, + value.values.map((dynamic v) => deserialize(v, targetType, growable: growable)), + ) as ReturnType; + } + break; + } + throw Exception('Cannot deserialize'); + } \ No newline at end of file diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/pubspec.yaml b/samples/openapi3/client/petstore/dart-dio/binary_response/pubspec.yaml new file mode 100644 index 00000000000..cbfddf172a6 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/pubspec.yaml @@ -0,0 +1,17 @@ +name: openapi +version: 1.0.0 +description: OpenAPI API client +homepage: homepage + + +environment: + sdk: '>=3.5.0 <4.0.0' + +dependencies: + dio: '^5.7.0' + json_annotation: '^4.9.0' + +dev_dependencies: + build_runner: any + json_serializable: '^6.9.3' + test: '^1.16.0' diff --git a/samples/openapi3/client/petstore/dart-dio/binary_response/test/default_api_test.dart b/samples/openapi3/client/petstore/dart-dio/binary_response/test/default_api_test.dart new file mode 100644 index 00000000000..dd547067d91 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/binary_response/test/default_api_test.dart @@ -0,0 +1,16 @@ +import 'package:test/test.dart'; +import 'package:openapi/openapi.dart'; + + +/// tests for DefaultApi +void main() { + final instance = Openapi().getDefaultApi(); + + group(DefaultApi, () { + //Future binaryResponse() async + test('test binaryResponse', () async { + // TODO + }); + + }); +} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/another_fake_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/another_fake_api.dart index d98f3c23f4f..20e0f8da946 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/another_fake_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/another_fake_api.dart @@ -84,6 +84,7 @@ _bodyData=jsonEncode(modelClient); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'ModelClient', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/default_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/default_api.dart index e9efb7b5b19..7db9981e2a8 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/default_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/default_api.dart @@ -64,6 +64,7 @@ class DefaultApi { try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'FooGetDefaultResponse', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_api.dart index 06486358eda..e91f47cebe9 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_api.dart @@ -74,6 +74,7 @@ class FakeApi { try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'FakeBigDecimalMap200Response', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -143,6 +144,7 @@ _responseData = rawData == null ? null : deserialize(rawData, 'HealthCheckResult', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -314,6 +316,7 @@ _bodyData=jsonEncode(body); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'bool', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -403,6 +406,7 @@ _bodyData=jsonEncode(outerComposite); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'OuterComposite', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -492,6 +496,7 @@ _bodyData=jsonEncode(body); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'num', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -581,6 +586,7 @@ _bodyData=jsonEncode(body); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'String', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -670,6 +676,7 @@ _bodyData=jsonEncode(outerObjectWithEnumProperty); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'OuterObjectWithEnumProperty', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -1027,6 +1034,7 @@ _bodyData=jsonEncode(modelClient); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'ModelClient', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_classname_tags123_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_classname_tags123_api.dart index 56ec33f1cac..85fbbac9287 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_classname_tags123_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_classname_tags123_api.dart @@ -91,6 +91,7 @@ _bodyData=jsonEncode(modelClient); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'ModelClient', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/pet_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/pet_api.dart index ec20128ee1e..aab8df51891 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/pet_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/pet_api.dart @@ -202,6 +202,7 @@ _bodyData=jsonEncode(pet); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize, Pet>(rawData, 'List', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -284,6 +285,7 @@ _responseData = rawData == null ? null : deserialize, Pet>(rawData, 'L try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize, Pet>(rawData, 'Set', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -362,6 +364,7 @@ _responseData = rawData == null ? null : deserialize, Pet>(rawData, 'Se try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'Pet', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -604,6 +607,7 @@ _bodyData=jsonEncode(pet); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'ApiResponse', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -702,6 +706,7 @@ _responseData = rawData == null ? null : deserialize(r try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'ApiResponse', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/store_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/store_api.dart index 23e8deceaea..f81b521ed41 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/store_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/store_api.dart @@ -118,6 +118,7 @@ class StoreApi { try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize, int>(rawData, 'Map', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -189,6 +190,7 @@ _responseData = rawData == null ? null : deserialize, int>(rawD try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'Order', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -278,6 +280,7 @@ _bodyData=jsonEncode(order); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'Order', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/user_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/user_api.dart index 24bbeb48f74..85583331abe 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/user_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/user_api.dart @@ -308,6 +308,7 @@ _bodyData=jsonEncode(user); try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'User', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions, @@ -387,6 +388,7 @@ _responseData = rawData == null ? null : deserialize(rawData, 'User' try { final rawData = _response.data; _responseData = rawData == null ? null : deserialize(rawData, 'String', growable: true); + } catch (error, stackTrace) { throw DioException( requestOptions: _response.requestOptions,