Hiding unimportant getter methods from swagger UI in Java

Tags: , , ,



I have two fields in my class startDate and endDate of LocalDateTime. Internally, these need to be returned as Optional<ZonedDateTime>, while in the query they are provided as LocalDateTimes.

@ApiModel
public class SearchQuery {

    private static final ZoneId UTC_TIMEZONE = ZoneId.of("UTC");

    @ApiParam(value = "Start date and time of the requested time frame.", example = "2019-06-01T12:30", defaultValue = "0000-01-01T00:00")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime startDate;

    @ApiParam(value = "End date and time of the requested time frame.", example = "2019-06-30T23:59", defaultValue = "9999-12-31T23:59")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime endDate;

    public LocalDateTime getStartDate() {
        return this.startDate;
    }

    public LocalDateTime getEndDate() {
        return this.endDate;
    }

    public Optional<ZonedDateTime> getStartDateWithTimezone() {
        return Optional.ofNullable(startDate)
            .map(date -> date.atZone(UTC_TIMEZONE));
    }

    public Optional<ZonedDateTime> getEndDateWithTimezone() {
        return Optional.ofNullable(endDate)
            .map(date -> date.atZone(UTC_TIMEZONE));
    }
}

Swagger (Springfox v2.9.2) now shows the needed fields but also endDateWithTimezone.present and startDateWithTimezone.present, which are of course both not needed as parameters:

enter image description here

I have been trying for some time now to find ways to hide these from my swagger documentation. How can I do this?

Answer

The solution was quite simple in the end.

You have to use the annotation @ApiModelProperty(hidden = true) directly on the getter you want to hide.



Source: stackoverflow