Add support for using WebFlux's WebTestClient to document an API
Closes gh-384
This commit is contained in:
@@ -23,7 +23,6 @@ import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.Extension;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
import org.junit.jupiter.api.extension.TestExtensionContext;
|
||||
|
||||
@@ -59,14 +58,14 @@ public class RestDocumentationExtension implements Extension, BeforeEachCallback
|
||||
|
||||
@Override
|
||||
public boolean supports(ParameterContext parameterContext,
|
||||
ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
ExtensionContext extensionContext) {
|
||||
return RestDocumentationContextProvider.class
|
||||
.isAssignableFrom(parameterContext.getParameter().getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolve(ParameterContext parameterContext,
|
||||
ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
ExtensionContext extensionContext) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
import org.springframework.restdocs.operation.OperationRequest;
|
||||
@@ -242,12 +243,13 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
|
||||
private OperationRequest preprocessRequest(OperationRequest request,
|
||||
Map<String, Object> configuration) {
|
||||
List<OperationRequestPreprocessor> requestPreprocessors = getRequestPreprocessors(
|
||||
configuration);
|
||||
for (OperationRequestPreprocessor preprocessor : requestPreprocessors) {
|
||||
request = preprocessor.preprocess(request);
|
||||
}
|
||||
return request;
|
||||
return preprocess(getRequestPreprocessors(configuration), request,
|
||||
this::preprocess);
|
||||
}
|
||||
|
||||
private OperationRequest preprocess(OperationRequestPreprocessor preprocessor,
|
||||
OperationRequest request) {
|
||||
return preprocessor.preprocess(request);
|
||||
}
|
||||
|
||||
private List<OperationRequestPreprocessor> getRequestPreprocessors(
|
||||
@@ -259,11 +261,13 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
|
||||
private OperationResponse preprocessResponse(OperationResponse response,
|
||||
Map<String, Object> configuration) {
|
||||
for (OperationResponsePreprocessor preprocessor : getResponsePreprocessors(
|
||||
configuration)) {
|
||||
response = preprocessor.preprocess(response);
|
||||
}
|
||||
return response;
|
||||
return preprocess(getResponsePreprocessors(configuration), response,
|
||||
this::preprocess);
|
||||
}
|
||||
|
||||
private OperationResponse preprocess(OperationResponsePreprocessor preprocessor,
|
||||
OperationResponse response) {
|
||||
return preprocessor.preprocess(response);
|
||||
}
|
||||
|
||||
private List<OperationResponsePreprocessor> getResponsePreprocessors(
|
||||
@@ -273,6 +277,15 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
configuration);
|
||||
}
|
||||
|
||||
private <P, T> T preprocess(List<P> preprocessors, T target,
|
||||
BiFunction<P, T, T> function) {
|
||||
T processed = target;
|
||||
for (P preprocessor : preprocessors) {
|
||||
processed = function.apply(preprocessor, processed);
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> List<T> getPreprocessors(T preprocessor, String preprocessorAttribute,
|
||||
Map<String, Object> configuration) {
|
||||
|
||||
@@ -49,11 +49,6 @@ final class JsonFieldProcessor {
|
||||
matches.add(match.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void absent() {
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
if (matches.isEmpty()) {
|
||||
throw new FieldDoesNotExistException(path);
|
||||
@@ -72,11 +67,6 @@ final class JsonFieldProcessor {
|
||||
match.remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void absent() {
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@@ -89,11 +79,6 @@ final class JsonFieldProcessor {
|
||||
match.removeSubsection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void absent() {
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@@ -376,7 +361,8 @@ final class JsonFieldProcessor {
|
||||
|
||||
void foundMatch(Match match);
|
||||
|
||||
void absent();
|
||||
default void absent() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user