Skip to content

Commit

Permalink
Disable Jackson's buffer recyling feature for WebFlux
Browse files Browse the repository at this point in the history
Issue: SPR-17193
  • Loading branch information
rstoyanchev committed Sep 6, 2018
1 parent 4b6a5fb commit 92bb76f
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,11 +55,20 @@
*/
public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport implements HttpMessageDecoder<Object> {

/**
* Until https://github.com/FasterXML/jackson-core/issues/476 is resolved,
* we need to ensure buffer recycling is off.
*/
private final JsonFactory jsonFactory;


/**
* Constructor with a Jackson {@link ObjectMapper} to use.
*/
protected AbstractJackson2Decoder(ObjectMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
this.jsonFactory = mapper.getFactory().copy()
.disable(JsonFactory.Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING);
}


Expand All @@ -75,24 +84,18 @@ public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

Flux<TokenBuffer> tokens = tokenize(input, true);
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, true);
return decodeInternal(tokens, elementType, mimeType, hints);
}

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

Flux<TokenBuffer> tokens = tokenize(input, false);
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, false);
return decodeInternal(tokens, elementType, mimeType, hints).singleOrEmpty();
}

private Flux<TokenBuffer> tokenize(Publisher<DataBuffer> input, boolean tokenizeArrayElements) {
Flux<DataBuffer> inputFlux = Flux.from(input);
JsonFactory factory = getObjectMapper().getFactory();
return Jackson2Tokenizer.tokenize(inputFlux, factory, tokenizeArrayElements);
}

private Flux<Object> decodeInternal(Flux<TokenBuffer> tokens, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

Expand Down

0 comments on commit 92bb76f

Please sign in to comment.