|
| 1 | +package tools.jackson.dataformat.avro.schema; |
| 2 | + |
| 3 | +import org.apache.avro.Schema; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import tools.jackson.dataformat.avro.*; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +// Tests for [dataformats-binary#348]: inner class schema namespace must not contain '$' |
| 11 | +// (actual fix already in via [dataformats-binary#167]) |
| 12 | +public class InnerClassNamespace348Test extends AvroTestBase |
| 13 | +{ |
| 14 | + // Outer class matching the issue's "Test" class |
| 15 | + static class Outer348 { |
| 16 | + // Inner class matching the issue's "TestImpl" |
| 17 | + static class Inner348 { |
| 18 | + public int value; |
| 19 | + public String name; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // Three-level nesting: OuterOuter348 -> Outer348b -> Inner348b |
| 24 | + static class OuterOuter348 { |
| 25 | + static class Outer348b { |
| 26 | + static class Inner348b { |
| 27 | + public int x; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private final AvroMapper MAPPER = newMapper(); |
| 33 | + |
| 34 | + // [dataformats-binary#348]: namespace of inner-class record schema must not contain '$' |
| 35 | + @Test |
| 36 | + public void testInnerClassNamespaceNosDollarSign() throws Exception |
| 37 | + { |
| 38 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 39 | + MAPPER.acceptJsonFormatVisitor(Outer348.Inner348.class, gen); |
| 40 | + Schema schema = gen.getGeneratedSchema().getAvroSchema(); |
| 41 | + |
| 42 | + final String namespace = schema.getNamespace(); |
| 43 | + final String name = schema.getName(); |
| 44 | + |
| 45 | + // Name must be the simple class name |
| 46 | + assertEquals("Inner348", name); |
| 47 | + |
| 48 | + // Namespace must NOT contain '$' |
| 49 | + assertFalse(namespace.contains("$"), |
| 50 | + "Namespace must not contain '$' but was: " + namespace); |
| 51 | + |
| 52 | + // Namespace must end with the outer class name, separated by '.' |
| 53 | + assertTrue(namespace.endsWith(".Outer348"), |
| 54 | + "Namespace must end with '.Outer348' but was: " + namespace); |
| 55 | + } |
| 56 | + |
| 57 | + // [dataformats-binary#348]: deeper nesting must also produce '$'-free namespaces |
| 58 | + @Test |
| 59 | + public void testDeeplyNestedClassNamespaceNoDollarSign() throws Exception |
| 60 | + { |
| 61 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 62 | + MAPPER.acceptJsonFormatVisitor(OuterOuter348.Outer348b.Inner348b.class, gen); |
| 63 | + Schema schema = gen.getGeneratedSchema().getAvroSchema(); |
| 64 | + |
| 65 | + final String namespace = schema.getNamespace(); |
| 66 | + final String name = schema.getName(); |
| 67 | + |
| 68 | + assertEquals("Inner348b", name); |
| 69 | + |
| 70 | + assertFalse(namespace.contains("$"), |
| 71 | + "Namespace must not contain '$' but was: " + namespace); |
| 72 | + |
| 73 | + // Namespace must use '.' separators through all nesting levels |
| 74 | + assertTrue(namespace.endsWith(".OuterOuter348.Outer348b"), |
| 75 | + "Namespace must end with '.OuterOuter348.Outer348b' but was: " + namespace); |
| 76 | + } |
| 77 | + |
| 78 | + // [dataformats-binary#348]: schema with inner class must be usable for roundtrip |
| 79 | + @Test |
| 80 | + public void testInnerClassRoundtrip() throws Exception |
| 81 | + { |
| 82 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 83 | + MAPPER.acceptJsonFormatVisitor(Outer348.Inner348.class, gen); |
| 84 | + AvroSchema schema = gen.getGeneratedSchema(); |
| 85 | + |
| 86 | + // Schema must be usable for write+read without errors |
| 87 | + Outer348.Inner348 input = new Outer348.Inner348(); |
| 88 | + input.value = 42; |
| 89 | + input.name = "hello"; |
| 90 | + |
| 91 | + byte[] encoded = MAPPER.writer(schema).writeValueAsBytes(input); |
| 92 | + Outer348.Inner348 decoded = MAPPER.readerFor(Outer348.Inner348.class) |
| 93 | + .with(schema) |
| 94 | + .readValue(encoded); |
| 95 | + |
| 96 | + assertEquals(input.value, decoded.value); |
| 97 | + assertEquals(input.name, decoded.name); |
| 98 | + } |
| 99 | +} |
0 commit comments