Skip to content

Generic Response Types #3323

@croman17

Description

@croman17

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))),
     })    

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions