Skip to content

Commit

Permalink
Fix #36: narrow ObjectMapper subtypes for lookups (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Nov 27, 2024
1 parent 97712d1 commit 6ad5846
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.*;

import jakarta.ws.rs.core.*;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Providers;

import com.fasterxml.jackson.core.*;

Expand Down Expand Up @@ -935,6 +937,24 @@ protected IOException _createNoContentException() {
/**********************************************************************
*/

// @since 2.19
protected <OM extends ObjectMapper> OM _locateMapperViaProvider(Class<?> type, MediaType mediaType,
Class<OM> mapperType, Providers providers) {
if (providers != null) {
ContextResolver<OM> resolver = providers.getContextResolver(mapperType, mediaType);
// Above should work as is, but due to this bug
// [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
// in Jersey, it doesn't. But this works until resolution of the issue:
if (resolver == null) {
resolver = providers.getContextResolver(mapperType, null);
}
if (resolver != null) {
return resolver.getContext(type);
}
}
return null;
}

protected static boolean _containedIn(Class<?> mainType, HashSet<ClassKey> set)
{
if (set != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;

import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;
import com.fasterxml.jackson.jakarta.rs.base.ProviderBase;
import com.fasterxml.jackson.jakarta.rs.cfg.Annotations;

Expand Down Expand Up @@ -155,9 +155,8 @@ protected boolean hasMatchingMediaType(MediaType mediaType)
return CBORMediaTypes.APPLICATION_JACKSON_CBOR_TYPE.getSubtype().equalsIgnoreCase(subtype) ||
"cbor".equalsIgnoreCase(subtype) || subtype.endsWith("+cbor");
}
/* Not sure if this can happen; but it seems reasonable
* that we can at least produce CBOR without media type?
*/
// Not sure if this can happen; but it seems reasonable
// that we can at least produce CBOR without media type?
return true;
}

Expand All @@ -184,25 +183,17 @@ protected boolean hasMatchingMediaType(MediaType mediaType)
@Override
protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
{
if (_providers != null) {
ContextResolver<ObjectMapper> resolver = _providers.getContextResolver(ObjectMapper.class, mediaType);
/* Above should work as is, but due to this bug
* [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
* in Jersey, it doesn't. But this works until resolution of
* the issue:
*/
if (resolver == null) {
resolver = _providers.getContextResolver(ObjectMapper.class, null);
}
if (resolver != null) {
ObjectMapper mapper = resolver.getContext(type);
// 07-Feb-2014, tatu: just in case, ensure we have correct type
if (mapper.getFactory() instanceof CBORFactory) {
return mapper;
}
// 26-Nov-2024, tatu: [jakarta-rs#36] Look for CBORMapper primarily
ObjectMapper m = _locateMapperViaProvider(type, mediaType, CBORMapper.class, _providers);
if (m == null) {
// but if not found, try ObjectMapper
m = _locateMapperViaProvider(type, mediaType, ObjectMapper.class, _providers);
// 07-Feb-2014, tatu: just in case, ensure we have correct type
if (m != null && !(m.getFactory() instanceof CBORFactory)) {
m = null;
}
}
return null;
return m;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Unit test to check [JACKSON-540]
*/
public class TestCanDeserialize extends JakartaRSTestBase
public class TestCanDeserializeCBOR extends JakartaRSTestBase
{
static class Bean {
public int x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.jakarta.rs.base.ProviderBase;
import com.fasterxml.jackson.jakarta.rs.cfg.Annotations;

Expand Down Expand Up @@ -184,30 +185,20 @@ protected boolean hasMatchingMediaType(MediaType mediaType)
|| "x-json".equals(subtype) // [Issue#40]
;
}
/* Not sure if this can happen; but it seems reasonable
* that we can at least produce JSON without media type?
*/
// Not sure if this can happen; but it seems reasonable
// that we can at least produce JSON without media type?
return true;
}

@Override
protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
{
if (_providers != null) {
ContextResolver<ObjectMapper> resolver = _providers.getContextResolver(ObjectMapper.class, mediaType);
/* Above should work as is, but due to this bug
* [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
* in Jersey, it doesn't. But this works until resolution of
* the issue:
*/
if (resolver == null) {
resolver = _providers.getContextResolver(ObjectMapper.class, null);
}
if (resolver != null) {
return resolver.getContext(type);
}
// 26-Nov-2024, tatu: [jakarta-rs#36] Look for JsonMapper primarily
ObjectMapper m =_locateMapperViaProvider(type, mediaType, JsonMapper.class, _providers);
if (m == null) {
m =_locateMapperViaProvider(type, mediaType, ObjectMapper.class, _providers);
}
return null;
return m;
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ Steven Schlansker (@stevenschlansker)

* Contributed #16: Add `JakartaRsFeature.READ_FULL_STREAM` to consume all content, on by default
(2.15.0)

Kevin Wooten (@kdubb)

* Suggested #36: Narrow types to format specific (e.g. CBORMapper) when resolving
via JAX-RS Providers
(2.19.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Sub-modules:

2.19.0 (not yet released)

#36: Narrow types to format specific (e.g. CBORMapper) when resolving
via JAX-RS Providers
(suggested by Kevin W)
* Woodstox dependency now 7.1.0

2.18.1 (28-Oct-2024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;

import com.fasterxml.jackson.dataformat.smile.SmileFactory;

import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
import com.fasterxml.jackson.jakarta.rs.base.ProviderBase;
import com.fasterxml.jackson.jakarta.rs.cfg.Annotations;

Expand Down Expand Up @@ -185,25 +184,17 @@ protected boolean hasMatchingMediaType(MediaType mediaType)
@Override
protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
{
if (_providers != null) {
ContextResolver<ObjectMapper> resolver = _providers.getContextResolver(ObjectMapper.class, mediaType);
/* Above should work as is, but due to this bug
* [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
* in Jersey, it doesn't. But this works until resolution of
* the issue:
*/
if (resolver == null) {
resolver = _providers.getContextResolver(ObjectMapper.class, null);
}
if (resolver != null) {
ObjectMapper mapper = resolver.getContext(type);
// 07-Feb-2014, tatu: just in case, ensure we have correct type
if (mapper.getFactory() instanceof SmileFactory) {
return mapper;
}
// 26-Nov-2024, tatu: [jakarta-rs#36] Look for SmileMapper primarily
ObjectMapper m = _locateMapperViaProvider(type, mediaType, SmileMapper.class, _providers);
if (m == null) {
// but if not found, try ObjectMapper
m =_locateMapperViaProvider(type, mediaType, ObjectMapper.class, _providers);
// 07-Feb-2014, tatu: just in case, ensure we have correct type
if ((m != null) && !(m.getFactory() instanceof SmileFactory)) {
m = null;
}
}
return null;
return m;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,7 @@ public XmlMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
XmlMapper m = _mapperConfig.getConfiguredMapper();
if (m == null) {
// If not, maybe we can get one configured via context?
if (_providers != null) {
ContextResolver<XmlMapper> resolver = _providers.getContextResolver(XmlMapper.class, mediaType);
/* Above should work as is, but due to this bug
* [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
* in Jersey, it doesn't. But this works until resolution of
* the issue:
*/
if (resolver == null) {
resolver = _providers.getContextResolver(XmlMapper.class, null);
}
if (resolver != null) {
m = resolver.getContext(type);
}
}
m = _locateMapperViaProvider(type, mediaType, XmlMapper.class, _providers);
if (m == null) {
// If not, let's get the fallback default instance
m = _mapperConfig.getDefaultMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,7 @@ public YAMLMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType) {
YAMLMapper m = _mapperConfig.getConfiguredMapper();
if (m == null) {
// If not, maybe we can get one configured via context?
if (_providers != null) {
ContextResolver<YAMLMapper> resolver = _providers.getContextResolver(YAMLMapper.class, mediaType);
/* Above should work as is, but due to this bug
* [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
* in Jersey, it doesn't. But this works until resolution of
* the issue:
*/
if (resolver == null) {
resolver = _providers.getContextResolver(YAMLMapper.class, null);
}
if (resolver != null) {
m = resolver.getContext(type);
}
}
m = _locateMapperViaProvider(type, mediaType, YAMLMapper.class, _providers);
if (m == null) {
// If not, let's get the fallback default instance
m = _mapperConfig.getDefaultMapper();
Expand Down

0 comments on commit 6ad5846

Please sign in to comment.