Skip to content

Commit 9162888

Browse files
committed
Read snippet templates as UTF-8 by default
Fixes gh-585
1 parent c203710 commit 9162888

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.restdocs.config;
1818

19+
import java.nio.charset.Charset;
1920
import java.util.Arrays;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -116,7 +117,8 @@ public void apply(Map<String, Object> configuration, RestDocumentationContext co
116117
}
117118
engineToUse = new MustacheTemplateEngine(
118119
new StandardTemplateResourceResolver(snippetConfiguration.getTemplateFormat()),
119-
Mustache.compiler().escapeHTML(false), templateContext);
120+
Charset.forName(snippetConfiguration.getEncoding()), Mustache.compiler().escapeHTML(false),
121+
templateContext);
120122
}
121123
configuration.put(TemplateEngine.class.getName(), engineToUse);
122124
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java

+59-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStreamReader;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2123
import java.util.Collections;
2224
import java.util.Map;
2325

@@ -40,23 +42,38 @@ public class MustacheTemplateEngine implements TemplateEngine {
4042

4143
private final TemplateResourceResolver templateResourceResolver;
4244

45+
private final Charset templateEncoding;
46+
4347
private final Compiler compiler;
4448

4549
private final Map<String, Object> context;
4650

4751
/**
4852
* Creates a new {@code MustacheTemplateEngine} that will use the given
49-
* {@code templateResourceResolver} to resolve template paths.
53+
* {@code templateResourceResolver} to resolve template paths. Templates will be read
54+
* as UTF-8.
5055
* @param templateResourceResolver the resolver to use
5156
*/
5257
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver) {
5358
this(templateResourceResolver, Mustache.compiler().escapeHTML(false));
5459
}
5560

61+
/**
62+
* Creates a new {@code MustacheTemplateEngine} that will use the given
63+
* {@code templateResourceResolver} to resolve template paths, reading them using the
64+
* given {@code templateEncoding}.
65+
* @param templateResourceResolver the resolver to use
66+
* @param templateEncoding the charset to use when reading the templates
67+
* @since 2.0.5
68+
*/
69+
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding) {
70+
this(templateResourceResolver, templateEncoding, Mustache.compiler().escapeHTML(false));
71+
}
72+
5673
/**
5774
* Creates a new {@code MustacheTemplateEngine} that will use the given
5875
* {@code templateResourceResolver} to resolve templates and the given
59-
* {@code compiler} to compile them.
76+
* {@code compiler} to compile them. Templates will be read as UTF-8.
6077
* @param templateResourceResolver the resolver to use
6178
* @param compiler the compiler to use
6279
*/
@@ -67,8 +84,23 @@ public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver,
6784
/**
6885
* Creates a new {@code MustacheTemplateEngine} that will use the given
6986
* {@code templateResourceResolver} to resolve templates and the given
70-
* {@code compiler} to compile them. Compiled templates will be created with the given
71-
* {@code context}.
87+
* {@code compiler} to compile them. Templates will be read using the given
88+
* {@code templateEncoding}.
89+
* @param templateResourceResolver the resolver to use
90+
* @param templateEncoding the charset to use when reading the templates
91+
* @param compiler the compiler to use
92+
* @since 2.0.5
93+
*/
94+
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding,
95+
Compiler compiler) {
96+
this(templateResourceResolver, templateEncoding, compiler, Collections.<String, Object>emptyMap());
97+
}
98+
99+
/**
100+
* Creates a new {@code MustacheTemplateEngine} that will use the given
101+
* {@code templateResourceResolver} to resolve templates. Templates will be read as
102+
* UTF-8. Once read, the given {@code compiler} will be used to compile them. Compiled
103+
* templates will be created with the given {@code context}.
72104
* @param templateResourceResolver the resolver to use
73105
* @param compiler the compiler to use
74106
* @param context the context to pass to compiled templates
@@ -77,15 +109,36 @@ public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver,
77109
*/
78110
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Compiler compiler,
79111
Map<String, Object> context) {
112+
this(templateResourceResolver, StandardCharsets.UTF_8, compiler, context);
113+
}
114+
115+
/**
116+
* Creates a new {@code MustacheTemplateEngine} that will use the given
117+
* {@code templateResourceResolver} to resolve templates. Template will be read using
118+
* the given {@code templateEncoding}. Once read, the given {@code compiler} will be
119+
* used to compile them. Compiled templates will be created with the given
120+
* {@code context}.
121+
* @param templateResourceResolver the resolver to use
122+
* @param templateEncoding the charset to use when reading the templates
123+
* @param compiler the compiler to use
124+
* @param context the context to pass to compiled templates
125+
* @since 2.0.5
126+
* @see MustacheTemplate#MustacheTemplate(org.springframework.restdocs.mustache.Template,
127+
* Map)
128+
*/
129+
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver, Charset templateEncoding,
130+
Compiler compiler, Map<String, Object> context) {
80131
this.templateResourceResolver = templateResourceResolver;
132+
this.templateEncoding = templateEncoding;
81133
this.compiler = compiler;
82134
this.context = context;
83135
}
84136

85137
@Override
86138
public Template compileTemplate(String name) throws IOException {
87139
Resource templateResource = this.templateResourceResolver.resolveTemplateResource(name);
88-
return new MustacheTemplate(this.compiler.compile(new InputStreamReader(templateResource.getInputStream())),
140+
return new MustacheTemplate(
141+
this.compiler.compile(new InputStreamReader(templateResource.getInputStream(), this.templateEncoding)),
89142
this.context);
90143
}
91144

spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.restdocs.config;
1818

1919
import java.net.URI;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.List;
@@ -73,6 +74,8 @@ public void defaultConfiguration() {
7374
this.configurer.apply(configuration, createContext());
7475
assertThat(configuration).containsKey(TemplateEngine.class.getName());
7576
assertThat(configuration.get(TemplateEngine.class.getName())).isInstanceOf(MustacheTemplateEngine.class);
77+
assertThat(configuration.get(TemplateEngine.class.getName())).hasFieldOrPropertyWithValue("templateEncoding",
78+
StandardCharsets.UTF_8);
7679
assertThat(configuration).containsKey(WriterResolver.class.getName());
7780
assertThat(configuration.get(WriterResolver.class.getName())).isInstanceOf(StandardWriterResolver.class);
7881
assertThat(configuration).containsKey(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS);
@@ -147,12 +150,15 @@ public void additionalDefaultSnippets() {
147150
@Test
148151
public void customSnippetEncoding() {
149152
Map<String, Object> configuration = new HashMap<>();
150-
this.configurer.snippets().withEncoding("ISO 8859-1").apply(configuration, createContext());
153+
this.configurer.snippets().withEncoding("ISO-8859-1");
154+
this.configurer.apply(configuration, createContext());
151155
assertThat(configuration).containsKey(SnippetConfiguration.class.getName());
152156
assertThat(configuration.get(SnippetConfiguration.class.getName())).isInstanceOf(SnippetConfiguration.class);
153157
SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration
154158
.get(SnippetConfiguration.class.getName());
155-
assertThat(snippetConfiguration.getEncoding()).isEqualTo("ISO 8859-1");
159+
assertThat(snippetConfiguration.getEncoding()).isEqualTo(StandardCharsets.ISO_8859_1.displayName());
160+
assertThat(configuration.get(TemplateEngine.class.getName())).hasFieldOrPropertyWithValue("templateEncoding",
161+
StandardCharsets.ISO_8859_1);
156162
}
157163

158164
@Test

0 commit comments

Comments
 (0)