OpenAPITools / OpenAPITools/openapi-generator

[BUG] go-server: Unable to return a *os.File in the body as simple binary data upon get request

Open
#12,315 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

I am trying to create an api get response, where part of the body is a full file content, encoded as a simple octet array, or plain binary data. When I define a corresponding endpoint uri that returns as part of the http response a binary file, I only receive an empty response for that file section.
I would expect it to return some sort of binary content within the response.

openapi-generator version

5.4.0
No idea if it is a regression.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: simple service
  license:
    name: Proprietary
  title: some title
  version: 1.0.0
externalDocs:
  description: Design file can be found in wiki
  url: https://somewhere.else/wiki
servers:
- url: http://localhost:9091/manager/v1
tags:
- name: manager
  description: TODO add a description
  
paths:
  /schemas:
    get:
      operationId: SchemaList
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/SchemaName'
          description: successful
        "500":
          description: internal server error
        "503":
          description: service unavailable          
      summary: get list of schema families
      description: gets the top level overview over families of schemas
      tags:
        - schema
  '/schema/{family}':
    get:
      operationId: FamilyList
      parameters:
        - name: family
          in: path
          description: name of the family
          required: true
          schema:
            type: string
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/SchemaName'
          description: successful
        "500":
          description: internal server error
        "503":
          description: service unavailable          
      summary: get list of schemas belonging to a schema family
      tags:
        - schema
  '/schema/{family}/{version}':
    get:
      operationId: SchemaFile
      parameters:
        - name: family
          in: path
          description: name of the family
          required: true
          schema:
            type: string
        - name: version
          in: path
          description: version of file within family of schemas
          required: true
          schema:
            type: string
      responses:
        "200":
          content:
            application/octet-stream:
              schema:
                $ref: '#/components/schemas/SchemaFile'
          description: successful
        "500":
          description: internal server error
        "503":
          description: service unavailable          
      summary: get schema of a certain family in a certain version
      tags:
        - schema  
components:
  schemas:
    SchemaName:
      title: SchemaName
      description: representation of a schema family
      type: object
      required:
        - name
      properties:
        name:
          type: string
          example: "myLittleSchema"
    SchemaFile:
      title: SchemaFile
      description: a schema file, either a single file or a archive (e.g. a .zip)
      type: object
      required:
        - name
        - file
      properties:
        name:
          description: filename
          type: string
          example: "yourFile.txt"
        file:
          description: the file itself
          type: string
          format: binary
Generation Details

Generated a go-server with a call of:
java -jar openapi-generator-cli.jar generate -i rest_api.yml -g go-server -o .

In the generated code file api_schema_service.go I extended the following code section:

const fakeFile string = "/home/user/existingfile.txt"

// SchemaFile - get schema of a certain family in a certain version
func (s *SchemaApiService) SchemaFile(ctx context.Context, family string, version string) (ImplResponse, error) {
	file, err := os.Open(fakeFile)
	fmt.Printf("name: %s\nfile: %v\nerr: %v\n", fakeFile, file, err)
	if err != nil {
		return Response(500, nil), errors.New("file not found")
	}
	return Response(http.StatusOK, SchemaFile{Name: fakeFile, File: file}), nil
}

Running this code and requesting a get from this with curl results in:

$ curl -X 'GET' 'http://localhost:8080/manager/v1/schema/A/b' -H 'accept: application/octet-stream'
{"name":"/home/user/existingfile.txt","file":{}}

I would expect the field "file" to contain something. The file '/home/user/existingfile.txt' exists and contains the following content:

$ cat /home/user/existingfile.txt 
Hello, my name is test file, and welcome to openapi
Steps to reproduce

Create a .yml with the contents above, and rerun the commands specified above.

Related issues/PRs

None

Suggest a fix

No idea.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated api_schema_service.go and the OpenAPI SchemaFile declaration, then reproduce the request using the supplied YAML, generation command, and curl example. Trace how the go-server response serializes the os.File field; done means the file field returns the expected binary content instead of an empty object.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.