'openapi - Return a file upon simple get request
I am trying to create a rest api, that, given a get request, returns a file. The contents of the file can be a text based format, like xml or json but it can also be binary. For this reason I just want to return the file as is and "let the client deal with it".
The issue that I am facing is, that the openapi-generator seems to be unable to parse an open file, that is defined in the response section of an endpoint in any way, and always just returns an empty section. What I expected to see is at least some sort of binary representation within the json based response body.
So far I have created the following .yml file for the specification:
The URI /schema/{family}/{version}' is meant to return a file corresponding to family and version.
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
Using the openapi-generator's jar file I can generate a valid go server with
java -jar openapi-generator-cli.jar generate -i rest_api.yml -g go-server -o .
The resulting runnable go project contains a go/ directory where I can plug in my business logic in the _service.go files, in this case the function SchemaFile(). I filled it for testing purposes as follows:
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
}
To test the output I used curl, but, like with a browser e.g. Chrome, I get an empty response section for the "file":
$ curl -X 'GET' 'http://localhost:8080/manager/v1/schema/A/b' -H 'accept: application/octet-stream'
{"name":"/home/user/existingfile.txt","file":{}}
What am I missing? Is returning an actual binary file content simply not within the scope of how openapi is meant to be used? Is the generator simply not implemented in the way to deal with an *os.File object that it most certainly understood to put in as an expected type for its response struct?
Any help would be appreciated.
EDIT: I changed the type for the file schema from binary to byte, i.e.:
file:
description: the file itself
type: string
format: byte
This changes the generated service stub over which I can return something from *os.File to string, as it expects me to return base64 code. That obviously works, but I find it slightly depressing to use base64 for something I was expecting to work in proper raw byte data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
