Skip to content

Commit

Permalink
processing order for Jackson/Jaxb annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Feb 24, 2021
1 parent 84b7eba commit e410aea
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ public class JacksonFeature implements Feature {
* Using them can provide a useful information to the user, but it can expose unnecessary information, too.
*/
private final boolean registerExceptionMappers;
private final boolean registerJaxbAnnotationModule;

/**
* Default constructor enables registering Jackson's exception mappers
*/
public JacksonFeature() {
this(true);
this(true, false);
}

private JacksonFeature(boolean registerExceptionMappers) {
private JacksonFeature(boolean registerExceptionMappers, boolean registerJaxbAnnotationModule) {
this.registerExceptionMappers = registerExceptionMappers;
this.registerJaxbAnnotationModule = registerJaxbAnnotationModule;
}

/**
Expand All @@ -71,7 +73,12 @@ public static JacksonFeature withExceptionMappers() {
* @return JacksonFeature without registered Jackson's exception mappers
*/
public static JacksonFeature withoutExceptionMappers() {
return new JacksonFeature(false);
return new JacksonFeature(false, false);
}

public static JacksonFeature withJaxbAnnotationModule() {

return new JacksonFeature(true, true);
}

private static final String JSON_FEATURE = JacksonFeature.class.getSimpleName();
Expand All @@ -93,7 +100,7 @@ public boolean configure(final FeatureContext context) {

// Register Jackson.
if (!config.isRegistered(JacksonJaxbJsonProvider.class)) {

DefaultJacksonJaxbJsonProvider.registerJaxbAnnotationModule.set(registerJaxbAnnotationModule);
if (registerExceptionMappers) {
// add the default Jackson exception mappers
context.register(JsonParseExceptionMapper.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,10 +17,12 @@
package org.glassfish.jersey.jackson.internal;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.Module;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.Annotations;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;

import java.util.Objects;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Singleton;

/**
Expand All @@ -29,7 +31,13 @@
@Singleton
public class DefaultJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {

//do not register JaxbAnnotationModule because it brakes default annotations processing
private static final String EXCLUDE_MODULE_NAME = "JaxbAnnotationModule";
//but if you know what you are doing, you are welcome
public static final AtomicBoolean registerJaxbAnnotationModule = new AtomicBoolean(false);

public DefaultJacksonJaxbJsonProvider() {
super();
findAndRegisterModules();
}

Expand All @@ -39,14 +47,20 @@ public DefaultJacksonJaxbJsonProvider(final Annotations... annotationsToUse) {
}

private void findAndRegisterModules() {

final ObjectMapper defaultMapper = _mapperConfig.getDefaultMapper();
if (Objects.nonNull(defaultMapper)) {
defaultMapper.findAndRegisterModules();
final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();

final List<Module> modules = ObjectMapper.findModules();
if (!registerJaxbAnnotationModule.get()) {
modules.removeIf(mod -> mod.getModuleName().contains(EXCLUDE_MODULE_NAME));
} else {
registerJaxbAnnotationModule.set(false);
}

final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();
if (Objects.nonNull(mapper)) {
mapper.findAndRegisterModules();
defaultMapper.registerModules(modules);
if (mapper != null) {
mapper.registerModules(modules);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.jackson.internal;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.jackson.internal.model.ServiceTest;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.core.Application;

import static junit.framework.TestCase.assertEquals;

public final class DefaultJacksonJaxbJsonProviderWithJaxbModuleTest extends JerseyTest {

@Override
protected final Application configure() {
return new ResourceConfig(ServiceTest.class).register(JacksonFeature.withJaxbAnnotationModule());
}

@Test
public final void testJavaOptional() {
final String response = target("entity/simple").request().get(String.class);
assertEquals("{\"jaxb\":\"Hello\",\"value\":\"World\"}", response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -23,6 +23,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;

@Path("/entity/")
public final class ServiceTest {
Expand All @@ -45,6 +46,7 @@ private static final class EntityTest {
this.value = value;
}

@XmlElement(name = "jaxb")
@JsonGetter("name")
public final String getName() {
return name;
Expand Down

0 comments on commit e410aea

Please sign in to comment.