Skip to content

Commit ef7aa28

Browse files
feat(schema): fallback in case of subject matching the schema id but not the TopicNameStrategy (#1820)
Fix #1795
1 parent 9f687f6 commit ef7aa28

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main/java/org/akhq/controllers/SchemaController.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ private Schema registerSchema(String cluster, @Body Schema schema) throws IOExce
157157
return register;
158158
}
159159

160+
/**
161+
* Find a subject by the schema id
162+
* In case of several subjects matching the schema id, we use the topic name to get the most relevant subject that
163+
* matches the topic name (TopicNameStrategy). If there is no topic or if the topic doesn't match any subject,
164+
* return the first subject that matches the schema id.
165+
*
166+
* @param request - The HTTP request
167+
* @param cluster - The cluster name
168+
* @param id - The schema id
169+
* @param topic - (Optional) The topic name
170+
* @return the most relevant subject
171+
*
172+
* @throws IOException
173+
* @throws RestClientException
174+
*/
160175
@Get("api/{cluster}/schema/id/{id}")
161176
@Operation(tags = {"schema registry"}, summary = "Find a subject by the schema id")
162177
public Schema getSubjectBySchemaIdAndTopic(
@@ -168,11 +183,15 @@ public Schema getSubjectBySchemaIdAndTopic(
168183
// TODO Do the check on the subject name too
169184
checkIfClusterAllowed(cluster);
170185

171-
return this.schemaRepository.getSubjectsBySchemaId(cluster, id)
172-
.stream()
186+
List<Schema> schemas = this.schemaRepository.getSubjectsBySchemaId(cluster, id);
187+
188+
// No topic, return the first subject that matches
189+
// If several subjects match the topic, return the first one
190+
return schemas.stream()
173191
.filter(s -> topic == null || s.getSubject().contains(topic))
174192
.findFirst()
175-
.orElse(null);
193+
// If there is a topic but no match, return the first one that matches to handle subjects not following TopicNameStrategy
194+
.orElseGet(() -> schemas.isEmpty() ? null : schemas.get(0));
176195
}
177196

178197
@Get("api/{cluster}/schema/{subject}/version")

0 commit comments

Comments
 (0)