Skip to content

Commit 7c57c55

Browse files
[dart-dio] Fix json_serializable response for type:string format:binary (#21379)
* fixed inconsistencies with the dart-dio json_serializable string binary response types * update deserialize template to handle responseFile * regenerate samples * only convert return_type to Uint8List * remove unused imports from previous changes * fixed issues where import wouldn't get included * specific unit test for binary response * reverted changes to nested generators * removed 2 additional php modifications * regen samples
1 parent a5f638f commit 7c57c55

File tree

30 files changed

+692
-1
lines changed

30 files changed

+692
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: dart-dio
2+
outputDir: samples/openapi3/client/petstore/dart-dio/binary_response
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_20682.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/dart/libraries/dio
5+
additionalProperties:
6+
hideGenerationTimestamp: "true"
7+
enumUnknownDefaultCase: "true"
8+
serializationLibrary: "json_serializable"

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,17 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
670670
op.imports.remove("Uint8List");
671671
}
672672

673+
if (SERIALIZATION_LIBRARY_JSON_SERIALIZABLE.equals(library)) {
674+
// built_value serialization uses Uint8List for all MultipartFile types
675+
// in json_serialization, MultipartFile is used as the file parameter type, but
676+
// MultipartFile isn't readable, instead we convert this to a Uin8List
677+
if (op.isResponseFile) {
678+
op.imports.add("Uint8List");
679+
op.returnType = "Uint8List";
680+
op.returnBaseType = "Uint8List";
681+
}
682+
}
683+
673684
resultImports.addAll(rewriteImports(op.imports, false));
674685

675686
if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
final rawData = _response.data;
2-
_responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true);
2+
{{#isResponseFile}}
3+
_responseData = rawData == null ? null : rawData as {{{returnType}}};
4+
{{/isResponseFile}}
5+
{{^isResponseFile}}
6+
_responseData = rawData == null ? null : deserialize<{{{returnType}}}, {{{returnBaseType}}}>(rawData, '{{{returnType}}}', growable: true);
7+
{{/isResponseFile}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Binary response
5+
paths:
6+
/limits:
7+
get:
8+
operationId: binaryResponse
9+
responses:
10+
'200':
11+
description: OK
12+
content:
13+
application/zip:
14+
schema:
15+
type: string
16+
format: binary
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://dart.dev/guides/libraries/private-files
2+
3+
# Files and directories created by pub
4+
.dart_tool/
5+
.buildlog
6+
.packages
7+
.project
8+
.pub/
9+
build/
10+
**/packages/
11+
12+
# Files created by dart2js
13+
# (Most Dart developers will use pub build to compile Dart, use/modify these
14+
# rules if you intend to use dart2js directly
15+
# Convention is to use extension '.dart.js' for Dart compiled to Javascript to
16+
# differentiate from explicit Javascript files)
17+
*.dart.js
18+
*.part.js
19+
*.js.deps
20+
*.js.map
21+
*.info.json
22+
23+
# Directory created by dartdoc
24+
doc/api/
25+
26+
# Don't commit pubspec lock file
27+
# (Library packages only! Remove pattern if developing an application package)
28+
pubspec.lock
29+
30+
# Don’t commit files and directories created by other development environments.
31+
# For example, if your development environment creates any of the following files,
32+
# consider putting them in a global ignore file:
33+
34+
# IntelliJ
35+
*.iml
36+
*.ipr
37+
*.iws
38+
.idea/
39+
40+
# Mac
41+
.DS_Store
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.gitignore
2+
README.md
3+
analysis_options.yaml
4+
build.yaml
5+
doc/DefaultApi.md
6+
lib/openapi.dart
7+
lib/src/api.dart
8+
lib/src/api/default_api.dart
9+
lib/src/auth/api_key_auth.dart
10+
lib/src/auth/auth.dart
11+
lib/src/auth/basic_auth.dart
12+
lib/src/auth/bearer_auth.dart
13+
lib/src/auth/oauth.dart
14+
lib/src/deserialize.dart
15+
pubspec.yaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.14.0-SNAPSHOT
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# openapi (EXPERIMENTAL)
2+
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
3+
4+
This Dart package is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:
5+
6+
- API version: 1.0.0
7+
- Generator version: 7.14.0-SNAPSHOT
8+
- Build package: org.openapitools.codegen.languages.DartDioClientCodegen
9+
10+
## Requirements
11+
12+
* Dart 2.15.0+ or Flutter 2.8.0+
13+
* Dio 5.0.0+ (https://pub.dev/packages/dio)
14+
* JSON Serializable 6.1.5+ (https://pub.dev/packages/json_serializable)
15+
16+
## Installation & Usage
17+
18+
### pub.dev
19+
To use the package from [pub.dev](https://pub.dev), please include the following in pubspec.yaml
20+
```yaml
21+
dependencies:
22+
openapi: 1.0.0
23+
```
24+
25+
### Github
26+
If this Dart package is published to Github, please include the following in pubspec.yaml
27+
```yaml
28+
dependencies:
29+
openapi:
30+
git:
31+
url: https://github.com/GIT_USER_ID/GIT_REPO_ID.git
32+
#ref: main
33+
```
34+
35+
### Local development
36+
To use the package from your local drive, please include the following in pubspec.yaml
37+
```yaml
38+
dependencies:
39+
openapi:
40+
path: /path/to/openapi
41+
```
42+
43+
## Getting Started
44+
45+
Please follow the [installation procedure](#installation--usage) and then run the following:
46+
47+
```dart
48+
import 'package:openapi/openapi.dart';
49+
50+
51+
final api = Openapi().getDefaultApi();
52+
53+
try {
54+
final response = await api.binaryResponse();
55+
print(response);
56+
} catch on DioException (e) {
57+
print("Exception when calling DefaultApi->binaryResponse: $e\n");
58+
}
59+
60+
```
61+
62+
## Documentation for API Endpoints
63+
64+
All URIs are relative to *http://localhost*
65+
66+
Class | Method | HTTP request | Description
67+
------------ | ------------- | ------------- | -------------
68+
[*DefaultApi*](doc/DefaultApi.md) | [**binaryResponse**](doc/DefaultApi.md#binaryresponse) | **GET** /limits |
69+
70+
71+
## Documentation For Models
72+
73+
74+
75+
## Documentation For Authorization
76+
77+
Endpoints do not require authorization.
78+
79+
80+
## Author
81+
82+
83+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
analyzer:
2+
language:
3+
strict-inference: true
4+
strict-raw-types: true
5+
strict-casts: false
6+
exclude:
7+
- test/*.dart
8+
- lib/src/model/*.g.dart
9+
errors:
10+
deprecated_member_use_from_same_package: ignore

0 commit comments

Comments
 (0)