diff --git a/src/com/amazon/ion/impl/lite/IonValueLite.java b/src/com/amazon/ion/impl/lite/IonValueLite.java index f3ef633e56..633fd70ff6 100644 --- a/src/com/amazon/ion/impl/lite/IonValueLite.java +++ b/src/com/amazon/ion/impl/lite/IonValueLite.java @@ -1168,9 +1168,7 @@ private static void writeFieldNameAndAnnotations(IonWriter writer, IonValueLite } } - if (value._annotations != null) { - writer.setTypeAnnotationSymbols(value._annotations); - } + writer.setTypeAnnotationSymbols(value._annotations); } /* The following template may be used to iterate over the tree at the current value. Copying this structure diff --git a/test/com/amazon/ion/impl/IonWriterTestCase.java b/test/com/amazon/ion/impl/IonWriterTestCase.java index 158e7b067b..fdfd52d15b 100644 --- a/test/com/amazon/ion/impl/IonWriterTestCase.java +++ b/test/com/amazon/ion/impl/IonWriterTestCase.java @@ -22,6 +22,7 @@ import static com.amazon.ion.impl.Symtabs.FRED_MAX_IDS; import static com.amazon.ion.impl._Private_IonWriterBase.ERROR_MISSING_FIELD_NAME; import static com.amazon.ion.impl._Private_Utils.newSymbolToken; +import static com.amazon.ion.junit.IonAssert.assertAnnotations; import static com.amazon.ion.junit.IonAssert.assertIonEquals; import static com.amazon.ion.junit.IonAssert.expectNextField; @@ -1288,4 +1289,23 @@ public void testWriteToWithFieldName() assertNull(reader.next()); } + + // Create a writer, set it using the provided annotations, then write the given value using IonValue.writeTo. + // Assert that the resulting value is annotated with only the annotations on the original IonValue, not the ones + // set on the writer. + private void writeToWithAnnotations(IonValue value, String... annotations) throws Exception { + iw = makeWriter(); + iw.setTypeAnnotations(annotations); + value.writeTo(iw); + IonValue reread = system().singleValue(outputByteArray()); + assertAnnotations(reread, value.getTypeAnnotations()); + } + + @Test + public void writeToOverwritesExistingAnnotations() throws Exception { + writeToWithAnnotations(system().singleValue("bar")); + writeToWithAnnotations(system().singleValue("foo::bar")); + writeToWithAnnotations(system().singleValue("foo"), "bar"); + writeToWithAnnotations(system().singleValue("foo::bar"), "baz"); + } }