Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12161
Closed
Open
Issue created Apr 18, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [Dart] Response type of application/octet-stream with format: binary are unusable

Created by: 0xNF

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

An endpoint which returns binary data defined as

schema:
  type: string
  format: binary

Will fail in the following ways:

  1. Declare the function's return type to be MultipartFile, which is only really valid for file upload, not for file download.
  2. Attempt to read the response data via the Response.Body field, which defaults to attempting to deserialize as a string. For true binary data, this isn't valid. This will throw an exception at runtime.

As a result, true binary data response endpoints (as in, not b64 encoded responses), are unrepresentable in the current Dart generator.

see also: https://swagger.io/docs/specification/data-models/data-types/#file

openapi-generator version

6.0.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: "1.1"
  title: Dart Uint8list Demo
servers:
  - url: 'localhost'
    variables:
      host:
        default: localhost
paths:
  /item:
    get:
      operationId: GetItem
      responses:
        "200":
          description: Binary data
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary

The generated method looks like this:

  Future<MultipartFile?> getItem() async {
    final response = await getItemWithHttpInfo();
    if (response.statusCode >= HttpStatus.badRequest) {
      throw ApiException(response.statusCode, await _decodeBodyBytes(response));
    }
    // When a remote server returns no body with a status of 204, we shall not decode it.
    // At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
    // FormatException when trying to decode an empty string.
    if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
      return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'MultipartFile',) as MultipartFile;
    
    }
    return null;
  }
Steps to reproduce
  1. java -jar openapi-generator-cli.jar generate -i ./spec.yaml -g dart -o uint
  2. use the getItem() method
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/8878

Suggest a fix
  1. Response types of application/octet-stream and schema declarations of type: string, format: binary should use the UInt8List datatype, not the MultipartFile. Request types should remain unchanged.
  2. Responses should be deserialized with request.bodyBytes, not request.Body, to avoid decoding exceptions

It also looks like the dart-dio-next generator handles this fine -- using -g dart-dio-next I get Response<Uint8List> instead of Response<MultipartFile>

Assignee
Assign to
Time tracking