diff --git a/.changeset/pink-starfishes-turn.md b/.changeset/pink-starfishes-turn.md new file mode 100644 index 00000000..345d10c9 --- /dev/null +++ b/.changeset/pink-starfishes-turn.md @@ -0,0 +1,7 @@ +--- +"@openapi-generator-plus/java-jaxrs-server-generator": minor +--- + +Make AbstractApiJaxbJsonProvider more customisable in code. + +Rather than relying on replacing a Handlebars frag. diff --git a/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs b/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs index 294bcbc5..c6a3440f 100644 --- a/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs +++ b/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs @@ -10,7 +10,37 @@ public abstract class AbstractApiJaxbJsonProvider extends com.fasterxml.jackson. } protected com.fasterxml.jackson.databind.ObjectMapper createMapper() { - {{>frag/jaxbJsonProviderBody}} + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + + /* We don't serialise null values so the client can treat them as null or undefined as they see fit */ + mapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL); + + /* Support PATCH requests using java.util.Optional */ + if (isRegisterJdk8Module()) { + mapper.registerModule(createJdk8Module()); + } + + /* Support Java time object types */ + mapper.registerModule(createJavaTimeModule()); + mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, isWriteDatesAsTimestamps()); + + return mapper; + } + + protected boolean isRegisterJdk8Module() { + return true; + } + + protected com.fasterxml.jackson.datatype.jdk8.Jdk8Module createJdk8Module() { + return new com.fasterxml.jackson.datatype.jdk8.Jdk8Module(); + } + + protected com.fasterxml.jackson.datatype.jsr310.JavaTimeModule createJavaTimeModule() { + return new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule(); + } + + protected boolean isWriteDatesAsTimestamps() { + return false; } } diff --git a/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs b/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs deleted file mode 100644 index 4bc5e637..00000000 --- a/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs +++ /dev/null @@ -1,13 +0,0 @@ -com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - -/* We don't serialise null values so the client can treat them as null or undefined as they see fit */ -mapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL); - -/* Support PATCH requests using java.util.Optional */ -mapper.registerModule(new com.fasterxml.jackson.datatype.jdk8.Jdk8Module()); - -/* Support Java time object types */ -mapper.registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule()); -mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); - -return mapper;