Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism with swagger not working as expected

I am using springfox version 2.9.2 and swagger annotations 1.5.x. The ApiModel annotations support the discriminator, subTypes and parent attribute which are required to make polymorphism work but I am not seeing the correct apidocs generated to enable polymorphism.

Here is my annotated code.

@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {
    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleController.class);

    @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    void post(@RequestBody Vehicle anyVehicle) {
        LOGGER.info("Vehicle : {}", anyVehicle);
    }
}

@ApiModel(discriminator = "type", subTypes = {Car.class, Bike.class})
public class Vehicle {
    String brand;
    String type;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


@ApiModel(parent = Vehicle.class)
public class Car extends Vehicle {
    int noOfDoors;
    boolean powerWindows;

    public int getNoOfDoors() {
        return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
        this.noOfDoors = noOfDoors;
    }

    public boolean isPowerWindows() {
        return powerWindows;
    }

    public void setPowerWindows(boolean powerWindows) {
        this.powerWindows = powerWindows;
    }
}

@ApiModel(parent = Vehicle.class)
public class Bike extends Vehicle {
    boolean pillion;

    public boolean isPillion() {
        return pillion;
    }

    public void setPillion(boolean pillion) {
        this.pillion = pillion;
    }
}

When the docs get generated is basically shows one endpoint which handles a POST request and takes in a Vehicle as the model.

This is how the generated UI looks

Is what I am doing here supposed to work? Can someone point me to a working example of this with SpringFox that I can look at?

like image 817
Moiz Raja Avatar asked Feb 04 '19 21:02

Moiz Raja


1 Answers

Support for discriminator is not available in Swagger UI yet. You can follow these issues for status updates:

Discriminator does not switch schema
subTypes not displayed in model

like image 73
Helen Avatar answered Oct 15 '22 12:10

Helen