From ed5921fe831c12efcb2db410f382bc887a196531 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 14 Sep 2020 09:11:28 +0100 Subject: [PATCH] Ensure that UTF-8 is used to read and write snippets in operation macro Fixes gh-696 --- .../src/main/resources/extensions/operation_block_macro.rb | 3 ++- .../restdocs/asciidoctor/AbstractOperationBlockMacroTests.java | 3 ++- .../src/test/resources/operations/all-snippets.html | 2 +- .../test/resources/operations/custom-snippet-custom-title.html | 2 +- .../resources/operations/custom-snippet-default-title.html | 2 +- .../src/test/resources/some-operation/custom-snippet.adoc | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb index 94f118018..3faa24584 100644 --- a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb +++ b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb @@ -37,6 +37,7 @@ def read_snippets(snippets_dir, snippet_names, parent, operation, def do_read_snippets(snippets, parent, operation, snippet_titles) content = StringIO.new + content.set_encoding "UTF-8" section_id = parent.id snippets.each do |snippet| append_snippet_block(content, snippet, section_id, @@ -93,7 +94,7 @@ def append_snippet_block(content, snippet, section_id, def write_content(content, snippet, operation) if File.file? snippet.path - content.puts File.readlines(snippet.path).join + content.puts File.readlines(snippet.path, :encoding => 'UTF-8').join else warn "Snippet #{snippet.name} not found at #{snippet.path} for"\ " operation #{operation}" diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java index c40f9344a..cdf57adf6 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -199,7 +200,7 @@ public void titleOfCustomSnippetCanBeCustomizedUsingDocumentAttribute() throws E private String getExpectedContentFromFile(String fileName) throws URISyntaxException, IOException { Path filePath = Paths.get(this.getClass().getResource("/operations/" + fileName + ".html").toURI()); - String content = new String(Files.readAllBytes(filePath)); + String content = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8); if (isWindows()) { return content.replace("\r\n", "\n"); } diff --git a/spring-restdocs-asciidoctor/src/test/resources/operations/all-snippets.html b/spring-restdocs-asciidoctor/src/test/resources/operations/all-snippets.html index 491f9dedf..508e45984 100644 --- a/spring-restdocs-asciidoctor/src/test/resources/operations/all-snippets.html +++ b/spring-restdocs-asciidoctor/src/test/resources/operations/all-snippets.html @@ -13,7 +13,7 @@

Custom snippet

-
mycustomsnippet
+
mycustomsnippet-äöü
diff --git a/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-custom-title.html b/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-custom-title.html index a8c27d4cb..0fed5ed2a 100644 --- a/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-custom-title.html +++ b/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-custom-title.html @@ -3,7 +3,7 @@

Customized title

-
mycustomsnippet
+
mycustomsnippet-äöü
diff --git a/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-default-title.html b/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-default-title.html index 880800a4d..f5613259f 100644 --- a/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-default-title.html +++ b/spring-restdocs-asciidoctor/src/test/resources/operations/custom-snippet-default-title.html @@ -3,7 +3,7 @@

Custom snippet

-
mycustomsnippet
+
mycustomsnippet-äöü
diff --git a/spring-restdocs-asciidoctor/src/test/resources/some-operation/custom-snippet.adoc b/spring-restdocs-asciidoctor/src/test/resources/some-operation/custom-snippet.adoc index 4a27b9bae..7933144b1 100644 --- a/spring-restdocs-asciidoctor/src/test/resources/some-operation/custom-snippet.adoc +++ b/spring-restdocs-asciidoctor/src/test/resources/some-operation/custom-snippet.adoc @@ -1,4 +1,4 @@ [source,http,options="nowrap"] ---- -mycustomsnippet +mycustomsnippet-äöü ---- \ No newline at end of file