OpenAPI generatorによるファイル入出力コードの自動生成について #1737
-
現在ファイル入出力を扱ったサンプルがないため、springdoc-OpenAPIとOpenAPI generatorによる生成ですべてカバーできるのか気になっています。需要があればサンプルに実装例が追加されてほしいです。 内部会話で挙がった実装例
// ファイルアップロード
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "成功.", content = @Content) })
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<MessageResponse> postFile(@RequestPart(name = "file", required = true) MultipartFile file)
{
} // ファイルダウンロード
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "成功.", content = @Content(mediaType = "application/octet-stream", schema = @Schema(implementation = Resource.class)))})
@GetMapping(path = "/{id}")
public ResponseEntity<Resource> getFileBinaryZip(@PathVariable("id") long id) throws IOException
{
Resource zipData = service.getFileBinary(id);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).body(zipData);
}
const response = await fileControllerApi.getFileBinaryZip(fileId, {
responseType: 'arraybuffer'
})
const blob = new Blob([response.data], { type: 'application/zip' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = `${fileName}.zip`
link.click()
URL.revokeObjectURL(link.href) |
Beta Was this translation helpful? Give feedback.
Answered by
fkoyama
Dec 20, 2024
Replies: 1 comment
-
上記のバックエンドの実装で、OpenAPI generatorが生成するgetFileBinaryZipのコードは以下の通り。
"paths": {
"/api/files/{id}": {
"get": {
"operationId": "getFileBinaryZip",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"responses": {
"200": {
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
},
"description": "成功."
}
},
"tags": [
"files-controller"
]
}
}
}
export const FilesControllerApiAxiosParamCreator = function (configuration?: Configuration) {
return {
getFileBinaryZip: async (id: number, options: RawAxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'id' is not null or undefined
assertParamExists('getFileBinaryZip', 'id', id)
const localVarPath = `/api/files/zip/{id}`.replace(`{${'id'}}`, encodeURIComponent(String(id)))
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL)
let baseOptions
if (configuration) {
baseOptions = configuration.baseOptions
}
const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options }
const localVarHeaderParameter = {} as any
const localVarQueryParameter = {} as any
setSearchParams(localVarUrlObj, localVarQueryParameter)
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}
localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers }
return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions
}
},
}
}
export const FilesControllerApiFp = function (configuration?: Configuration) {
const localVarAxiosParamCreator = FilesControllerApiAxiosParamCreator(configuration)
return {
async getFileBinaryZip(
id: number,
options?: RawAxiosRequestConfig
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<File>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getFileBinaryZip(id, options)
const localVarOperationServerIndex = configuration?.serverIndex ?? 0
const localVarOperationServerBasePath = operationServerMap['FilesControllerApi.getFileBinaryZip']?.[localVarOperationServerIndex]?.url
return (axios, basePath) =>
createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath)
}
}
}
export const FilesControllerApiFactory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
const localVarFp = FilesControllerApiFp(configuration)
return {
getFileBinaryZip(id: number, options?: any): AxiosPromise<File> {
return localVarFp.getFileBinaryZip(id, options).then((request) => request(axios, basePath))
},
}
}
export class FilesControllerApi extends BaseAPI {
public getFileBinaryZip(id: number, options?: RawAxiosRequestConfig) {
return FilesControllerApiFp(this.configuration)
.getFileBinaryZip(id, options)
.then((request) => request(this.axios, this.basePath))
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
tsuna-can-se
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
上記のバックエンドの実装で、OpenAPI generatorが生成するgetFileBinaryZipのコードは以下の通り。