3

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?

Vladas Maier
  • 2,054
  • 2
  • 22
  • 34
basseur
  • 153
  • 1
  • 1
  • 11

3 Answers3

0

I believe this might be a duplicate?: https://stackoverflow.com/a/41677559/8932643

Ronny Shibley answers:

For Parameters

public void getApi(@ApiIgnore String param){}

@ApiModelProperty(hidden="true")
public String paramInsideClass
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Quadrivics
  • 181
  • 2
  • 10
  • No, unfortunately, it is not. As I said, I need `startDate` and `endDate`. If I use `@ApiModelProperty(hidden="true")` on the field, I won't get anything in Swagger. I would like to use it on the methods that return the Optional, but that won't work. – basseur Nov 17 '19 at 09:02
0

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.

basseur
  • 153
  • 1
  • 1
  • 11
0

rename getStartDateWithTimezone to findStartDateWithTimezone

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Yaroslav
  • 21
  • 2