Optional<LocalDate> in model parameter not rendered as String
See original GitHub issueWhat kind of issue is this?
- Question. Is this a question about how to do a certain thing? (May be)
- Bug report. (may be)
- Repository with minimal code for reproduction : link
Hi,
I don’t know if it is a bug or a misconfiguration but when i use an Òptional<LocalDate>
inside a model like this (all the code is available in the linked repo) :
// Model
public class Modele {
@DateTimeFormat(pattern = SwaggerConfiguration.LOCAL_DATE_IN_URL)
private Optional<LocalDate> date;
public Optional<LocalDate> getDate() {
return date;
}
public void setDate(Optional<LocalDate> date) {
this.date = date;
}
}
// Controller
@Controller
@RequestMapping("/test")
public class SampleController {
@GetMapping
@ResponseBody
public ResponseEntity<Object> test(@RequestParam(name = "date", required=false) @DateTimeFormat(pattern = SwaggerConfiguration.LOCAL_DATE_IN_URL) Optional<LocalDate> date) {
return new ResponseEntity<>(HttpStatus.OK);
}
@GetMapping("/model")
@ResponseBody
public ResponseEntity<Object> test(Modele modele) {
return new ResponseEntity<>(HttpStatus.OK);
}
}
// Configuration
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
public static final String LOCAL_DATE_IN_URL = "dd-MM-yyyy";
public static final String LOCAL_DATE_TIME_IN_URL = "yyyy-MM-dd HH:mm:ss";
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.genericModelSubstitutes(Optional.class)
.apiInfo(apiInfo());
}
@Bean
public FormattingConversionService conversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern(LOCAL_DATE_IN_URL));
registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_IN_URL));
registrar.registerFormatters(conversionService);
return conversionService;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("FBO Spring REST Sample with Swagger")
.description("FBO Spring REST Sample with Swagger")
.termsOfServiceUrl("https://groupefbo.com/")
.version("1.0.0")
.build();
}
}
It works as expected when using directly an Optional<LocalDate>
as a @RequestParam
or when using a plain LocalDate
field in the model.
But when i use an Optional<LocalDate>
as a field the ouput is :
Is there a configuration that can solve my issue ?
Thanks !
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
Why should Java 8's Optional not be used in arguments
Although it might be tempting to consider Optional for not mandatory method parameters, such a solution pale in comparison with other possible alternatives....
Read more >Model parameters—ArcGIS Pro | Documentation
Any model variable can be set as a model parameter. Set a variable as a ... If the variable has no value in...
Read more >Component Parameters - Apache Tapestry
Components can have any number of render variables. Render variables are named values with no specific type (they are ultimately stored in a...
Read more >Query String variables not showing up in URL after a RedirectTo
I am trying to set up a simple link, where the HREF points to a URL with a collection of query string parameters:...
Read more >Groovy Language Documentation
This extra lenience allows using method or variable names that were not keywords in ... class Car { String make String model }...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, it did ! Thanks. (Sorry for a long time before answering, the project was dropped off and I forgot)
ok:
Note that a big down side exists where you get ride of some type information e.g. your api doc know longer knows what part of a model is a URI for example it only knows it is a String. This is the pain I have https://github.com/springfox/springfox/issues/2987 that might not be an issue for you though.