2

I'm trying to use SpringFox 2.8+ to generate a model for class containing Optional fields.

Docket configuration has genericModelSubstitutes(Optional.class) rule, that perfectly works for Optional<String>. But when the type is Optional<LocalDateTime>, documentation contains invalid /definitions/LocalDateTime reference. In case of regular LocalDateTime field default rule works fine and I got {"type":"string","format":"date-time"}.

I try to use @ApiModelProperty as workaround, but it doesn't affect resulting json. Is there a way to override Optional model property definition?

public class MyModel {
    @ApiModelProperty(dataType = "string", example = "abc") // works
    public Optional<String> a;

    @ApiModelProperty(dataType = "string", example = "2019-08-19T15:05:59.785Z") // works
    public LocalDateTime b;

    @ApiModelProperty(dataType = "string", example = "2019-08-19T15:05:59.785Z") // doesn't work :(
    public Optional<LocalDateTime> c;
}

Adamovskiy
  • 1,376
  • 1
  • 12
  • 42

1 Answers1

0

I had a similar problem while working with @ApiParam. The following change to Docket configuration solved the issue for me. Maybe you can give it a try with @ApiModelProperty.

Docket(DocumentationType.SWAGGER_2)
  .select()
  ...
  .alternateTypeRules(
    newRule(
      typeResolver.resolve(Optional.class, LocalDateTime.class),
      typeResolver.resolve(String.class)
    )
  );
Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65