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
  • #8039
Closed
Open
Issue created Nov 27, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [Go] New go client generator doesn't handle binary/file response

Created by: euiko

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

The problem occured when using latest 5.0.0-beta3 release which deprecate old go client implementation. Using path's response with binary/file schema type (i.e. for file download with application specific content-type, e.g. pdf, xls, html, etc), got unhandled response type when using the generated client. Peeking on the implementation https://github.com/OpenAPITools/openapi-generator/blob/3f75691da200c20764a860ae8eae9b7b458c6e62/modules/openapi-generator/src/main/resources/go/client.mustache#L397 there is changes compared to the old implementation in go-deprecated, it doesn't handle (*os.File type). Is there are changes by design?

openapi-generator version

Look like already handled in previous stable version 4.1.3, that are now deprecated but missing in new generator

OpenAPI declaration file content or url

Simple schema, only parts of a whole openapi spec :

...
paths:
  '/reports/{id}/get':
    parameters:
      - schema:
          type: string
          format: uuid
          example: df50a5d7-b9cf-453d-af18-524ff47fe558
        name: id
        in: path
        required: true
    get:
      summary: Download report
      tags:
        - client
      responses:
        '200':
          description: OK
          headers: {}
          content:
            application/pdf:
              schema:
                $ref: '#/components/schemas/FileDownload'
            application/vnd.ms-excel:
              schema:
                $ref: '#/components/schemas/FileDownload'
            text/html:
              schema:
                $ref: '#/components/schemas/FileDownload'
      operationId: getReportJobResult
      description: Download generated report
components:
  schemas:
    FileDownload:
      title: FileDownload
      type: string
      format: binary
Generation Details

Generate using go client generator

Steps to reproduce
  • Create schema contain binary response
  • Generate using new go client
  • Use the generated client to integrate with server implementation (either mock or real)
Related issues/PRs
Suggest a fix

Current workaround is by enumerating errors returned by generated client like :

_, httpResp, err := c.client.ClientApi.GetReportJobResult(ctx, jobID).Execute()
if httpResp.StatusCode != 200 {
	return ErrDownloadFailed
}
if err != nil {
	// error happened? check its type is related with undefined response type
	if xerr, ok := err.(schema.GenericOpenAPIError); ok && xerr.Error() == "undefined response type" {
		// write the request body to file instead
		f.Write(xerr.Body())
		return nil
	}
	return err
}

But I would like to know the proper handling for these such case, I am thinking using the previous approach in go-deprecated by creating temporary files is enough. Or maybe using read writer interface as datatype of binary-like format, that could be changed through generation config.

Assignee
Assign to
Time tracking