-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Let Jackson use JAXB3 Keep optional dependency for JAXB2 to keep it working Signed-off-by: jansupol <[email protected]>
- Loading branch information
Showing
17 changed files
with
289 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ackson/src/main/java/org/glassfish/jersey/jackson/internal/JacksonMapperConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2022 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 com.fasterxml.jackson.databind.AnnotationIntrospector; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; | ||
import org.glassfish.jersey.internal.util.ReflectionHelper; | ||
import org.glassfish.jersey.internal.util.collection.LazyValue; | ||
import org.glassfish.jersey.internal.util.collection.Value; | ||
import org.glassfish.jersey.internal.util.collection.Values; | ||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.Annotations; | ||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JsonMapperConfigurator; | ||
|
||
import java.security.AccessController; | ||
import java.util.ArrayList; | ||
|
||
public class JacksonMapperConfigurator extends JsonMapperConfigurator { | ||
public JacksonMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations) { | ||
super(mapper, defAnnotations); | ||
} | ||
|
||
@Override | ||
protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotationsToUse) { | ||
// Let's ensure there are no dups there first, filter out nulls | ||
ArrayList<AnnotationIntrospector> intr = new ArrayList<AnnotationIntrospector>(); | ||
for (Annotations a : annotationsToUse) { | ||
if (a != null) { | ||
_resolveIntrospector(a, intr); | ||
} | ||
} | ||
int count = intr.size(); | ||
if (count == 0) { | ||
return AnnotationIntrospector.nopInstance(); | ||
} | ||
AnnotationIntrospector curr = intr.get(0); | ||
for (int i = 1, len = intr.size(); i < len; ++i) { | ||
curr = AnnotationIntrospector.pair(curr, intr.get(i)); | ||
} | ||
return curr; | ||
} | ||
|
||
protected void _resolveIntrospector(Annotations ann, ArrayList<AnnotationIntrospector> intr) { | ||
switch (ann) { | ||
case JAXB: | ||
/* For this, need to use indirection just so that error occurs | ||
* when we get here, and not when this class is being loaded | ||
*/ | ||
try { | ||
if (_jaxbIntrospectorClass == null) { | ||
_jaxbIntrospectorClass = JakartaXmlBindAnnotationIntrospector.class; | ||
} | ||
intr.add(JakartaXmlBindAnnotationIntrospector.class.newInstance()); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Failed to instantiate JakartaXmlBindAnnotationIntrospector: " | ||
+ e.getMessage(), e); | ||
} | ||
|
||
if (jaxb2AnnotationIntrospector.get() == true) { | ||
Class<? extends AnnotationIntrospector> tempJaxbIntrospectorClass = _jaxbIntrospectorClass; | ||
_jaxbIntrospectorClass = null; | ||
intr.add(super._resolveIntrospector(ann)); | ||
_jaxbIntrospectorClass = tempJaxbIntrospectorClass; | ||
} | ||
break; | ||
default: | ||
intr.add(super._resolveIntrospector(ann)); | ||
} | ||
} | ||
|
||
private static LazyValue<Boolean> jaxb2AnnotationIntrospector = Values.lazy((Value<Boolean>) () -> { | ||
final Class<?> aClass = AccessController.doPrivileged( | ||
ReflectionHelper.classForNamePA("com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector") | ||
); | ||
return aClass != null; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...son/src/test/java/org/glassfish/jersey/jackson/internal/JacksonJaxb2JsonProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2022 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 jakarta.ws.rs.core.Application; | ||
import org.glassfish.jersey.jackson.internal.model.Jaxb2ServiceTest; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
public final class JacksonJaxb2JsonProviderTest extends JerseyTest { | ||
|
||
@Override | ||
protected final Application configure() { | ||
return new ResourceConfig(Jaxb2ServiceTest.class); | ||
} | ||
|
||
@Test | ||
public final void testJavaOptional() { | ||
final String response = target("entity/simple").request().get(String.class); | ||
assertEquals("{\"name\":\"Hello\",\"value\":\"World\"}", response); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...n-jackson/src/test/java/org/glassfish/jersey/jackson/internal/model/Jaxb2ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2020, 2022 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.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
|
||
import java.util.Optional; | ||
|
||
@Path("/entity/") | ||
public final class Jaxb2ServiceTest { | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Path("/simple") | ||
public final EntityTest simple() { | ||
return new EntityTest("Hello", "World"); | ||
} | ||
|
||
private static final class EntityTest { | ||
|
||
private final String name; | ||
|
||
private final String value; | ||
|
||
EntityTest(final String name, final String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
@XmlElement(name = "jaxb") | ||
@JsonGetter("name") | ||
public final String getName() { | ||
return name; | ||
} | ||
|
||
@JsonGetter("value") | ||
public final Optional<String> getValue() { | ||
return Optional.ofNullable(value); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...a/json-jackson/src/test/java/org/glassfish/jersey/jackson/internal/model/ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.