Polish "Add support for configuring default request and response preprocessors"
Closes gh-424
This commit is contained in:
@@ -26,21 +26,24 @@ import org.springframework.restdocs.operation.preprocess.OperationResponsePrepro
|
||||
import org.springframework.restdocs.operation.preprocess.Preprocessors;
|
||||
|
||||
/**
|
||||
* A configurer that can be used to configure the default operation preprocessors that need to be used.
|
||||
* A configurer that can be used to configure the default operation preprocessors.
|
||||
*
|
||||
* @param <PARENT> The type of the configurer's parent
|
||||
* @param <TYPE> The concrete type of the configurer to be returned from chained methods
|
||||
* @author Filip Hrisafov
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public abstract class OperationPreprocessorsConfigurer<PARENT, TYPE>
|
||||
extends AbstractNestedConfigurer<PARENT> {
|
||||
|
||||
private OperationRequestPreprocessor defaultOperationRequestPreprocessor;
|
||||
|
||||
private OperationResponsePreprocessor defaultOperationResponsePreprocessor;
|
||||
|
||||
/**
|
||||
* Creates a new {@code OperationPreprocessorConfigurer} with the given {@code parent}.
|
||||
* Creates a new {@code OperationPreprocessorConfigurer} with the given
|
||||
* {@code parent}.
|
||||
*
|
||||
* @param parent the parent
|
||||
*/
|
||||
@@ -49,34 +52,40 @@ public abstract class OperationPreprocessorsConfigurer<PARENT, TYPE>
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Map<String, Object> configuration, RestDocumentationContext context) {
|
||||
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
|
||||
public void apply(Map<String, Object> configuration,
|
||||
RestDocumentationContext context) {
|
||||
configuration.put(
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
|
||||
this.defaultOperationRequestPreprocessor);
|
||||
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
|
||||
configuration.put(
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
|
||||
this.defaultOperationResponsePreprocessor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the default documentation operation request preprocessors.
|
||||
* Configures the default operation request preprocessors.
|
||||
*
|
||||
* @param preprocessors the preprocessors
|
||||
* @return {@code this}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public TYPE withDefaultRequestPreprocessors(OperationPreprocessor... preprocessors) {
|
||||
this.defaultOperationRequestPreprocessor = Preprocessors.preprocessRequest(preprocessors);
|
||||
public TYPE withRequestDefaults(OperationPreprocessor... preprocessors) {
|
||||
this.defaultOperationRequestPreprocessor = Preprocessors
|
||||
.preprocessRequest(preprocessors);
|
||||
return (TYPE) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the default documentation operation response preprocessors.
|
||||
* Configures the default operation response preprocessors.
|
||||
*
|
||||
* @param preprocessors the preprocessors
|
||||
* @return {@code this}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public TYPE withDefaultResponsePreprocessors(OperationPreprocessor... preprocessors) {
|
||||
this.defaultOperationResponsePreprocessor = Preprocessors.preprocessResponse(preprocessors);
|
||||
public TYPE withResponseDefaults(OperationPreprocessor... preprocessors) {
|
||||
this.defaultOperationResponsePreprocessor = Preprocessors
|
||||
.preprocessResponse(preprocessors);
|
||||
return (TYPE) this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2017 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.
|
||||
@@ -35,7 +35,7 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||
/**
|
||||
* Abstract base class for the configuration of Spring REST Docs.
|
||||
*
|
||||
* @param <S> The concrete type of the {@link SnippetConfigurer}.
|
||||
* @param <S> The concrete type of the {@link SnippetConfigurer}
|
||||
* @param <P> The concrete type of the {@link OperationPreprocessorsConfigurer}
|
||||
* @param <T> The concrete type of this configurer, to be returned from methods that
|
||||
* support chaining
|
||||
@@ -58,8 +58,8 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
|
||||
public abstract S snippets();
|
||||
|
||||
/**
|
||||
* Returns an {@link OperationPreprocessorsConfigurer} that can be used to configure the operation request and
|
||||
* response preprocessors that will be used during the documentation.
|
||||
* Returns an {@link OperationPreprocessorsConfigurer} that can be used to configure
|
||||
* the operation request and response preprocessors that will be used.
|
||||
*
|
||||
* @return the operation preprocessors configurer
|
||||
*/
|
||||
@@ -100,8 +100,8 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
|
||||
protected final void apply(Map<String, Object> configuration,
|
||||
RestDocumentationContext context) {
|
||||
List<AbstractConfigurer> configurers = Arrays.asList(snippets(),
|
||||
operationPreprocessors(),
|
||||
this.templateEngineConfigurer, this.writerResolverConfigurer);
|
||||
operationPreprocessors(), this.templateEngineConfigurer,
|
||||
this.writerResolverConfigurer);
|
||||
for (AbstractConfigurer configurer : configurers) {
|
||||
configurer.apply(configuration, context);
|
||||
}
|
||||
|
||||
@@ -57,17 +57,16 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
public static final String ATTRIBUTE_NAME_DEFAULT_SNIPPETS = "org.springframework.restdocs.defaultSnippets";
|
||||
|
||||
/**
|
||||
* Name of the operation attribute used to hold the default operation request preprocessor.
|
||||
* Name of the operation attribute used to hold the default operation request
|
||||
* preprocessor.
|
||||
*/
|
||||
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR =
|
||||
"org.springframework.restdocs.defaultOperationRequestPreprocessor";
|
||||
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR = "org.springframework.restdocs.defaultOperationRequestPreprocessor";
|
||||
|
||||
/**
|
||||
* Name of the operation attribute used to hold the default operation response preprocessor.
|
||||
* Name of the operation attribute used to hold the default operation response
|
||||
* preprocessor.
|
||||
*/
|
||||
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR =
|
||||
"org.springframework.restdocs.defaultOperationResponsePreprocessor";
|
||||
|
||||
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR = "org.springframework.restdocs.defaultOperationResponsePreprocessor";
|
||||
|
||||
private final String identifier;
|
||||
|
||||
@@ -198,8 +197,10 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
*/
|
||||
public void handle(REQ request, RESP response, Map<String, Object> configuration) {
|
||||
Map<String, Object> attributes = new HashMap<>(configuration);
|
||||
OperationRequest operationRequest = preprocessRequest(request, attributes);
|
||||
OperationResponse operationResponse = preprocessResponse(response, attributes);
|
||||
OperationRequest operationRequest = preprocessRequest(
|
||||
this.requestConverter.convert(request), attributes);
|
||||
OperationResponse operationResponse = preprocessResponse(
|
||||
this.responseConverter.convert(response), attributes);
|
||||
Operation operation = new StandardOperation(this.identifier, operationRequest,
|
||||
operationResponse, attributes);
|
||||
try {
|
||||
@@ -239,40 +240,45 @@ public final class RestDocumentationGenerator<REQ, RESP> {
|
||||
return combinedSnippets;
|
||||
}
|
||||
|
||||
private OperationRequest preprocessRequest(REQ request, Map<String, Object> configuration) {
|
||||
List<OperationRequestPreprocessor> requestPreprocessors = getRequestPreprocessors(configuration);
|
||||
OperationRequest operationRequest = this.requestConverter.convert(request);
|
||||
private OperationRequest preprocessRequest(OperationRequest request,
|
||||
Map<String, Object> configuration) {
|
||||
List<OperationRequestPreprocessor> requestPreprocessors = getRequestPreprocessors(
|
||||
configuration);
|
||||
for (OperationRequestPreprocessor preprocessor : requestPreprocessors) {
|
||||
operationRequest = preprocessor.preprocess(operationRequest);
|
||||
request = preprocessor.preprocess(request);
|
||||
}
|
||||
return operationRequest;
|
||||
return request;
|
||||
}
|
||||
|
||||
private List<OperationRequestPreprocessor> getRequestPreprocessors(Map<String, Object> configuration) {
|
||||
List<OperationRequestPreprocessor> preprocessors = new ArrayList<>(2);
|
||||
preprocessors.add(this.requestPreprocessor);
|
||||
OperationRequestPreprocessor defaultRequestPreprocessor = (OperationRequestPreprocessor) configuration.get(
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR);
|
||||
if (defaultRequestPreprocessor != null) {
|
||||
preprocessors.add(defaultRequestPreprocessor);
|
||||
}
|
||||
return preprocessors;
|
||||
private List<OperationRequestPreprocessor> getRequestPreprocessors(
|
||||
Map<String, Object> configuration) {
|
||||
return getPreprocessors(this.requestPreprocessor,
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
|
||||
configuration);
|
||||
}
|
||||
|
||||
private OperationResponse preprocessResponse(RESP response, Map<String, Object> configuration) {
|
||||
List<OperationResponsePreprocessor> responsePreprocessors = getResponsePreprocessors(configuration);
|
||||
OperationResponse operationResponse = this.responseConverter.convert(response);
|
||||
for (OperationResponsePreprocessor preprocessor : responsePreprocessors) {
|
||||
operationResponse = preprocessor.preprocess(operationResponse);
|
||||
private OperationResponse preprocessResponse(OperationResponse response,
|
||||
Map<String, Object> configuration) {
|
||||
for (OperationResponsePreprocessor preprocessor : getResponsePreprocessors(
|
||||
configuration)) {
|
||||
response = preprocessor.preprocess(response);
|
||||
}
|
||||
return operationResponse;
|
||||
return response;
|
||||
}
|
||||
|
||||
private List<OperationResponsePreprocessor> getResponsePreprocessors(Map<String, Object> configuration) {
|
||||
List<OperationResponsePreprocessor> preprocessors = new ArrayList<>(2);
|
||||
preprocessors.add(this.responsePreprocessor);
|
||||
OperationResponsePreprocessor defaultResponsePreprocessor = (OperationResponsePreprocessor) configuration.get(
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR);
|
||||
private List<OperationResponsePreprocessor> getResponsePreprocessors(
|
||||
Map<String, Object> configuration) {
|
||||
return getPreprocessors(this.responsePreprocessor,
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
|
||||
configuration);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> List<T> getPreprocessors(T preprocessor, String preprocessorAttribute,
|
||||
Map<String, Object> configuration) {
|
||||
List<T> preprocessors = new ArrayList<>(2);
|
||||
preprocessors.add(preprocessor);
|
||||
T defaultResponsePreprocessor = (T) configuration.get(preprocessorAttribute);
|
||||
if (defaultResponsePreprocessor != null) {
|
||||
preprocessors.add(defaultResponsePreprocessor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user