OpenAPITools / OpenAPITools/openapi-generator
[BUG] [java-jaxrs-resteasy] JacksonConfig.java uses wrong date library
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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;
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/JavaJaxRS/resteasy/JacksonConfig.mustache and compare its date handling with the generated User.java shown in the report. Trace how the java8 dateLibrary option is applied, then verify that generated JacksonConfig.java uses the configured date library without requiring Joda dependencies.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100