Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [java-jaxrs-resteasy] JacksonConfig.java uses wrong date library #5584

Open
NaigoApps opened this issue Mar 11, 2020 · 4 comments
Open

Comments

@NaigoApps
Copy link

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;
    }
}
@albertotn
Copy link

As part of hacktoberfest 2020 I'd like to try and solve this

@alwibrm
Copy link

alwibrm commented Oct 2, 2020

@albertotn Would appreciate a bugfix here. I just ran into the problem, too.

@wing328
Copy link
Member

wing328 commented Nov 9, 2020

We welcome contributions from the community to fix it or someone to sponsor this fix to give it a higher priority among other requests.

@lostiniceland
Copy link
Contributor

Would it be possible to backport this fix to 4.3.x
Due to the inheritance-bug we have to downgrade the plugin from 5.x to 4.3.1 which now generates the wrong date imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants