swagger-api / swagger-api/swagger-codegen
Different status code to marshall to different model
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I have an open api swagger model which defines an API that returns different model in different status code. For example, for status code 200, it returns class A payload, while for status code 202, it returns class B payload. Is it possible in swagger codegen to handle this case?
Swagger-codegen version
3.0.46
Swagger declaration file content or url
responses:
'200':
content:
application/json:
schema:
$ref: SomeClassA
description: The response contains a representation of the SomeClassA resource
'202':
content:
application/json:
schema:
$ref: SomeClassB
description: Accepted; a SomeClassB has been generated
'204':
description: Success, but no response data
default:
content:
application/json:
schema:
$ref: Error
description: Error condition
Command line used for generation
N/A
Steps to reproduce
N/A
Related issues/PRs
N/A
Suggest a fix/enhancement
Below code snippet is where the client is deserializing the response. Instead of deserializing Status.Family.SUCCESSFUL which is 2xx into the target object, can we only deserializing 200 or a configured status code into the successful target object, but all other status code into the ApiException? Client side can check the status code further and handle the edge case accordingly.
else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) {
if (returnType == null)
return null;
else
return deserialize(response, returnType);
} else {
String message = "error";
String respBody = null;
if (response.hasEntity()) {
try {
respBody = String.valueOf(response.readEntity(String.class));
message = respBody;
} catch (RuntimeException e) {
// e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
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 with the Java client response-deserialization entry point shown in the issue and the OpenAPI response declaration containing status codes 200, 202, 204, and default. Determine how generated clients currently select the successful return type and where status-specific models would be represented. Done means distinct response models can be selected by configured status code while other statuses reach ApiException, with coverage for the declared responses.
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