Skip to content

Commit 2362518

Browse files
authored
DGS-19689 Add support for schema guids that are stable across contexts (#3551)
1 parent 80fa365 commit 2362518

File tree

17 files changed

+515
-33
lines changed

17 files changed

+515
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ target/
2323
# Log file from schema registry daemon
2424
logs/
2525
nohup.out
26+
27+
# jenv version
28+
.java-version

client/src/main/java/io/confluent/kafka/schemaregistry/client/SchemaMetadata.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
2626
import io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference;
27+
import java.util.Objects;
2728

2829
public class SchemaMetadata {
2930

3031
private String subject;
3132
private int id;
3233
private int version;
34+
private String guid;
3335
private String schemaType;
3436
private String schema;
3537
private List<SchemaReference> references;
@@ -59,7 +61,9 @@ public SchemaMetadata(int id,
5961
public SchemaMetadata(Schema schema) {
6062
this.subject = schema.getSubject();
6163
this.id = schema.getId();
64+
this.guid = schema.getGuid();
6265
this.version = schema.getVersion();
66+
this.guid = schema.getGuid();
6367
this.schemaType = schema.getSchemaType();
6468
this.schema = schema.getSchema();
6569
this.references = schema.getReferences();
@@ -79,6 +83,10 @@ public int getVersion() {
7983
return version;
8084
}
8185

86+
public String getGuid() {
87+
return guid;
88+
}
89+
8290
public String getSchemaType() {
8391
return schemaType;
8492
}
@@ -98,4 +106,27 @@ public Metadata getMetadata() {
98106
public RuleSet getRuleSet() {
99107
return this.ruleSet;
100108
}
109+
110+
@Override
111+
public boolean equals(Object o) {
112+
if (o == null || getClass() != o.getClass()) {
113+
return false;
114+
}
115+
SchemaMetadata that = (SchemaMetadata) o;
116+
return Objects.equals(subject, that.subject)
117+
&& id == that.id
118+
&& version == that.version
119+
&& Objects.equals(guid, that.guid)
120+
&& Objects.equals(schemaType, that.schemaType)
121+
&& Objects.equals(schema, that.schema)
122+
&& Objects.equals(references, that.references)
123+
&& Objects.equals(metadata, that.metadata)
124+
&& Objects.equals(ruleSet, that.ruleSet);
125+
}
126+
127+
@Override
128+
public int hashCode() {
129+
return Objects.hash(
130+
subject, id, version, guid, schemaType, schema, references, metadata, ruleSet);
131+
}
101132
}

client/src/main/java/io/confluent/kafka/schemaregistry/client/rest/RestService.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.io.CharStreams;
2626
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig;
2727
import io.confluent.kafka.schemaregistry.client.rest.entities.Config;
28+
import io.confluent.kafka.schemaregistry.client.rest.entities.ContextId;
2829
import io.confluent.kafka.schemaregistry.client.rest.entities.ErrorMessage;
2930
import io.confluent.kafka.schemaregistry.client.rest.entities.Schema;
3031
import io.confluent.kafka.schemaregistry.client.rest.entities.SchemaRegistryServerVersion;
@@ -129,6 +130,9 @@ public class RestService implements Closeable, Configurable {
129130
private static final TypeReference<List<SubjectVersion>> GET_VERSIONS_RESPONSE_TYPE =
130131
new TypeReference<List<SubjectVersion>>() {
131132
};
133+
private static final TypeReference<List<ContextId>> GET_IDS_RESPONSE_TYPE =
134+
new TypeReference<List<ContextId>>() {
135+
};
132136
private static final TypeReference<CompatibilityCheckResponse>
133137
COMPATIBILITY_CHECK_RESPONSE_TYPE_REFERENCE =
134138
new TypeReference<CompatibilityCheckResponse>() {
@@ -1410,6 +1414,39 @@ public List<SubjectVersion> getAllVersionsById(Map<String, String> requestProper
14101414
return response;
14111415
}
14121416

1417+
public SchemaString getByGuid(String guid, String format)
1418+
throws IOException, RestClientException {
1419+
return getByGuid(DEFAULT_REQUEST_PROPERTIES, guid, format);
1420+
}
1421+
1422+
public SchemaString getByGuid(Map<String, String> requestProperties, String guid, String format)
1423+
throws IOException, RestClientException {
1424+
UriBuilder builder = UriBuilder.fromPath("/schemas/guids/{guid}");
1425+
if (format != null) {
1426+
builder.queryParam("format", format);
1427+
}
1428+
String path = builder.build(guid).toString();
1429+
1430+
SchemaString response = httpRequest(path, "GET", null, requestProperties,
1431+
GET_SCHEMA_BY_ID_RESPONSE_TYPE);
1432+
return response;
1433+
}
1434+
1435+
public List<ContextId> getAllContextIds(String guid) throws IOException, RestClientException {
1436+
return getAllContextIds(DEFAULT_REQUEST_PROPERTIES, guid);
1437+
}
1438+
1439+
public List<ContextId> getAllContextIds(Map<String, String> requestProperties, String guid)
1440+
throws IOException, RestClientException {
1441+
UriBuilder builder = UriBuilder.fromPath("/schemas/guids/{guid}/ids");
1442+
String path = builder.build(guid).toString();
1443+
1444+
List<ContextId> response = httpRequest(path, "GET", null, requestProperties,
1445+
GET_IDS_RESPONSE_TYPE);
1446+
1447+
return response;
1448+
}
1449+
14131450
public Integer deleteSchemaVersion(
14141451
Map<String, String> requestProperties,
14151452
String subject,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2025 Confluent Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.confluent.kafka.schemaregistry.client.rest.entities;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21+
import com.fasterxml.jackson.annotation.JsonInclude;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import java.util.Objects;
24+
25+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
26+
@JsonIgnoreProperties(ignoreUnknown = true)
27+
@io.swagger.v3.oas.annotations.media.Schema(description = "Context ID pair")
28+
public class ContextId implements Comparable<ContextId> {
29+
30+
private String context;
31+
private Integer id;
32+
33+
@JsonCreator
34+
public ContextId(@JsonProperty("context") String context,
35+
@JsonProperty("id") Integer id) {
36+
this.context = context;
37+
this.id = id;
38+
}
39+
40+
@JsonProperty("context")
41+
public String getContext() {
42+
return context;
43+
}
44+
45+
@JsonProperty("context")
46+
public void setContext(String context) {
47+
this.context = context;
48+
}
49+
50+
@JsonProperty("id")
51+
public Integer getId() {
52+
return this.id;
53+
}
54+
55+
@JsonProperty("id")
56+
public void setId(Integer id) {
57+
this.id = id;
58+
}
59+
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) {
63+
return true;
64+
}
65+
if (o == null || getClass() != o.getClass()) {
66+
return false;
67+
}
68+
ContextId schema1 = (ContextId) o;
69+
return Objects.equals(context, schema1.context)
70+
&& Objects.equals(id, schema1.id);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(context, id);
76+
}
77+
78+
@Override
79+
public String toString() {
80+
StringBuilder sb = new StringBuilder();
81+
sb.append("{context=" + this.context + ",");
82+
sb.append("id=" + this.id + "}");
83+
return sb.toString();
84+
}
85+
86+
@Override
87+
public int compareTo(ContextId that) {
88+
int result = this.context.compareTo(that.context);
89+
if (result != 0) {
90+
return result;
91+
}
92+
result = this.id - that.id;
93+
return result;
94+
}
95+
}

client/src/main/java/io/confluent/kafka/schemaregistry/client/rest/entities/ExtendedSchema.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ public class ExtendedSchema extends Schema {
3434

3535
@JsonCreator
3636
public ExtendedSchema(@JsonProperty("subject") String subject,
37-
@JsonProperty("version") Integer version,
38-
@JsonProperty("id") Integer id,
39-
@JsonProperty("schemaType") String schemaType,
40-
@JsonProperty("references") List<SchemaReference> references,
41-
@JsonProperty("metadata") Metadata metadata,
42-
@JsonProperty("ruleset") RuleSet ruleSet,
43-
@JsonProperty("schema") String schema,
44-
@JsonProperty("aliases") List<String> aliases) {
45-
super(subject, version, id, schemaType, references, metadata, ruleSet, schema);
37+
@JsonProperty("version") Integer version,
38+
@JsonProperty("id") Integer id,
39+
@JsonProperty("guid") String guid,
40+
@JsonProperty("schemaType") String schemaType,
41+
@JsonProperty("references") List<SchemaReference> references,
42+
@JsonProperty("metadata") Metadata metadata,
43+
@JsonProperty("ruleset") RuleSet ruleSet,
44+
@JsonProperty("schema") String schema,
45+
@JsonProperty("aliases") List<String> aliases) {
46+
super(subject, version, id, guid, schemaType, references, metadata, ruleSet, schema);
4647
this.aliases = aliases;
4748
}
4849

4950
public ExtendedSchema(Schema schema, List<String> aliases) {
50-
super(schema.getSubject(), schema.getVersion(), schema.getId(), schema.getSchemaType(),
51-
schema.getReferences(), schema.getMetadata(), schema.getRuleSet(), schema.getSchema());
51+
super(schema.getSubject(), schema.getVersion(), schema.getId(), schema.getGuid(),
52+
schema.getSchemaType(), schema.getReferences(), schema.getMetadata(), schema.getRuleSet(),
53+
schema.getSchema());
5254
this.aliases = aliases;
5355
}
5456

@@ -93,6 +95,7 @@ public String toString() {
9395
sb.append("{subject=" + getSubject() + ",");
9496
sb.append("version=" + getVersion() + ",");
9597
sb.append("id=" + getId() + ",");
98+
sb.append("guid=" + getGuid() + ",");
9699
sb.append("schemaType=" + getSchemaType() + ",");
97100
sb.append("references=" + getReferences() + ",");
98101
sb.append("metadata=" + getMetadata() + ",");

0 commit comments

Comments
 (0)