Skip to content

Commit

Permalink
Fix #200
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 27, 2024
1 parent 159583b commit 9a2d2ff
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Providers;

import com.fasterxml.jackson.core.*;

Expand Down Expand Up @@ -980,6 +982,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,6 +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.jaxrs.base.ProviderBase;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;

Expand Down Expand Up @@ -154,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 @@ -183,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,6 +8,7 @@

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

Expand Down Expand Up @@ -194,21 +195,12 @@ 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) {
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
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Sub-modules:

2.19.0 (not yet released)

#200: Narrow types to format specific (e.g. CBORMapper) when resolving
via JAX-RS Providers
* 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,6 +9,7 @@
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.jaxrs.base.ProviderBase;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;

Expand Down Expand Up @@ -182,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 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 @@ -199,20 +199,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 9a2d2ff

Please sign in to comment.