Skip to content

Commit 78281fd

Browse files
[improve][pip] PIP-464: Deprecate legacy Jackson JsonSchema format for SchemaType.JSON (#25361)
1 parent 7e5839c commit 78281fd

1 file changed

Lines changed: 213 additions & 0 deletions

File tree

pip/pip-464.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# PIP-464: Deprecate legacy Jackson JsonSchema format for SchemaType.JSON
2+
3+
## Background knowledge
4+
5+
In Pulsar, `SchemaType.JSON` is used for topics where producers and consumers exchange JSON-encoded messages with a defined schema. The schema definition is stored in `SchemaInfo.schema` (the `schema_data` field) and is used by the broker for validation and compatibility checking, and by consumers for deserialization.
6+
7+
There are two relevant schema definition formats:
8+
9+
- **Apache Avro schema format**: `{"type":"record","name":"MyRecord","fields":[{"name":"field1","type":"string"}]}` — the standard format since Pulsar 2.1. This is what consumers (`AvroBaseStructSchema`, `GenericJsonSchema`, `AutoConsumeSchema`) require to function correctly.
10+
11+
- **Jackson JSON Schema Draft format**: `{"type":"object","properties":{"field1":{"type":"string"}}}` — the legacy format from Pulsar 2.0, generated by Jackson's `JsonSchemaGenerator`. This was superseded in Pulsar 2.1 ([commit 1893323bc2](https://github.com/apache/pulsar/commit/1893323bc2), [PR #2071](https://github.com/apache/pulsar/pull/2071)) when the project standardized on Avro format for all structured schemas.
12+
13+
To maintain backward compatibility with schemas created during the 2.0 era, fallback logic was added in several components:
14+
15+
- **`StructSchemaDataValidator`** — the broker-side validator that checks whether a schema definition is structurally valid. It first attempts to parse the schema as Avro; if that fails, it falls back to Jackson `JsonSchema` parsing.
16+
- **`JsonSchemaCompatibilityCheck`** — the broker-side compatibility checker. It has permissive handling for mixed-format scenarios (Avro↔Jackson, Jackson↔Jackson).
17+
- **`ProducerImpl`** — the Java client's producer implementation. It detects the broker's protocol version and sends the old Jackson format to brokers below protocol version 13.
18+
19+
## Motivation
20+
21+
The broker-side fallback logic is too lenient. When Avro parsing fails, the Jackson fallback accepts **any valid JSON** as a schema definition for `SchemaType.JSON`, not just the legacy Jackson format. This has caused real issues for non-Java clients (e.g., the Rust client) where users accidentally register a JSON Schema Draft 2020-12 definition:
22+
23+
1. The broker's `StructSchemaDataValidator` accepts it — Avro parse fails, Jackson fallback succeeds because it accepts any JSON.
24+
2. The broker's compatibility check allows it — empty block for the Avro→JsonSchema or JsonSchema→JsonSchema path.
25+
3. But when a Java consumer uses `AutoConsumeSchema` or `GenericJsonSchema`, it fails with `SchemaParseException: Type not supported: object` because `AvroBaseStructSchema` strictly requires Avro format — no fallback on the consumer side.
26+
27+
The result is that the broker stores a schema that no Java consumer can read. The failure is deferred from producer registration time (where it should be caught) to consumer read time (where it is confusing and unrecoverable without schema deletion).
28+
29+
There is an asymmetry in the system today: the broker side is lenient (accepts any JSON), but the consumer side is strict (requires Avro). This PIP resolves the asymmetry by making the broker side equally strict.
30+
31+
## Goals
32+
33+
### In Scope
34+
35+
- Add a broker configuration to control whether the legacy Jackson JSON Schema format is accepted for `SchemaType.JSON`.
36+
- Default to strict Avro-only validation, consistent with what the consumer side already requires.
37+
- Tighten `StructSchemaDataValidator` and `JsonSchemaCompatibilityCheck` to reject non-Avro schemas when the legacy flag is disabled.
38+
- Deprecate (but not remove) the `ProducerImpl` client-side code that sends old Jackson format to brokers below protocol version 13.
39+
- Document that `schema_data` for `SchemaType.JSON` must be an Apache Avro schema definition.
40+
41+
### Out of Scope
42+
43+
- Removing the `ProducerImpl` client-side backward-compatibility code — this will be addressed in a future major release.
44+
- Migration tooling to detect or convert legacy schemas in existing deployments.
45+
- Changes to schema handling for other `SchemaType` values (AVRO, PROTOBUF, etc.).
46+
47+
## High Level Design
48+
49+
A new broker configuration `schemaJsonAllowLegacyJacksonFormat` (default `false`) controls whether the old Jackson JSON Schema format is accepted for `SchemaType.JSON` schema definitions.
50+
51+
When disabled (default):
52+
- `StructSchemaDataValidator` requires valid Avro schema format. If Avro parsing fails, the schema is rejected immediately with the Avro `SchemaParseException` — no Jackson fallback.
53+
- `JsonSchemaCompatibilityCheck` requires both the existing and new schema to be valid Avro format. Mixed-format compatibility is rejected.
54+
55+
When enabled:
56+
- The current backward-compatible behavior is preserved exactly as it is today. No logging or metrics overhead.
57+
58+
On the client side, the existing `ProducerImpl` code that sends old Jackson format to pre-v13 brokers is annotated as `@Deprecated` with a reference to this PIP.
59+
60+
## Detailed Design
61+
62+
### Design & Implementation Details
63+
64+
#### 1. `StructSchemaDataValidator`
65+
66+
**File:** `pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/validator/StructSchemaDataValidator.java`
67+
68+
Current behavior:
69+
```
70+
try {
71+
parse Avro schema
72+
} catch (SchemaParseException) {
73+
try {
74+
parse Jackson JsonSchema // accepts ANY valid JSON
75+
} catch (...) {
76+
throw invalid schema
77+
}
78+
}
79+
```
80+
81+
Proposed behavior:
82+
```
83+
try {
84+
parse Avro schema
85+
} catch (SchemaParseException e) {
86+
if (schemaJsonAllowLegacyJacksonFormat) {
87+
try {
88+
parse Jackson JsonSchema
89+
} catch (...) {
90+
throw invalid schema
91+
}
92+
} else {
93+
throw invalid schema (propagate original Avro SchemaParseException)
94+
}
95+
}
96+
```
97+
98+
When `schemaJsonAllowLegacyJacksonFormat=false` (default), the Avro `SchemaParseException` is propagated directly. This reuses the existing error message without modification.
99+
100+
#### 2. `JsonSchemaCompatibilityCheck`
101+
102+
**File:** `pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/JsonSchemaCompatibilityCheck.java`
103+
104+
Current behavior: The compatibility check has empty/permissive handling for mixed-format scenarios (Avro↔Jackson, Jackson↔Jackson).
105+
106+
Proposed behavior: When `schemaJsonAllowLegacyJacksonFormat=false`, all schema definitions passed to compatibility checking must be valid Avro format. If either the existing or new schema fails Avro parsing, the compatibility check returns incompatible. This is consistent since `StructSchemaDataValidator` will have already rejected non-Avro schemas at registration time, so this serves as a defense-in-depth check.
107+
108+
#### 3. `ProducerImpl` Client-Side Code (Deprecation Only)
109+
110+
**File:** `pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java`
111+
112+
The existing code that detects broker protocol version and falls back to sending the old Jackson format is annotated with `@Deprecated` and a code comment referencing this PIP. No behavioral change to the client in this PIP — removal is deferred to a future major release.
113+
114+
#### 4. Configuration Plumbing
115+
116+
The `schemaJsonAllowLegacyJacksonFormat` config value must be accessible from both `StructSchemaDataValidator` and `JsonSchemaCompatibilityCheck`. This will be threaded through the existing `SchemaRegistryService` → validator/checker dependency chain, consistent with how other schema-related broker configs are propagated.
117+
118+
### Public-facing Changes
119+
120+
#### Public API
121+
122+
No changes to the public client API, admin API, or REST API.
123+
124+
#### Binary protocol
125+
126+
No changes to the Pulsar binary protocol.
127+
128+
#### Configuration
129+
130+
Add a new broker configuration parameter:
131+
132+
| Property | Type | Default | Description |
133+
|---|---|---|---|
134+
| `schemaJsonAllowLegacyJacksonFormat` | boolean | `false` | Whether to allow legacy Jackson JsonSchema format for `SchemaType.JSON` schema definitions. When `false`, only valid Apache Avro schema format is accepted, consistent with what the consumer side requires. When `true`, the pre-2.1 backward-compatible behavior is preserved for deployments that still have topics with legacy-format schemas. |
135+
136+
```java
137+
@FieldContext(
138+
category = CATEGORY_SCHEMA,
139+
doc = "Whether to allow legacy Jackson JsonSchema format for SchemaType.JSON schema definitions. "
140+
+ "When false (default), only valid Apache Avro schema format is accepted, consistent with "
141+
+ "what the consumer side requires. When true, the pre-2.1 backward-compatible behavior is "
142+
+ "preserved for deployments that still have topics with legacy-format schemas."
143+
)
144+
private boolean schemaJsonAllowLegacyJacksonFormat = false;
145+
```
146+
147+
#### CLI
148+
149+
No CLI changes.
150+
151+
#### Metrics
152+
153+
No new metrics.
154+
155+
## Monitoring
156+
157+
No specific monitoring changes. When `schemaJsonAllowLegacyJacksonFormat=false` (default), producers attempting to register non-Avro schema definitions will receive an error response at registration time. Operators can monitor for increased schema registration failures after upgrading if they suspect legacy schemas may exist in their deployment.
158+
159+
## Security Considerations
160+
161+
No security implications. This change tightens input validation, which marginally improves the broker's input handling by rejecting malformed schema definitions earlier.
162+
163+
## Backward & Forward Compatibility
164+
165+
### Upgrade
166+
167+
This is a **breaking change** in default behavior.
168+
169+
- **Schemas registered before Pulsar 2.1 (2018)** that still use the Jackson JSON Schema Draft format will be rejected when new schema versions are registered against the same topic. Existing stored schemas are not modified or deleted.
170+
- **Java producers** are unaffected. `JSONSchema.of()` has generated Avro format since Pulsar 2.1.
171+
- **Non-Java producers** that were incorrectly registering JSON Schema Draft definitions will now receive a clear failure at registration time instead of a deferred failure at consumer read time.
172+
- **Users with genuine legacy schemas** can set `schemaJsonAllowLegacyJacksonFormat=true` in `broker.conf` to restore the previous behavior.
173+
174+
The legacy Jackson format has been superseded since Pulsar 2.1, released in 2018. Any active topics with old-format schemas have likely been migrated or recreated over the past 7+ years. The Java client has not generated Jackson format schemas since 2.1.
175+
176+
### Downgrade / Rollback
177+
178+
Rolling back to a prior Pulsar version will restore the lenient fallback behavior. No data migration is needed — the configuration flag is purely a runtime behavioral switch.
179+
180+
### Pulsar Geo-Replication Upgrade & Downgrade/Rollback Considerations
181+
182+
No impact on geo-replication. Schema definitions are replicated as-is between clusters. If one cluster has `schemaJsonAllowLegacyJacksonFormat=false` and receives a replicated topic with a legacy-format schema, the schema was already stored — this PIP only affects new schema registrations, not existing stored schemas. However, operators should ensure consistent configuration across geo-replicated clusters to avoid asymmetric behavior where a schema is accepted on one cluster but rejected on another.
183+
184+
## Alternatives
185+
186+
### Alternative 1: Default to `true` (backward-compatible default)
187+
188+
Rejected. The legacy Jackson format has been superseded since Pulsar 2.1 (2018). The Java client's `JSONSchema.of()` has generated Avro format for over 7 years. Defaulting to `true` perpetuates a silent failure mode where non-Java clients can register schemas that Java consumers cannot read. The primary value of this PIP is fixing the default behavior.
189+
190+
### Alternative 2: Descriptive error messages with format detection
191+
192+
Considered adding logic to detect JSON Schema Draft format (e.g., presence of `"$schema"` or `"type":"object"` with `"properties"`) and return a targeted error message. Rejected in favor of propagating the existing Avro `SchemaParseException` to minimize code change surface. The PIP documentation and Pulsar schema documentation updates will serve as the guide for non-Java client developers.
193+
194+
### Alternative 3: Two-release deprecation period
195+
196+
Considered defaulting to `true` in the first release and flipping to `false` in the next. Rejected because the legacy format is 7+ years old, the Java client has not generated it since 2.1, and any active topics with old-format schemas have likely been migrated or recreated. An immediate default flip is appropriate.
197+
198+
### Alternative 4: Warn logging or metrics when legacy format is accepted
199+
200+
Considered adding WARN-level logging or a counter metric when `schemaJsonAllowLegacyJacksonFormat=true` and a legacy schema is encountered. Rejected to keep the opt-in path simple and silent — users who enable the flag are making a conscious choice.
201+
202+
### Alternative 5: Migration tooling
203+
204+
Considered providing an admin CLI command to scan stored schemas and report which topics use the old format. Rejected as out of scope — this can be added later if demand materializes. Operators can identify legacy schemas by attempting to parse stored schema definitions with an Avro parser.
205+
206+
## General Notes
207+
208+
The documentation for `SchemaType.JSON` should be updated to clearly state that `schema_data` must be an Apache Avro schema definition, not a JSON Schema Draft definition. This is particularly important for non-Java client implementations (Rust, Go, Python, C++, Node.js, .NET) that construct schema definitions manually rather than using the Java client's `JSONSchema.of()` helper.
209+
210+
## Links
211+
212+
* Mailing List discussion thread:
213+
* Mailing List voting thread:

0 commit comments

Comments
 (0)