Fix issue with matchesJson when JSONPath is a String
GraphQlTransport returns ExecutionResult with parsed JSON so for the most part having MappingProvider is enough. However, when a JSONPath evaluates to String (vs Map or List), the JsonSmartJsonProvider used by default, throws UnsupportedOperationException from toJson.
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.jayway.jsonpath.Configuration;
|
||||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
|
||||
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
|
||||
import com.jayway.jsonpath.spi.mapper.MappingProvider;
|
||||
import graphql.GraphQLError;
|
||||
@@ -133,11 +134,31 @@ public abstract class AbstractGraphQlTesterBuilder<B extends AbstractGraphQlTest
|
||||
|
||||
private static class Jackson2Configurer {
|
||||
|
||||
private static final MappingProvider defaultProvider = Configuration.defaultConfiguration().mappingProvider();
|
||||
private static final Class<?> defaultJsonProviderType;
|
||||
|
||||
private static final Class<?> defaultMappingProviderType;
|
||||
|
||||
static {
|
||||
Configuration config = Configuration.defaultConfiguration();
|
||||
defaultJsonProviderType = config.jsonProvider().getClass();
|
||||
defaultMappingProviderType = config.mappingProvider().getClass();
|
||||
}
|
||||
|
||||
// GraphQlTransport returns ExecutionResult with JSON parsed to Map/List,
|
||||
// but we still need JsonProvider for matchesJson(String)
|
||||
|
||||
static Configuration configure(Configuration config) {
|
||||
return (config.mappingProvider() != null && config.mappingProvider() != defaultProvider ? config :
|
||||
config.mappingProvider(new JacksonMappingProvider()));
|
||||
if (isDefault(config.jsonProvider(), defaultJsonProviderType)) {
|
||||
config = config.jsonProvider(new JacksonJsonProvider());
|
||||
}
|
||||
if (isDefault(config.mappingProvider(), defaultMappingProviderType)) {
|
||||
config = config.mappingProvider(new JacksonMappingProvider());
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private static <T> boolean isDefault(@Nullable T provider, Class<? extends T> defaultProviderType) {
|
||||
return (provider == null || defaultProviderType.isInstance(provider));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -82,8 +82,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
|
||||
|
||||
response.path("").matchesJson("{\"me\": {\"name\":\"Luke Skywalker\",\"friends\":[]}}");
|
||||
response.path("me").matchesJson("{\"name\":\"Luke Skywalker\"}");
|
||||
response.path("me").matchesJson("{\"friends\":[]}"); // lenient match with subset of
|
||||
// fields
|
||||
response.path("me").matchesJson("{\"friends\":[]}"); // lenient match with subset of fields
|
||||
|
||||
assertThatThrownBy(() -> response.path("me").matchesJsonStrictly("{\"friends\":[]}"))
|
||||
.as("Extended fields should fail in strict mode")
|
||||
|
||||
@@ -190,6 +190,7 @@ public class WebGraphQlTesterBuilderTests {
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
response.path("me").entity(MovieCharacter.class).isEqualTo(character);
|
||||
response.path("me").matchesJson("{name:\"Luke Skywalker\"}");
|
||||
assertThat(testDecoder.getLastValue()).isEqualTo(character);
|
||||
}
|
||||
|
||||
|
||||
@@ -112,11 +112,18 @@ public abstract class AbstractGraphQlClientBuilder<B extends AbstractGraphQlClie
|
||||
|
||||
private static class Jackson2Configurer {
|
||||
|
||||
private static final MappingProvider defaultProvider = Configuration.defaultConfiguration().mappingProvider();
|
||||
private static final Class<?> defaultMappingProviderType =
|
||||
Configuration.defaultConfiguration().mappingProvider().getClass();
|
||||
|
||||
// We only need a MappingProvider:
|
||||
// GraphQlTransport returns ExecutionResult with JSON parsed to Map/List
|
||||
|
||||
static Configuration configure(Configuration config) {
|
||||
return (config.mappingProvider() != null && config.mappingProvider() != defaultProvider ? config :
|
||||
config.mappingProvider(new JacksonMappingProvider()));
|
||||
MappingProvider provider = config.mappingProvider();
|
||||
if (provider == null || defaultMappingProviderType.isInstance(provider)) {
|
||||
config = config.mappingProvider(new JacksonMappingProvider());
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user