Skip to content
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

[DART-dio] Can't send body and binary data #9079

Closed
jaumard opened this issue Mar 25, 2021 · 3 comments · Fixed by #9542
Closed

[DART-dio] Can't send body and binary data #9079

jaumard opened this issue Mar 25, 2021 · 3 comments · Fixed by #9542

Comments

@jaumard
Copy link
Contributor

jaumard commented Mar 25, 2021

I have the following definition:

"/api/v1/shops/{shop_id}/transactions/{id}/signature":
    patch:
      operationId: patch_shops_transaction_signature_partial
      description: ""
      requestBody:
        content:
          image/png:
            schema:
              type: string
              nullable: true
              format: binary
        description: signature image to upload
        required: true
      responses:
        "201":
          description: All good
      tags:
        - transactions

This is generated like this:

Future<Response<void>> patchShopsTransactionSignaturePartial(
    int id,
    int shopId,
    Uint8List body, { 
    CancelToken cancelToken,
    Map<String, dynamic> headers,
    Map<String, dynamic> extra,
    ValidateStatus validateStatus,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) async {
    final _request = RequestOptions(
      path: r'/api/v1/shops/{shop_id}/transactions/{id}/signature'.replaceAll('{' r'id' '}', id.toString()).replaceAll('{' r'shop_id' '}', shopId.toString()),
      method: 'PATCH',
      headers: <String, dynamic>{
        ...?headers,
      },
      extra: <String, dynamic>{
        'secure': <Map<String, String>>[
          {
            'type': 'apiKey',
            'name': 'Bearer',
            'keyName': 'Authorization',
            'where': 'header',
          },
        ],
        ...?extra,
      },
      validateStatus: validateStatus,
      contentType: [
        'image/png',
      ].first,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );

    dynamic _bodyData;

    _bodyData = body;

    final _response = await _dio.request<dynamic>(
      _request.path,
      data: _bodyData,
      options: _request,
    );

    return _response;
  }

But this is not working, the correct way of sending the data is like this according to this issue of dio (cfug/dio#1036):

Future<Response<void>> patchShopsTransactionSignaturePartial(
      int id,
      int shopId,
      Uint8List body, {
        CancelToken cancelToken,
        Map<String, dynamic> headers,
        Map<String, dynamic> extra,
        ValidateStatus validateStatus,
        ProgressCallback onSendProgress,
        ProgressCallback onReceiveProgress,
      }) async {
    final _request = RequestOptions(
      path: r'/api/v1/shops/{shop_id}/transactions/{id}/signature'.replaceAll('{' r'id' '}', id.toString()).replaceAll('{' r'shop_id' '}', shopId.toString()),
      method: 'PATCH',
      headers: <String, dynamic>{
        ...?headers,
        'Content-length': body.length,
      },
      extra: <String, dynamic>{
        'secure': <Map<String, String>>[
          {
            'type': 'apiKey',
            'name': 'Bearer',
            'keyName': 'Authorization',
            'where': 'header',
          },
        ],
        ...?extra,
      },
      validateStatus: validateStatus,
      contentType: [
        'application/octet-stream',
      ].first,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );

    List<int> _bodyData;

    _bodyData = body;

    final _response = await _dio.request<dynamic>(
      _request.path,
      data: Stream.fromIterable(_bodyData.map((e) => [e])),
      options: _request,
    );

    return _response;
  }
@kuhnroyal
Copy link
Contributor

Not sure if we have a sample for this.

@jaumard
Copy link
Contributor Author

jaumard commented Mar 29, 2021

It's a pretty bad use case, almost no body does that... but unfortunately my backend does loool I have been able to change the API so I'm now using multipart instead and it works great. But I think it still should be fixed in generator to avoid headache to users having that because they will take time understanding the problem is wrong generated code

@kuhnroyal
Copy link
Contributor

Yea I agree, have not seen this in the wild either but I guess it is valid.

kuhnroyal added a commit to kuhnroyal/openapi-generator that referenced this issue May 21, 2021
* add support for filenames in multipart requests by using  `MultipartFile` from dio directly
* add support for binary/file body data
* fixes OpenAPITools#6671
* fixes OpenAPITools#9079
wing328 pushed a commit that referenced this issue May 25, 2021
* [dart][dart-dio] Improve support for file uploads

* add support for filenames in multipart requests by using  `MultipartFile` from dio directly
* add support for binary/file body data
* fixes #6671
* fixes #9079

* Add and fix tests

* Only use MultipartFile for body/multipart parameters

* Fix test

* Actually fix tests
premiumitsolution pushed a commit to premiumitsolution/openapi-generator that referenced this issue May 26, 2021
…9542)

* [dart][dart-dio] Improve support for file uploads

* add support for filenames in multipart requests by using  `MultipartFile` from dio directly
* add support for binary/file body data
* fixes OpenAPITools#6671
* fixes OpenAPITools#9079

* Add and fix tests

* Only use MultipartFile for body/multipart parameters

* Fix test

* Actually fix tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants