Forum Discussion

petergret's avatar
petergret
Regular Visitor
6 years ago

java.util.Optional CodeGen

Hi

 

I am using Swagger code generation in a SpringBoot application to generate a JSON describing the REST-API. One of the REST-APIs has a java.util.Optional<Boolean> as a return type. For this return type, swagger only generates the "present"-field, nut none for the actual value of the optional. I tried to configure the Jdk8Module for the object mapper in the following way, but without success:

 

import static springfox.documentation.builders.PathSelectors.ant;

import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.base.Predicates;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.schema.configuration.ObjectMapperConfigured;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@Profile(SPRING_PROFILE_SWAGGER_GEN)
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig implements ApplicationListener<ObjectMapperConfigured> {
@Override
public void onApplicationEvent(ObjectMapperConfigured event) {
event.getObjectMapper().registerModule(new Jdk8Module());
}

@Bean
@SuppressWarnings("unchecked")
public Docket restApi() {
return new Docket(DocumentationType.SWAGGER_2)
.forCodeGeneration(true)
.select()
.paths(Predicates.and(ant("/api/**"),
Predicates.not(ant("/api/*test*")),
Predicates.not(ant("/api/*test*/**"))))
.build();
}
}

Does anybody know how to make Swagger map java.util.Optional properly?

No RepliesBe the first to reply