Read snippet templates as UTF-8 by default
Fixes gh-585
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2020 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.restdocs.config;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -116,7 +117,8 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
|
||||
}
|
||||
engineToUse = new MustacheTemplateEngine(
|
||||
new StandardTemplateResourceResolver(snippetConfiguration.getTemplateFormat()),
|
||||
Mustache.compiler().escapeHTML(false), templateContext);
|
||||
Charset.forName(snippetConfiguration.getEncoding()), Mustache.compiler().escapeHTML(false),
|
||||
templateContext);
|
||||
}
|
||||
configuration.put(TemplateEngine.class.getName(), engineToUse);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2020 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.
|
||||
@@ -18,6 +18,8 @@ package org.springframework.restdocs.templates.mustache;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -40,23 +42,38 @@ public class MustacheTemplateEngine implements TemplateEngine {
|
||||
|
||||
private final TemplateResourceResolver templateResourceResolver;
|
||||
|
||||
private final Charset templateEncoding;
|
||||
|
||||
private final Compiler compiler;
|
||||
|
||||
private final Map<String, Object> context;
|
||||
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve template paths.
|
||||
* {@code templateResourceResolver} to resolve template paths. Templates will be read
|
||||
* as UTF-8.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
*/
|
||||
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver) {
|
||||
this(templateResourceResolver, Mustache.compiler().escapeHTML(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve template paths, reading them using the
|
||||
* given {@code templateEncoding}.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
* @param templateEncoding the charset to use when reading the templates
|
||||
* @since 2.0.5
|
||||
*/
|
||||
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding) {
|
||||
this(templateResourceResolver, templateEncoding, Mustache.compiler().escapeHTML(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve templates and the given
|
||||
* {@code compiler} to compile them.
|
||||
* {@code compiler} to compile them. Templates will be read as UTF-8.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
* @param compiler the compiler to use
|
||||
*/
|
||||
@@ -67,8 +84,23 @@ public class MustacheTemplateEngine implements TemplateEngine {
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve templates and the given
|
||||
* {@code compiler} to compile them. Compiled templates will be created with the given
|
||||
* {@code context}.
|
||||
* {@code compiler} to compile them. Templates will be read using the given
|
||||
* {@code templateEncoding}.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
* @param templateEncoding the charset to use when reading the templates
|
||||
* @param compiler the compiler to use
|
||||
* @since 2.0.5
|
||||
*/
|
||||
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding,
|
||||
Compiler compiler) {
|
||||
this(templateResourceResolver, templateEncoding, compiler, Collections.<String, Object>emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve templates. Templates will be read as
|
||||
* UTF-8. Once read, the given {@code compiler} will be used to compile them. Compiled
|
||||
* templates will be created with the given {@code context}.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
* @param compiler the compiler to use
|
||||
* @param context the context to pass to compiled templates
|
||||
@@ -77,7 +109,27 @@ public class MustacheTemplateEngine implements TemplateEngine {
|
||||
*/
|
||||
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Compiler compiler,
|
||||
Map<String, Object> context) {
|
||||
this(templateResourceResolver, StandardCharsets.UTF_8, compiler, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code MustacheTemplateEngine} that will use the given
|
||||
* {@code templateResourceResolver} to resolve templates. Template will be read using
|
||||
* the given {@code templateEncoding}. Once read, the given {@code compiler} will be
|
||||
* used to compile them. Compiled templates will be created with the given
|
||||
* {@code context}.
|
||||
* @param templateResourceResolver the resolver to use
|
||||
* @param templateEncoding the charset to use when reading the templates
|
||||
* @param compiler the compiler to use
|
||||
* @param context the context to pass to compiled templates
|
||||
* @since 2.0.5
|
||||
* @see MustacheTemplate#MustacheTemplate(org.springframework.restdocs.mustache.Template,
|
||||
* Map)
|
||||
*/
|
||||
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding,
|
||||
Compiler compiler, Map<String, Object> context) {
|
||||
this.templateResourceResolver = templateResourceResolver;
|
||||
this.templateEncoding = templateEncoding;
|
||||
this.compiler = compiler;
|
||||
this.context = context;
|
||||
}
|
||||
@@ -85,7 +137,8 @@ public class MustacheTemplateEngine implements TemplateEngine {
|
||||
@Override
|
||||
public Template compileTemplate(String name) throws IOException {
|
||||
Resource templateResource = this.templateResourceResolver.resolveTemplateResource(name);
|
||||
return new MustacheTemplate(this.compiler.compile(new InputStreamReader(templateResource.getInputStream())),
|
||||
return new MustacheTemplate(
|
||||
this.compiler.compile(new InputStreamReader(templateResource.getInputStream(), this.templateEncoding)),
|
||||
this.context);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2020 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.restdocs.config;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -73,6 +74,8 @@ public class RestDocumentationConfigurerTests {
|
||||
this.configurer.apply(configuration, createContext());
|
||||
assertThat(configuration).containsKey(TemplateEngine.class.getName());
|
||||
assertThat(configuration.get(TemplateEngine.class.getName())).isInstanceOf(MustacheTemplateEngine.class);
|
||||
assertThat(configuration.get(TemplateEngine.class.getName())).hasFieldOrPropertyWithValue("templateEncoding",
|
||||
StandardCharsets.UTF_8);
|
||||
assertThat(configuration).containsKey(WriterResolver.class.getName());
|
||||
assertThat(configuration.get(WriterResolver.class.getName())).isInstanceOf(StandardWriterResolver.class);
|
||||
assertThat(configuration).containsKey(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS);
|
||||
@@ -147,12 +150,15 @@ public class RestDocumentationConfigurerTests {
|
||||
@Test
|
||||
public void customSnippetEncoding() {
|
||||
Map<String, Object> configuration = new HashMap<>();
|
||||
this.configurer.snippets().withEncoding("ISO 8859-1").apply(configuration, createContext());
|
||||
this.configurer.snippets().withEncoding("ISO-8859-1");
|
||||
this.configurer.apply(configuration, createContext());
|
||||
assertThat(configuration).containsKey(SnippetConfiguration.class.getName());
|
||||
assertThat(configuration.get(SnippetConfiguration.class.getName())).isInstanceOf(SnippetConfiguration.class);
|
||||
SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration
|
||||
.get(SnippetConfiguration.class.getName());
|
||||
assertThat(snippetConfiguration.getEncoding()).isEqualTo("ISO 8859-1");
|
||||
assertThat(snippetConfiguration.getEncoding()).isEqualTo(StandardCharsets.ISO_8859_1.displayName());
|
||||
assertThat(configuration.get(TemplateEngine.class.getName())).hasFieldOrPropertyWithValue("templateEncoding",
|
||||
StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user