From 6e2ca4d7c8f5ab6179886fc2f34a946e04139bcf Mon Sep 17 00:00:00 2001 From: Sergio Segura Date: Thu, 29 Oct 2026 18:53:56 +0100 Subject: [PATCH] Write the reports whole when a quoted value is cut inside an emoji A change to an accepted request quotes the value it sent, cut at 40 characters. The cut counted UTF-16 units, so it could fall between the two halves of an emoji and leave half a character in the description. WholeFile then wrote report.json with a strict UTF-8 encoder, which refused it: both report.json and report.html were lost, and the run ended with 4. The quote is now cut at 40 code points, as every other cut in the code base is, and WholeFile writes with an encoder that puts a question mark where half a character stands, whatever brought it there. Co-Authored-By: Claude Opus 5.5 --- .../main/java/io/restest/gen/Mutations.java | 10 +++++-- .../java/io/restest/gen/MutationsTest.java | 26 +++++++++++++++++++ .../java/io/restest/report/WholeFile.java | 14 +++++++++- .../io/restest/report/HtmlReportTest.java | 21 +++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/restest-gen/src/main/java/io/restest/gen/Mutations.java b/restest-gen/src/main/java/io/restest/gen/Mutations.java index 314fc89cf..f82a500b6 100644 --- a/restest-gen/src/main/java/io/restest/gen/Mutations.java +++ b/restest-gen/src/main/java/io/restest/gen/Mutations.java @@ -1291,10 +1291,16 @@ private static String items(int howMany) { return howMany + (howMany == 1 ? " item" : " items"); } + /** + * The value as JSON, cut after so many characters. A character outside the most common ones, + * an emoji say, is two halves in a Java string, and cutting between them would leave half a + * character that no file can hold: the cut counts whole characters instead. + */ private static String quoted(JsonValue value) { String written = JsonText.write(value); - return written.length() <= QUOTED_AT_MOST - ? written : written.substring(0, QUOTED_AT_MOST) + "..."; + return written.codePointCount(0, written.length()) <= QUOTED_AT_MOST + ? written + : written.substring(0, written.offsetByCodePoints(0, QUOTED_AT_MOST)) + "..."; } /** diff --git a/restest-gen/src/test/java/io/restest/gen/MutationsTest.java b/restest-gen/src/test/java/io/restest/gen/MutationsTest.java index 532e57028..61302a444 100644 --- a/restest-gen/src/test/java/io/restest/gen/MutationsTest.java +++ b/restest-gen/src/test/java/io/restest/gen/MutationsTest.java @@ -603,6 +603,32 @@ void against_the_pattern() { .contains("which its pattern ^[a-z]+$ refuses"); }); } + + @Test + @DisplayName("a long word is quoted cut between two characters, never inside an emoji") + void quoted_whole_characters() { + // Written as JSON, the quote and thirty-eight letters come first, so the fortieth + // character, where the quote stops, is the emoji - two halves in a Java string. + String word = "x".repeat(38) + "😀"; + Operation find = Operation.of(HttpMethod.GET, "/moods", List.of( + Parameter.of("mood", ParameterLocation.QUERY, true, + new StringSchema(SchemaMetadata.none().withEnumeration( + List.of(JsonValue.of(word))), Optional.empty(), + Optional.empty(), Optional.empty(), Optional.empty())))) + .withId(OperationId.of("findMoods")); + + List changed = changesIn(find, accepted(find, + value("mood", ParameterLocation.QUERY, JsonValue.of(word))), + "breakAnEnumeration"); + + assertThat(changed).extracting(each -> each.mutation().orElseThrow().description()) + .isNotEmpty() + .allSatisfy(description -> assertThat(description.codePoints()) + .describedAs("half a character, which no file can hold") + .noneMatch(point -> Character.getType(point) == Character.SURROGATE)) + .anySatisfy(description -> assertThat(description) + .contains("\"" + "X".repeat(38) + "😀...")); + } } @Nested diff --git a/restest-report/src/main/java/io/restest/report/WholeFile.java b/restest-report/src/main/java/io/restest/report/WholeFile.java index 2f5fdbec7..0bbc32ce9 100644 --- a/restest-report/src/main/java/io/restest/report/WholeFile.java +++ b/restest-report/src/main/java/io/restest/report/WholeFile.java @@ -15,9 +15,14 @@ */ package io.restest.report; +import java.io.BufferedWriter; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.UncheckedIOException; import java.io.Writer; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.StandardCharsets; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.Path; @@ -61,7 +66,14 @@ static Path whileBeingWritten(Path file) { static void write(Path file, String what, Content content) { Path partial = whileBeingWritten(file); try { - try (Writer out = Files.newBufferedWriter(partial)) { + // Half a character - the first or last half of an emoji, say, from text cut in the + // wrong place - cannot be written in UTF-8. The writer Files hands out refuses it and + // takes the whole report with it; this one writes a question mark in its place. + CharsetEncoder utf8 = StandardCharsets.UTF_8.newEncoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); + try (Writer out = new BufferedWriter( + new OutputStreamWriter(Files.newOutputStream(partial), utf8))) { content.writeTo(out); } try { diff --git a/restest-report/src/test/java/io/restest/report/HtmlReportTest.java b/restest-report/src/test/java/io/restest/report/HtmlReportTest.java index 2131f0c5b..fc5d47b12 100644 --- a/restest-report/src/test/java/io/restest/report/HtmlReportTest.java +++ b/restest-report/src/test/java/io/restest/report/HtmlReportTest.java @@ -22,6 +22,8 @@ import io.restest.core.json.JsonText; import io.restest.core.json.JsonValue; import io.restest.core.model.SpecificationIssue; +import io.restest.core.oracle.Finding; +import io.restest.core.oracle.WfcFault; import io.restest.core.settings.Settings; import io.restest.core.settings.SettingsInEffect; import java.io.IOException; @@ -103,6 +105,25 @@ void a_hostile_reply_cannot_break_out(@TempDir Path directory) throws IOExceptio .contains("alert(1)"); } + @Test + @DisplayName("half a character in the run, which UTF-8 cannot hold, is written as a question " + + "mark, and both files are still written") + void half_a_character_is_written_as_a_question_mark(@TempDir Path directory) + throws IOException { + Path json = directory.resolve("report.json"); + Path page = directory.resolve("report.html"); + HtmlReport report = HtmlReport.around(JsonReport.to(json), page); + report.on(Runs.started(WHEN)); + // The first half of an emoji, and nothing after it: what cutting a quote in the wrong + // place leaves. + report.on(new RunEvent.FaultFound(WHEN, Finding.of(WfcFault.HTTP_STATUS_500, + Runs.attempt("GET /pets", "/pets", 500), "sent x\uD83D... for q"))); + report.on(new RunEvent.RunFinished(WHEN, Duration.ofSeconds(1), Runs.engine())); + + assertThat(Files.readString(json)).contains("sent x?... for q"); + assertThat(theRunIn(Files.readString(page))).contains("sent x?... for q"); + } + @Test @DisplayName("what changes in the run's text is only what has to, written so that JSON reads " + "it back the same")