[BUG] [java-jaxrs-resteasy] JacksonConfig.java uses wrong date library
Created by: NaigoApps
I'm using openapi-generator-maven-plugin to generate some classes given a yaml file. I wanted to use java8 as date library, but it seems that I'll need to add a dependency to joda library as well, because generated code refers that library despite configuration.
Tool used: openapi-generator-maven-plugin version 4.2.3
Plugin configuration:
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api/swagger/api.yaml</inputSpec>
<generatorName>jaxrs-resteasy</generatorName>
<configOptions>
<sourceFolder>src/main/java/api/generated</sourceFolder>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
api.yaml
openapi: 3.0.0
info:
title: My API
description: My API
version: 1.0.0
servers:
- url: myapi.com
paths:
/users:
get:
summary: 'Gets all users'
tags: ['users']
responses:
'200':
description: 'List of users'
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
username:
type: string
registerDate:
type: string
format: date
roles:
type: array
items:
$ref: '#/components/schemas/UserRole'
UserRole:
type: string
enum:
- CLIENT
- EXPERT
- ADMIN
User class is generated correctly, as I found field registerDate of type LocalDate User.java
public class User {
private String username;
private LocalDate registerDate;
private List<UserRole> roles = new ArrayList<>();
//other generated code...
}
The generator generates the JacksonConfig class as well, but this uses joda date library, even if I specified java8 in plugin configuration. I saw JacksonConfig.mustache, and I can't find customizable content. JacksonConfig.mustache
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.datatype.joda.JodaModule
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.ISODateTimeFormat;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
public class JacksonConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonConfig() throws Exception {
objectMapper = new ObjectMapper()
.setDateFormat(new RFC3339DateFormat())
.registerModule(new JodaModule() {
{
addSerializer(DateTime.class, new StdSerializer<DateTime>(DateTime.class) {
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(ISODateTimeFormat.dateTimeNoMillis().print(value));
}
});
addSerializer(LocalDate.class, new StdSerializer<LocalDate>(LocalDate.class) {
@Override
public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(ISODateTimeFormat.date().print(value));
}
});
}
});
}
public ObjectMapper getContext(Class<?> arg0) {
return objectMapper;
}
}