From fb5b448a585cf2982fa94c2224af798137b4106d Mon Sep 17 00:00:00 2001 From: Simon Gene Gottlieb Date: Mon, 10 Aug 2026 14:00:40 +0200 Subject: [PATCH] fix: multiple documents starting with a tag When emitting multiple documents and the documents start with a tag, this caused the emitter state to go into an invalid state, stopping to emit any data. Even though a tag was emitter, the next value (e.g.: a scalar) trigger another start of the document. This is fixed by tightening the constraints when to emit the start of the document. --- src/emitter.cpp | 4 +++- test/integration/emitter_test.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/emitter.cpp b/src/emitter.cpp index 9c7316c79..e57ee4684 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -309,8 +309,10 @@ void Emitter::PrepareTopNode(EmitterNodeType::value child) { if (child == EmitterNodeType::NoType) return; - if (m_pState->CurGroupChildCount() > 0 && m_stream.col() > 0) + if (m_pState->CurGroupChildCount() > 0 && m_stream.col() > 0 + && !m_pState->HasBegunContent()) { EmitBeginDoc(); + } switch (child) { case EmitterNodeType::NoType: diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 928f19237..ddf0a4068 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -2059,5 +2059,32 @@ TEST_F(EmitterTest, EmitSetLocalTagInNameHandle) { ExpectEmit("num: !a!foo 42"); } +TEST_F(EmitterTest, EmitMultiDocsWithTags) { + out << YAML::BeginDoc + << YAML::LocalTag("The_Tag") + << YAML::BeginSeq + << "Some Value" + << YAML::EndSeq + << YAML::EndDoc; + + out << YAML::BeginDoc + << YAML::LocalTag("The_Tag") + << YAML::BeginSeq + << "Some Value" + << YAML::EndSeq + << YAML::EndDoc; + + ExpectEmit( + "---\n" + "!The_Tag\n" + "- Some Value\n" + "...\n" + "---\n" + "!The_Tag\n" + "- Some Value\n" + "...\n"); +} + + } // namespace } // namespace YAML