OpenAPITools / OpenAPITools/openapi-generator
[REQ] "throws Exception" for specified APIs
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
we have api.yaml definition
openapi: "3.0.2"
info:
version: "1.0.0"
title: "File Management API"
description: >-
File Management
paths:
/api/v1/documents:
post:
tags:
- Manage Files
summary: "Upload new file"
operationId: "createDocument"
requestBody:
content:
multipart/form-data:
schema:
properties:
file:
type: array
items:
type: string
format: binary
metadata:
type: object
responses:
201:
description: "Document created, return generated document information"
404:
description: "Not Found"
409:
description: "Conflict"
500:
description: "Internal Server Error"
get:
operationId: "getDocument"
responses:
200:
description: "return document information"
404:
description: "Not Found"
409:
description: "Conflict"
500:
description: "Internal Server Error"
it generate APIs as below, no "throws Exception" generated, but we really need "throws" in some cases:
default ResponseEntity<Document> createDocument(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile file,@ApiParam(value = "") @RequestParam(value="metadata", required=false) Object metadata)
default ResponseEntity<Void> getDocument()
I found two related PR/issue
- remove "throws Exception"
https://github.com/swagger-api/swagger-codegen/issues/7437 - add "throws Exception" with unhandledException flag, it's global flag for all APIs
https://github.com/OpenAPITools/openapi-generator/pull/2482
directly remove/add "throws Exception" for all APIs are not good idea, since a part of APIs need it.
Describe the solution you'd like
I think we can use Open API "Specification Extensions", with additional flag "x-" to tell generator which API need throws, which don't need.

options:
Option 1. add x-operationId-exception: true/false ---- boolean value
api.yaml:
paths:
/api/v1/documents:
post:
operationId: "createDocument"
x-operationId-exception: true
option1.1 generate logical:
if (x-operationId-exception == true/undefined && unhandledException == true) {
generate "throws Exception"
} else if (x-operationId-exception == false && unhandledException == true) {
// e.g. we have 10 APIs, only a few APIs(e.g. 1,2) doesn't need throws
no throws generated
} else if (x-operationId-exception == true && unhandledException == false/undefined) {
// e.g. we have 10 APIs, only a few APIs(e.g. 1,2) do need throws
generate "throws Exception"
} else if (x-operationId-exception == false/undefined && unhandledException == false/undefined) {
no throws generated
}
option 1.2
for simple,
if(x-operationId-exception == true) {
generate "throws Exception"
} else {
no throws generated
}
Option 2. add x-operationId-exception: ---- string array
api.yaml:
paths:
/api/v1/documents:
post:
operationId: "createDocument"
x-operationId-exception: [MyException1, MyException2]
option 2.1 generate logical:
if (x-operationId-exception is defined) {
// no need to check unhandledException
generate "throws MyException1, MyException2"
} else if (x-operationId-exception is undefined) {
if (unhandledException == true) {
generate "throws Exception"
} else {
no throws generated
}
}
option 2.2
for simple,
if(x-operationId-exception is defined) {
generate "throws MyException1, MyException2"
} else {
no throws generated
}
Option 2 (for simple, option 2.2) may be better, how do you think?
Describe alternatives you've considered
Additional context
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how the Java generator turns an OpenAPI operation into the shown method signature and how the global unhandledException setting is handled. Compare the two proposed x-operationId-exception forms with the related issue and pull request. Done means a decided per-operation behavior, generated throws declarations matching that behavior, and coverage for operations with and without the extension.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100