Spring Auto Rest Docs documents all Json subTypes

208 views Asked by At

I have a Rest service with two operations /balance and /transactions to get the balance and transactions of a customer.

The return type of this operations is BalanceResponse and TransactionResponse and both these type is extended from Response

when documenting for /balance service operation it is also listing response fields in second subType(TransactionResponse).

How to display only the fields corresponding to its return type ? If its /balance then display (status, balance and restrictions), and If its /transaction only display (status and list of transactions) in the Response field

Can somebody please let me know how to handle basically inheritance types in docs

Please find below code snippet and Auto RestDoc generated doc

//Base class
@JsonTypeInfo(use = NAME, include = PROPERTY, property = "type", visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = BalanceResponse.class, name = "BalanceResponse"),
        @JsonSubTypes.Type(value = TransactionsResponse.class, name = "TransactionResponse")})
public class Response {
    public Status status;
....
...
    public Response(StatusCode status) {
        this.status = new Status(status.getCode(), status.getDescription());
    }

}
// Type1: BalanceResponse
@JsonPropertyOrder({ "status", "balance", "restrictions" })
public class BalanceResponse extends Response {
    /**
     * The balance of this account
     */
    public int balance = -1; 

    /**
     * List of limitations on this account.
     */
    public List<String> restrictions = Collections.emptyList();
}

// SubType-2 TransactionResponse
public class TransactionsResponse extends Response {
    public List<Transaction> transactions;

enter image description hereAuto RestDoc Response field

1

There are 1 answers

0
Juraj Misur On

You need to return a specific subtype in your controller method, for Spring Auto Rest Docs to output only that type's fields. If you return parent type, then the response can be anything and SARD will output all possible fields from all types.

    // returns all subtype fields
    public Response anything() {
        return new BalanceResponse();
    }

    // returns only BalanceResponse fields
    public BalanceResponse balances() {
        return new BalanceResponse();
    }