Search code examples
javaspring-bootswaggerswagger-uiopenapi

ApiResponses is confusing Swagger-UI output to not find the "type"


I am not really a java developer (I was one 15 years ago) but I am trying to freshen my knowledge to be more valuable in those projects. I am attempting to incorporate swagger-ui into a basic rest springboot app. I am getting the following error:

Unknown Type: CustomerVisit

I have the following controller defined:

package controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;


@RequestMapping("/api/v1")
@RestController
public class ScenarioController {

    @Autowired
    ScenarioService scenarioService;

    @RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
    @ApiResponses(value={
        @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(type="CustomerVisit"))),
        @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
        @ApiResponse(responseCode="400", description="request not accepted, invalid input")
    })
    public ResponseEntity<?> getCustomerVisitByScenarioId(@PathVariable String scenarioIdStr) {

        try {
            int scenarioId = Integer.valueOf(scenarioIdStr);
            List<CustomerVisit> customerList = scenarioService.getScenarioById(scenarioId);

            if (customerList.size() > 0) {
                return new ResponseEntity<>(customerList, HttpStatus.OK);
            }
            return new ResponseEntity<>(customerList, HttpStatus.ACCEPTED);
        } catch (NumberFormatException e) {
            return new ResponseEntity<>( "Scenario ID must be integer.", HttpStatus.BAD_REQUEST);
        }
    }
}

and the CustomerVisit bean as

package bean;
    
@Schema(name="CustomerVisit", description="A model of a customer")
public class CustomerVisit {
  
  public CustomerVisit(/* cut */) {
     /* cut - constructing class */      
  }

  /* cut getters */
}

The swagger-ui comes up and for other methods where I am not trying to provide ApiResponses in the documentation everything works. Once I add the ApiResponses things start to fall apart. IF i do not add the @Content annotation the ui describes the response types as {}. If I add the name of the type as tagged in the bean - it says the type is unknown. I haven't been able to find documentation to guide me on this was hoping to get a nudge in the right direction.

There look to be answers for older versions...


Solution

  • Update working but ugly approach

    1. added static class that extends ArrayList
    2. expose that

    such that

    public static class CustomerVisitList extends ArrayList<CustomerVisit> {}
    

    and

    @RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
    @ApiResponses(value={
            @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisitList.class))),
            @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
            @ApiResponse(responseCode="400", description="request not accepted, invalid input")
     })
    

    Is there a better way?


    This seems to be a step in the right direction (it is not right) - but it should really return a list back:

    @RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
    @ApiResponses(value={
        @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisit.class))),
        @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
        @ApiResponse(responseCode="400", description="request not accepted, invalid input")
    })