-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[dart-dio] Fix json_serializable response for type:string format:binary #21379
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
Merged
wing328
merged 10 commits into
OpenAPITools:master
from
matthewnitschke-wk:dart-dio_fix_json_serializable_binary_return
Jul 1, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d916d2
fixed inconsistencies with the dart-dio json_serializable string bina…
matthewnitschke-wk 52f45f9
update deserialize template to handle responseFile
matthewnitschke-wk daf9295
regenerate samples
matthewnitschke-wk 266073a
only convert return_type to Uint8List
matthewnitschke-wk 3e95845
remove unused imports from previous changes
matthewnitschke-wk a65d0b8
fixed issues where import wouldn't get included
matthewnitschke-wk a436034
specific unit test for binary response
matthewnitschke-wk 3a7a3c3
reverted changes to nested generators
matthewnitschke-wk 842e0a8
removed 2 additional php modifications
matthewnitschke-wk 44f76c8
regen samples
matthewnitschke-wk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...ain/resources/dart/libraries/dio/serialization/json_serializable/api/deserialize.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| final rawData = _response.data; | ||
| _responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true); | ||
| {{#isResponseFile}} | ||
| _responseData = rawData == null ? null : rawData as {{{returnType}}}; | ||
| {{/isResponseFile}} | ||
| {{^isResponseFile}} | ||
| _responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true); | ||
| {{/isResponseFile}} |
16 changes: 16 additions & 0 deletions
16
modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
41 changes: 41 additions & 0 deletions
41
samples/openapi3/client/petstore/dart-dio/binary_response/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
23 changes: 23 additions & 0 deletions
23
samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator-ignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
15 changes: 15 additions & 0 deletions
15
samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/FILES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
1 change: 1 addition & 0 deletions
1
samples/openapi3/client/petstore/dart-dio/binary_response/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 7.14.0-SNAPSHOT |
83 changes: 83 additions & 0 deletions
83
samples/openapi3/client/petstore/dart-dio/binary_response/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
|
|
||
|
|
10 changes: 10 additions & 0 deletions
10
samples/openapi3/client/petstore/dart-dio/binary_response/analysis_options.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
18 changes: 18 additions & 0 deletions
18
samples/openapi3/client/petstore/dart-dio/binary_response/build.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
51 changes: 51 additions & 0 deletions
51
samples/openapi3/client/petstore/dart-dio/binary_response/doc/DefaultApi.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
|
14 changes: 14 additions & 0 deletions
14
samples/openapi3/client/petstore/dart-dio/binary_response/lib/openapi.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'; | ||
|
|
||
|
|
68 changes: 68 additions & 0 deletions
68
samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Interceptor>? 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); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a previous iteration I implemented the same logic as the built_value serialization method, where both parameter and return types are considered
Uint8ListIn an effort to keep this change slightly less impactful, I've opted to just change the return type when the response is a file
I'm open to input on this change, whether we want parity with built_value (both parameters and return types are
Uint8List) or are ok with what I have here