Skip to content

Commit 55a9fdb

Browse files
mattisonchaoclaude
authored andcommitted
[fix][broker] Use compatible Avro name validator in JsonSchemaCompatibilityCheck (#25255)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5454031 commit 55a9fdb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/JsonSchemaCompatibilityCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import org.apache.avro.Schema;
2626
import org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException;
27+
import org.apache.pulsar.broker.service.schema.validator.StructSchemaDataValidator;
2728
import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy;
2829
import org.apache.pulsar.common.protocol.schema.SchemaData;
2930
import org.apache.pulsar.common.schema.SchemaType;
@@ -91,7 +92,7 @@ private void isCompatibleJsonSchema(SchemaData from, SchemaData to) throws Incom
9192
private boolean isAvroSchema(SchemaData schemaData) {
9293
try {
9394

94-
Schema.Parser fromParser = new Schema.Parser();
95+
Schema.Parser fromParser = new Schema.Parser(StructSchemaDataValidator.COMPATIBLE_NAME_VALIDATOR);
9596
fromParser.setValidateDefaults(false);
9697
Schema fromSchema = fromParser.parse(new String(schemaData.getData(), UTF_8));
9798
return true;

pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/JsonSchemaCompatibilityCheckTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.pulsar.broker.service.schema;
2020

21+
import static java.nio.charset.StandardCharsets.UTF_8;
2122
import com.fasterxml.jackson.core.JsonProcessingException;
2223
import com.fasterxml.jackson.databind.ObjectMapper;
2324
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
@@ -60,6 +61,33 @@ public void testJsonSchemaBackwardsCompatibility() throws JsonProcessingExceptio
6061
Assert.assertTrue(jsonSchemaCompatibilityCheck.isCompatible(from, to, SchemaCompatibilityStrategy.FULL));
6162
}
6263

64+
@Test
65+
public void testSchemaWithDollarSignInRecordNameRejectsIncompatibleChange() {
66+
// Schema v1: has field1 (string)
67+
String schemaV1 =
68+
"{\"type\":\"record\",\"name\":\"Outer$Inner\",\"namespace\":\"org.example\","
69+
+ "\"fields\":[{\"name\":\"field1\",\"type\":\"string\"}]}";
70+
// Schema v2: removed field1, added field2 without default — NOT backward compatible
71+
String schemaV2 =
72+
"{\"type\":\"record\",\"name\":\"Outer$Inner\",\"namespace\":\"org.example\","
73+
+ "\"fields\":[{\"name\":\"field2\",\"type\":\"string\"}]}";
74+
SchemaData from = SchemaData.builder()
75+
.data(schemaV1.getBytes(UTF_8))
76+
.type(SchemaType.JSON)
77+
.build();
78+
SchemaData to = SchemaData.builder()
79+
.data(schemaV2.getBytes(UTF_8))
80+
.type(SchemaType.JSON)
81+
.build();
82+
JsonSchemaCompatibilityCheck check = new JsonSchemaCompatibilityCheck();
83+
// Without the fix, isAvroSchema() rejects '$' and the compatibility check is
84+
// skipped entirely (falls through to "corrupted, allow overwrite"), so this
85+
// would incorrectly return true.
86+
// With the fix, isAvroSchema() recognizes these as valid Avro schemas and the
87+
// Avro compatibility check correctly detects the incompatibility.
88+
Assert.assertFalse(check.isCompatible(from, to, SchemaCompatibilityStrategy.BACKWARD));
89+
}
90+
6391
@Data
6492
private static class Foo {
6593
private String field1;

0 commit comments

Comments
 (0)