diff --git a/.changeset/fast-worms-return.md b/.changeset/fast-worms-return.md new file mode 100644 index 00000000..2e992519 --- /dev/null +++ b/.changeset/fast-worms-return.md @@ -0,0 +1,5 @@ +--- +"@openapi-generator-plus/java-jaxrs-server-generator": minor +--- + +Generate ApiJaxbJsonProvider as an abstract class so users can modify the implementation without it being overwritten diff --git a/packages/java-jaxrs-server/src/index.ts b/packages/java-jaxrs-server/src/index.ts index a74fe975..110a5a1e 100644 --- a/packages/java-jaxrs-server/src/index.ts +++ b/packages/java-jaxrs-server/src/index.ts @@ -118,8 +118,10 @@ export const createGenerator: CodegenGeneratorConstructor const providerPackagePath = generatorOptions.apiProviderPackage ? packageToPath(generatorOptions.apiProviderPackage) : undefined if (providerPackagePath) { - await emit('ApiJaxbJsonProvider', path.join(outputPath, relativeApiImplSourceOutputPath, providerPackagePath, 'ApiJaxbJsonProvider.java'), + await emit('AbstractApiJaxbJsonProvider', path.join(outputPath, relativeApiImplSourceOutputPath, providerPackagePath, 'AbstractApiJaxbJsonProvider.java'), { ...rootContext }, true, hbs) + await emit('ApiJaxbJsonProvider', path.join(outputPath, relativeApiImplSourceOutputPath, providerPackagePath, 'ApiJaxbJsonProvider.java'), + { ...rootContext }, false, hbs) } if (context.additionalExportTemplates) { diff --git a/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs b/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs new file mode 100644 index 00000000..294bcbc5 --- /dev/null +++ b/packages/java-jaxrs-server/templates/AbstractApiJaxbJsonProvider.hbs @@ -0,0 +1,16 @@ +package {{apiProviderPackage}}; + +{{>generatedAnnotation}} +public abstract class AbstractApiJaxbJsonProvider extends com.fasterxml.jackson.{{#if @root.useJakarta}}jakarta.rs{{else}}jaxrs{{/if}}.json.{{#if @root.useJakarta}}JacksonXmlBindJsonProvider{{else}}JacksonJaxbJsonProvider{{/if}} { + + public AbstractApiJaxbJsonProvider() { + super(); + + this.setMapper(createMapper()); + } + + protected com.fasterxml.jackson.databind.ObjectMapper createMapper() { + {{>frag/jaxbJsonProviderBody}} + } + +} diff --git a/packages/java-jaxrs-server/templates/ApiJaxbJsonProvider.hbs b/packages/java-jaxrs-server/templates/ApiJaxbJsonProvider.hbs index fb7e1207..0b097483 100644 --- a/packages/java-jaxrs-server/templates/ApiJaxbJsonProvider.hbs +++ b/packages/java-jaxrs-server/templates/ApiJaxbJsonProvider.hbs @@ -8,15 +8,10 @@ package {{apiProviderPackage}}; @{{javax}}.ws.rs.Consumes({"application/json; charset=UTF-8", "application/*+json; charset=UTF-8" }) @{{javax}}.ws.rs.ext.Provider {{>hooks/jaxbJsonProviderAnnotations}} -{{>generatedAnnotation}} -public class ApiJaxbJsonProvider extends com.fasterxml.jackson.{{#if @root.useJakarta}}jakarta.rs{{else}}jaxrs{{/if}}.json.{{#if @root.useJakarta}}JacksonXmlBindJsonProvider{{else}}JacksonJaxbJsonProvider{{/if}} { +public class ApiJaxbJsonProvider extends {{apiProviderPackage}}.AbstractApiJaxbJsonProvider { public ApiJaxbJsonProvider() { super(); - - {{>frag/jaxbJsonProviderBody}} - - this.setMapper(mapper); } } diff --git a/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs b/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs index 53bc1dfd..4bc5e637 100644 --- a/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs +++ b/packages/java-jaxrs-server/templates/frag/jaxbJsonProviderBody.hbs @@ -9,3 +9,5 @@ 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;