diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java index b2ab8316..67147fc9 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java @@ -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 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.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 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 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); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java index c8ade927..f5f0e708 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java @@ -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 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