Describe the bug
swagger-ui dont take care of jackson objectMapper/@jsonformat ,
let's consider with have a REST endpoint that consume a POJO with LocalTime as attribute with correct @JsonFormat
We have configured ObjectMapper, but in swagger-ui java.time.LocalTime is not represented by a String but has an entity
Exemple of swagger-ui output
{
"heureDebutActivite": {
"hour": "string",
"minute": "string",
"nano": 0,
"second": "string"
},
"heureFinActivite": {
"hour": "string",
"minute": "string",
"nano": 0,
"second": "string"
}
}
POJO
public class Site {
...
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
public LocalTime heureDebutActivite;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
public LocalTime heureFinActivite;
public Site() {
// TODO Auto-generated constructor stub
}
...
}
Object Mapper configuration
import javax.inject.Singleton;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import io.quarkus.jackson.ObjectMapperCustomizer;
@Singleton
public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer {
public void customize(ObjectMapper mapper) {
mapper.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
}
}
pom.xml
...
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
...
Expected behavior
LocalTime can be represented by ISO string (and pattern specififed in @JsonFormat) in swagger ui
Actual behavior
LocalTime not correctly formatted in swagger ui
To Reproduce
Steps to reproduce the behavior:
Environment (please complete the following information):
windows 10
Quarkus version 1.0.1.Final
jdk:8
/cc @EricWittmann
Not sure OpenAPI supports Jackson specific stuff, though.
I think it's openAPi generator config https://stackoverflow.com/questions/56471246/how-to-show-java8-localtime-as-a-string-in-swagger , the question is now, it's possible to configure openapi generator in Quarkus ?
The Microprofile OpenAPI spec doesn't really have much to say about how to handle specific java types and annotations. But the SmallRye OpenAPI implementation does try to do sensible things whenever possible - in general as well as specifically in regard to data types. There are, of course, a lot of data types and not all of them are currently handled ideally. This is a good example.
If @MikeEdgar wants to provide additional insight, as he is closer to that code than I am, that'd be great. But for types that aren't already being handled ideally, we have (or will soon have) the ability to provide custom mappings. So in this case you'd be able to map the LocalTime to a string with an ISO pattern.
It might be also useful to update SmallRye OpenAPI to do that by default for certain java.time classes.
SmallRye OpenAPI currently supports date and date-time, but not time for the java.time classes, likely because they are not mentioned in the OpenAPI spec itself. As @EricWittmann suggested, it would probably be good to support time, following along with the formats outlined by RFC3339 for partial-time and/or full-time.
In the short term @zakhdar, you should be able to get your expected output by applying a @Schema attribute to each of your LocalTime fields with implementation of String.class and your desired format. Please also open an issue in smallrye-open-api to look at adding out-of-box support for LocalTime.
Thank you @EricWittmann and @MikeEdgar for your information , i will use @Schema as workaround and open an issue in smallrye-open-api for adding out-ofbox support for LocalTime. thk you for your support
@zakhdar I saw that the PR on smallrye-openapi to fix that has been merged.
Can we close this one ?
The same issue for JODATIME, still not fixed
@csfercoci - are you saying the issue is still occurring for LocalTime or that it is also happening for Joda types? If it is happening for Joda types, there has been no change in the scanner to support them.
P.S. do you have control over the code using Joda-Time types? The recommendation is that code using Joda-Time on JDK 8 or later migrate to java.time.
Closing this one as it has been fixed on Smallrye OpenAPI side.
Most helpful comment
Thank you @EricWittmann and @MikeEdgar for your information , i will use @Schema as workaround and open an issue in smallrye-open-api for adding out-ofbox support for LocalTime. thk you for your support