swagger-api / swagger-api/swagger-core

Generic Response Types

Open
#3,323 39 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog Feature
Dominant language
Java
Stars
7.5k
Forks
2.3k
Avg merge
18h 1m
Merged PRs (30d)
10

Description

When an API returns a generic response type swagger-core:

  1. The swagger-core generated object name is not linked to the API definition response.
  2. Does not provide a means to customize the name of the returned object.

Example:
ResponseBody<List> getTeamMemberNames(Long teamId);

Swagger-core generates:
schema object: ResponseBodyListString (not a name we would want to expose)

  "ResponseBodyListString" : {
    "type" : "object",
    "properties" : {
      "data" : {
        "type" : "array",
        "items" : {
          "type" : "string"
        }
      }
    }
  },

API definition:
"/team/membernames" : {
"get" : {
"operationId" : "getTeamMemberNames",
"parameters" : [ {
"name" : "teamId",
"in" : "path",
"required" : true,
"schema" : {
"type" : "integer",
"format" : "int64"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/ResponseBody"
}
}
}
},
}
}
},

You can see above, that the API definition response returns "ResponseBody" the name of the generic class. Rather than the generated ResponseBodyListString object.

Current workaround: We define a custom "doc" class and use that in the Operation annotation.
This requires the doc class and the actual implementation are kept in-sync.

@Operation(summary = "Gets team member names", 
    description = "Gets list of team member names.",
    responses = {
         @ApiResponse(responseCode = "200", description = "OK", 
             content = @Content(schema = @Schema(implementation = TeamMemberNamesResponse.class))),
     })    

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 by tracing swagger-core's generic response schema generation using the ResponseBody<List> example and the @Operation/@ApiResponse annotations shown. Compare the generated ResponseBodyListString schema with the ResponseBody reference; done should include a way to associate or customize the exposed response schema name without maintaining a separate documentation class.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.