0

I have set up a swagger on my spring boot application, where on a lot of methods and controllers works correctly. The problem is the one field in the response class where Optional field is not showing on UI.

public class CalculatedResult {
    private CalculatorKey key;
    private CalculatedField field;
    **@ApiModelProperty(value = "Depending on CalculatedField it can be number or string")
    private Optional<?> value;**

... getters setters and etc

And the swagger class:

@Bean
public Docket api() {
    //global default message that are on all methods
    List<ResponseMessage> globalMessages = Arrays.asList(GLOBAL_MESSAGES); // ignore this
    
    return new Docket(DocumentationType.SWAGGER_2)
            .select()      
            .apis(RequestHandlerSelectors.basePackage("pts.riskengine"))
            .paths(PathSelectors.any())   
            .build()
            .directModelSubstitute(LocalDate.class, String.class)
            .genericModelSubstitutes(ResponseEntity.class)
            .alternateTypeRules(
                    newRule(typeResolver.resolve(DeferredResult.class,
                            typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                            typeResolver.resolve(WildcardType.class)))
            .additionalModels(typeResolver.resolve(ClientSettings.class), typeResolver.resolve(CalculatedResult.class),
                    typeResolver.resolve(CalculatorKey.class))
            .useDefaultResponseMessages(false)
            .forCodeGeneration(true)
            .apiInfo(apiInfo())
            .securitySchemes(Arrays.asList(securityScheme()))
            .securityContexts(Arrays.asList(securityContext()))
            .globalResponseMessage(RequestMethod.GET, globalMessages)
            .globalResponseMessage(RequestMethod.POST, globalMessages)
            .globalResponseMessage(RequestMethod.PUT, globalMessages)
            .globalResponseMessage(RequestMethod.DELETE, globalMessages)
            ;
}

I have tried to put in .additionalModels and also in .genericModelSubstitutes as String, but not working. dataType = "java.lang.String" is also not solving my issue. Do anyone else got this kind of problem or know how it could be solvable ?

Looking on UI: Screenshot

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>   

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
    <scope>compile</scope>
</dependency>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Brucebayne
  • 39
  • 3
  • Utilizing Optional type for the field is not good practice. – Alexander Ivanchenko May 16 '22 at 16:30
  • Thanks Alexander. Got this as a legacy code, maybe I will figure out better solution. – Brucebayne May 17 '22 at 08:47
  • Does this answer your question? [Why java.util.Optional is not Serializable, how to serialize the object with such fields](https://stackoverflow.com/questions/24547673/why-java-util-optional-is-not-serializable-how-to-serialize-the-object-with-suc) – tbjorch May 18 '22 at 08:25

1 Answers1

1

Optional class is not serializable. That's why swagger is not showing Optional.

https://stackoverflow.com/a/24564612/19008623

mbehram
  • 71
  • 5