Make it easier to provide a placeholder resolver that uses the context
Closes gh-235
This commit is contained in:
@@ -23,7 +23,7 @@ import java.util.Map;
|
||||
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.restdocs.mustache.Mustache;
|
||||
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver;
|
||||
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolverFactory;
|
||||
import org.springframework.restdocs.snippet.StandardWriterResolver;
|
||||
import org.springframework.restdocs.snippet.WriterResolver;
|
||||
import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
|
||||
@@ -138,7 +138,7 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
|
||||
SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration
|
||||
.get(SnippetConfiguration.class.getName());
|
||||
resolverToUse = new StandardWriterResolver(
|
||||
new RestDocumentationContextPlaceholderResolver(context),
|
||||
new RestDocumentationContextPlaceholderResolverFactory(),
|
||||
snippetConfiguration.getEncoding(),
|
||||
snippetConfiguration.getTemplateFormat());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2014-2016 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
|
||||
*
|
||||
* http://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.restdocs.snippet;
|
||||
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
|
||||
/**
|
||||
* A factory for creating {@link PlaceholderResolver} instances.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1
|
||||
*/
|
||||
public interface PlaceholderResolverFactory {
|
||||
|
||||
/**
|
||||
* Creates a new {@link PlaceholderResolver} using the given {@code context}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the placeholder resolver
|
||||
*/
|
||||
PlaceholderResolver create(RestDocumentationContext context);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2014-2016 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
|
||||
*
|
||||
* http://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.restdocs.snippet;
|
||||
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
|
||||
/**
|
||||
* A {@link PlaceholderResolverFactory} that creates
|
||||
* {@link RestDocumentationContextPlaceholderResolver} instances.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1
|
||||
*/
|
||||
public final class RestDocumentationContextPlaceholderResolverFactory
|
||||
implements PlaceholderResolverFactory {
|
||||
|
||||
@Override
|
||||
public PlaceholderResolver create(RestDocumentationContext context) {
|
||||
return new RestDocumentationContextPlaceholderResolver(context);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,7 +35,7 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
*/
|
||||
public final class StandardWriterResolver implements WriterResolver {
|
||||
|
||||
private final PlaceholderResolver placeholderResolver;
|
||||
private final PlaceholderResolverFactory placeholderResolverFactory;
|
||||
|
||||
private final PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper(
|
||||
"{", "}");
|
||||
@@ -52,27 +52,29 @@ public final class StandardWriterResolver implements WriterResolver {
|
||||
*
|
||||
* @param placeholderResolver the placeholder resolver
|
||||
* @deprecated since 1.1.0 in favor of
|
||||
* {@link #StandardWriterResolver(PropertyPlaceholderHelper.PlaceholderResolver, String, TemplateFormat)}
|
||||
* {@link #StandardWriterResolver(PlaceholderResolverFactory, String, TemplateFormat)}
|
||||
*/
|
||||
@Deprecated
|
||||
public StandardWriterResolver(PlaceholderResolver placeholderResolver) {
|
||||
this(placeholderResolver, "UTF-8", TemplateFormats.asciidoctor());
|
||||
this(new SingleInstancePlaceholderResolverFactory(placeholderResolver), "UTF-8",
|
||||
TemplateFormats.asciidoctor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code StandardWriterResolver} that will use the given
|
||||
* {@code placeholderResolver} to resolve any placeholders in the
|
||||
* Creates a new {@code StandardWriterResolver} that will use a
|
||||
* {@link PlaceholderResolver} created from the given
|
||||
* {@code placeholderResolverFactory} to resolve any placeholders in the
|
||||
* {@code operationName}. Writers will use the given {@code encoding} and, when
|
||||
* writing to a file, will use a filename appropriate for content generated from
|
||||
* templates in the given {@code templateFormat}.
|
||||
*
|
||||
* @param placeholderResolver the placeholder resolver
|
||||
* @param placeholderResolverFactory the placeholder resolver factory
|
||||
* @param encoding the encoding
|
||||
* @param templateFormat the snippet format
|
||||
*/
|
||||
public StandardWriterResolver(PlaceholderResolver placeholderResolver,
|
||||
public StandardWriterResolver(PlaceholderResolverFactory placeholderResolverFactory,
|
||||
String encoding, TemplateFormat templateFormat) {
|
||||
this.placeholderResolver = placeholderResolver;
|
||||
this.placeholderResolverFactory = placeholderResolverFactory;
|
||||
this.encoding = encoding;
|
||||
this.templateFormat = templateFormat;
|
||||
}
|
||||
@@ -82,7 +84,7 @@ public final class StandardWriterResolver implements WriterResolver {
|
||||
RestDocumentationContext context) throws IOException {
|
||||
File outputFile = resolveFile(
|
||||
this.propertyPlaceholderHelper.replacePlaceholders(operationName,
|
||||
this.placeholderResolver),
|
||||
this.placeholderResolverFactory.create(context)),
|
||||
snippetName + "." + this.templateFormat.getFileExtension(), context);
|
||||
|
||||
if (outputFile != null) {
|
||||
@@ -127,4 +129,21 @@ public final class StandardWriterResolver implements WriterResolver {
|
||||
}
|
||||
}
|
||||
|
||||
private static final class SingleInstancePlaceholderResolverFactory
|
||||
implements PlaceholderResolverFactory {
|
||||
|
||||
private final PlaceholderResolver placeholderResolver;
|
||||
|
||||
private SingleInstancePlaceholderResolverFactory(
|
||||
PlaceholderResolver placeholderResolver) {
|
||||
this.placeholderResolver = placeholderResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlaceholderResolver create(RestDocumentationContext context) {
|
||||
return this.placeholderResolver;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.restdocs.ManualRestDocumentation;
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -36,11 +35,11 @@ import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor
|
||||
*/
|
||||
public class StandardWriterResolverTests {
|
||||
|
||||
private final PlaceholderResolver placeholderResolver = mock(
|
||||
PlaceholderResolver.class);
|
||||
private final PlaceholderResolverFactory placeholderResolverFactory = mock(
|
||||
PlaceholderResolverFactory.class);
|
||||
|
||||
private final StandardWriterResolver resolver = new StandardWriterResolver(
|
||||
this.placeholderResolver, "UTF-8", asciidoctor());
|
||||
this.placeholderResolverFactory, "UTF-8", asciidoctor());
|
||||
|
||||
@Test
|
||||
public void absoluteInput() {
|
||||
|
||||
@@ -39,7 +39,7 @@ import org.springframework.restdocs.operation.OperationResponse;
|
||||
import org.springframework.restdocs.operation.OperationResponseFactory;
|
||||
import org.springframework.restdocs.operation.Parameters;
|
||||
import org.springframework.restdocs.operation.StandardOperation;
|
||||
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver;
|
||||
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolverFactory;
|
||||
import org.springframework.restdocs.snippet.StandardWriterResolver;
|
||||
import org.springframework.restdocs.snippet.WriterResolver;
|
||||
import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
|
||||
@@ -107,7 +107,7 @@ public class OperationBuilder {
|
||||
this.attributes.put(RestDocumentationContext.class.getName(), context);
|
||||
this.attributes.put(WriterResolver.class.getName(),
|
||||
new StandardWriterResolver(
|
||||
new RestDocumentationContextPlaceholderResolver(context), "UTF-8",
|
||||
new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8",
|
||||
this.templateFormat));
|
||||
return new StandardOperation(this.name,
|
||||
(this.requestBuilder == null
|
||||
|
||||
Reference in New Issue
Block a user