|
28 | 28 | import java.util.function.Consumer; |
29 | 29 | import java.util.function.Function; |
30 | 30 |
|
| 31 | +import com.jayway.jsonpath.Configuration; |
| 32 | +import com.jayway.jsonpath.TypeRef; |
| 33 | +import com.jayway.jsonpath.spi.mapper.MappingProvider; |
31 | 34 | import org.jspecify.annotations.Nullable; |
32 | 35 |
|
33 | 36 | import org.springframework.core.ParameterizedTypeReference; |
| 37 | +import org.springframework.core.ResolvableType; |
34 | 38 | import org.springframework.http.HttpHeaders; |
35 | 39 | import org.springframework.http.HttpMethod; |
36 | 40 | import org.springframework.http.HttpRequest; |
@@ -486,7 +490,8 @@ public BodyContentSpec xml(String expectedXml) { |
486 | 490 |
|
487 | 491 | @Override |
488 | 492 | public JsonPathAssertions jsonPath(String expression) { |
489 | | - return new JsonPathAssertions(this, getBodyAsString(), expression, null); |
| 493 | + Configuration config = JsonPathConfigurationProvider.getConfiguration(this.result); |
| 494 | + return new JsonPathAssertions(this, getBodyAsString(), expression, config); |
490 | 495 | } |
491 | 496 |
|
492 | 497 | @Override |
@@ -540,4 +545,37 @@ public byte[] getRequestContent(String requestId) { |
540 | 545 | } |
541 | 546 | } |
542 | 547 |
|
| 548 | + |
| 549 | + private static class JsonPathConfigurationProvider { |
| 550 | + |
| 551 | + static Configuration getConfiguration(EntityExchangeResult<?> result) { |
| 552 | + Configuration config = Configuration.defaultConfiguration(); |
| 553 | + JsonConverterDelegate delegate = result.getJsonConverterDelegate(); |
| 554 | + return (delegate != null ? config.mappingProvider(new MessageConverterMappingProvider(delegate)) : config); |
| 555 | + } |
| 556 | + } |
| 557 | + |
| 558 | + |
| 559 | + private record MessageConverterMappingProvider(JsonConverterDelegate delegate) implements MappingProvider { |
| 560 | + |
| 561 | + @Override |
| 562 | + public <T> T map(Object value, Class<T> targetType, Configuration configuration) { |
| 563 | + return mapToTargetType(value, ResolvableType.forClass(targetType)); |
| 564 | + } |
| 565 | + |
| 566 | + @Override |
| 567 | + public <T> T map(Object value, TypeRef<T> targetType, Configuration configuration) { |
| 568 | + return mapToTargetType(value, ResolvableType.forType(targetType.getType())); |
| 569 | + } |
| 570 | + |
| 571 | + private <T> T mapToTargetType(Object value, ResolvableType targetType) { |
| 572 | + try { |
| 573 | + return delegate().map(value, targetType); |
| 574 | + } |
| 575 | + catch (IOException ex) { |
| 576 | + throw new IllegalStateException("Failed to map " + value + " to " + targetType, ex); |
| 577 | + } |
| 578 | + } |
| 579 | + } |
| 580 | + |
543 | 581 | } |
0 commit comments