-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure jsonpath MappingProvider in WebTestClient
This commit improves jsonpath support in WebTestClient by detecting a suitable json encoder/decoder that can be applied to assert more complex data structure. Closes gh-31653
- Loading branch information
Showing
10 changed files
with
481 additions
and
58 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
84 changes: 84 additions & 0 deletions
84
...main/java/org/springframework/test/web/reactive/server/EncoderDecoderMappingProvider.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,84 @@ | ||
/* | ||
* Copyright 2002-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.web.reactive.server; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import com.jayway.jsonpath.Configuration; | ||
import com.jayway.jsonpath.TypeRef; | ||
import com.jayway.jsonpath.spi.mapper.MappingProvider; | ||
|
||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.codec.Decoder; | ||
import org.springframework.core.codec.Encoder; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.core.io.buffer.DataBufferFactory; | ||
import org.springframework.core.io.buffer.DefaultDataBufferFactory; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.MimeType; | ||
import org.springframework.util.MimeTypeUtils; | ||
|
||
/** | ||
* JSON Path {@link MappingProvider} implementation using {@link Encoder} | ||
* and {@link Decoder}. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @author Stephane Nicoll | ||
* @since 6.2 | ||
*/ | ||
final class EncoderDecoderMappingProvider implements MappingProvider { | ||
|
||
private final Encoder<?> encoder; | ||
|
||
private final Decoder<?> decoder; | ||
|
||
/** | ||
* Create an instance with the specified writers and readers. | ||
*/ | ||
public EncoderDecoderMappingProvider(Encoder<?> encoder, Decoder<?> decoder) { | ||
this.encoder = encoder; | ||
this.decoder = decoder; | ||
} | ||
|
||
|
||
@Nullable | ||
@Override | ||
public <T> T map(Object source, Class<T> targetType, Configuration configuration) { | ||
return mapToTargetType(source, ResolvableType.forClass(targetType)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) { | ||
return mapToTargetType(source, ResolvableType.forType(targetType.getType())); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
private <T> T mapToTargetType(Object source, ResolvableType targetType) { | ||
DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance; | ||
MimeType mimeType = MimeTypeUtils.APPLICATION_JSON; | ||
Map<String, Object> hints = Collections.emptyMap(); | ||
|
||
DataBuffer buffer = ((Encoder<T>) this.encoder).encodeValue( | ||
(T) source, bufferFactory, ResolvableType.forInstance(source), mimeType, hints); | ||
|
||
return ((Decoder<T>) this.decoder).decode(buffer, targetType, mimeType, hints); | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
...g-test/src/main/java/org/springframework/test/web/reactive/server/JsonEncoderDecoder.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,112 @@ | ||
/* | ||
* Copyright 2002-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.web.reactive.server; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.codec.Decoder; | ||
import org.springframework.core.codec.Encoder; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.codec.DecoderHttpMessageReader; | ||
import org.springframework.http.codec.EncoderHttpMessageWriter; | ||
import org.springframework.http.codec.HttpMessageReader; | ||
import org.springframework.http.codec.HttpMessageWriter; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* {@link Encoder} and {@link Decoder} that is able to handle a map to and from | ||
* json. Used to configure the jsonpath infrastructure without having a hard | ||
* dependency on the library. | ||
* | ||
* @param encoder the json encoder | ||
* @param decoder the json decoder | ||
* @author Stephane Nicoll | ||
* @author Rossen Stoyanchev | ||
* @since 6.2 | ||
*/ | ||
record JsonEncoderDecoder(Encoder<?> encoder, Decoder<?> decoder) { | ||
|
||
private static final ResolvableType MAP_TYPE = ResolvableType.forClass(Map.class); | ||
|
||
|
||
/** | ||
* Create a {@link JsonEncoderDecoder} instance based on the specified | ||
* infrastructure. | ||
* @param messageWriters the HTTP message writers | ||
* @param messageReaders the HTTP message readers | ||
* @return a {@link JsonEncoderDecoder} or {@code null} if a suitable codec | ||
* is not available | ||
*/ | ||
@Nullable | ||
static JsonEncoderDecoder from(Collection<HttpMessageWriter<?>> messageWriters, | ||
Collection<HttpMessageReader<?>> messageReaders) { | ||
|
||
Encoder<?> jsonEncoder = findJsonEncoder(messageWriters); | ||
Decoder<?> jsonDecoder = findJsonDecoder(messageReaders); | ||
if (jsonEncoder != null && jsonDecoder != null) { | ||
return new JsonEncoderDecoder(jsonEncoder, jsonDecoder); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* Find the first suitable {@link Encoder} that can encode a {@link Map} | ||
* to json. | ||
* @param writers the writers to inspect | ||
* @return a suitable json {@link Encoder} or {@code null} | ||
*/ | ||
@Nullable | ||
private static Encoder<?> findJsonEncoder(Collection<HttpMessageWriter<?>> writers) { | ||
return findJsonEncoder(writers.stream() | ||
.filter(writer -> writer instanceof EncoderHttpMessageWriter) | ||
.map(writer -> ((EncoderHttpMessageWriter<?>) writer).getEncoder())); | ||
} | ||
|
||
@Nullable | ||
private static Encoder<?> findJsonEncoder(Stream<Encoder<?>> stream) { | ||
return stream | ||
.filter(encoder -> encoder.canEncode(MAP_TYPE, MediaType.APPLICATION_JSON)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
/** | ||
* Find the first suitable {@link Decoder} that can decode a {@link Map} to | ||
* json. | ||
* @param readers the readers to inspect | ||
* @return a suitable json {@link Decoder} or {@code null} | ||
*/ | ||
@Nullable | ||
private static Decoder<?> findJsonDecoder(Collection<HttpMessageReader<?>> readers) { | ||
return findJsonDecoder(readers.stream() | ||
.filter(reader -> reader instanceof DecoderHttpMessageReader) | ||
.map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder())); | ||
} | ||
|
||
@Nullable | ||
private static Decoder<?> findJsonDecoder(Stream<Decoder<?>> decoderStream) { | ||
return decoderStream | ||
.filter(decoder -> decoder.canDecode(MAP_TYPE, MediaType.APPLICATION_JSON)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
} |
Oops, something went wrong.