Bediar Sofiane
Bediar Sofiane

Reputation: 21

using Swagger @ApiResponse responseContainer not working when code is 400

In Swagger Java API, when I use a responsecontainer="List" (Or "Set") with a code=400, I am not getting the model of the response on Swagger-GUI. I am just getting Array[Object].

Here is the concrete case:

@CrossOrigin
@RestController
@RequestMapping(value = "/api")
@Loggable(prepend = true, trim = false)
public class ConfigResource {

    private final ConfigResourceDelegate delegate;

    @Inject
    public ConfigResource(final ConfigResourceDelegate delegate) {
        this.delegate = delegate;
    }


    @RequestMapping(
        value = "/v1/config",
        method = PUT,
        consumes = APPLICATION_JSON_UTF8_VALUE,
        produces = APPLICATION_JSON_UTF8_VALUE
    )
    @ApiResponses(value = {@ApiResponse(code=202,message = "ACCEPTED" ),
        @ApiResponse(code=200,response = Rejection.class, responseContainer 
        = "Set", message = "BAD_REQUEST"),
        @ApiResponse(code=500, message = "INTERNAL_SERVER_ERROR")})
    public ResponseEntity<?> putConfig(final @RequestBody ConfigDto 
    configDto){
    return delegate.putConfig(riskConfigDto);
    }
}

Here is the Rejection Class:

public class Rejection {


private Long id;


private RejectionDTO rejection;


private String originMessage;

public Rejection() {
}

public Long getId() {
    return id;
}

public RejectionDTO getRejection() {
    return rejection;
}

public String getOriginMessage() {
    return originMessage;
}

public void setId(Long id) {
    this.id = id;
}

public void setRejection(RejectionDTO rejection) {
    this.rejection = rejection;
}

public void setOriginMessage(String originMessage) {
    this.originMessage = originMessage;
}
}

So normally i'am supposed to have this model between [] in the swagger UI. However, I am getting Array[Object]:

See screen capture

Upvotes: 2

Views: 9338

Answers (1)

Indra Basak
Indra Basak

Reputation: 7394

To make your example work, you need to change your return value from wildcard, ResponseEntity<?>, to a concrete class, ResponseEntity<List<Rejection>>. Also, you need to change responseContainer to a List from Set.

@RequestMapping(
        value = "/v1/config",
        method = PUT,
        consumes = APPLICATION_JSON_UTF8_VALUE,
        produces = APPLICATION_JSON_UTF8_VALUE
    )
    @ApiResponses(value = {@ApiResponse(code=202,message = "ACCEPTED" ),
        @ApiResponse(code=200,response = Rejection.class, responseContainer 
        = "List", message = "BAD_REQUEST"),
        @ApiResponse(code=500, message = "INTERNAL_SERVER_ERROR")})
    public ResponseEntity<List<Rejection>> putConfig(final @RequestBody ConfigDto 
    configDto){
    return delegate.putConfig(riskConfigDto);
    }

Upvotes: 3

Related Questions