Skip to content

Commit c732544

Browse files
committed
feat: add new get schema id endpoint
1 parent 608b076 commit c732544

File tree

5 files changed

+258
-9
lines changed

5 files changed

+258
-9
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
2626

2727
import io.confluent.kafka.schemaregistry.client.SchemaMetadata;
28+
import io.confluent.kafka.schemaregistry.client.rest.entities.requests.FindSchemaIdRequest;
2829
import io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest;
2930
import io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaResponse;
3031
import java.nio.charset.StandardCharsets;
@@ -164,6 +165,12 @@ public Schema(String subject, RegisterSchemaRequest request) {
164165
this.schema = request.getSchema();
165166
}
166167

168+
public Schema(FindSchemaIdRequest request) {
169+
this.schemaType = request.getSchemaType() != null
170+
? request.getSchemaType() : AvroSchema.TYPE;
171+
this.schema = request.getSchema();
172+
}
173+
167174
public Schema(String subject, RegisterSchemaResponse response) {
168175
this.subject = subject;
169176
this.version = response.getVersion() != null ? response.getVersion() : 0;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2018 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.requests;
18+
19+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20+
import com.fasterxml.jackson.annotation.JsonInclude;
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
23+
import io.confluent.kafka.schemaregistry.client.rest.entities.Schema;
24+
import io.confluent.kafka.schemaregistry.client.rest.entities.SchemaTypeConverter;
25+
import io.confluent.kafka.schemaregistry.utils.JacksonMapper;
26+
import java.io.IOException;
27+
import java.util.Objects;
28+
29+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
30+
@JsonIgnoreProperties(ignoreUnknown = true)
31+
@io.swagger.v3.oas.annotations.media.Schema(description = "Find schema id request")
32+
public class FindSchemaIdRequest {
33+
34+
private String schemaType;
35+
private String schema;
36+
37+
public FindSchemaIdRequest() {
38+
}
39+
40+
public static FindSchemaIdRequest fromJson(String json) throws IOException {
41+
return JacksonMapper.INSTANCE.readValue(json, FindSchemaIdRequest.class);
42+
}
43+
44+
@io.swagger.v3.oas.annotations.media.Schema(description = Schema.TYPE_DESC)
45+
@JsonProperty("schemaType")
46+
@JsonSerialize(converter = SchemaTypeConverter.class)
47+
public String getSchemaType() {
48+
return this.schemaType;
49+
}
50+
51+
@JsonProperty("schemaType")
52+
public void setSchemaType(String schemaType) {
53+
this.schemaType = schemaType;
54+
}
55+
56+
@io.swagger.v3.oas.annotations.media.Schema(description = Schema.SCHEMA_DESC)
57+
@JsonProperty("schema")
58+
public String getSchema() {
59+
return this.schema;
60+
}
61+
62+
@JsonProperty("schema")
63+
public void setSchema(String schema) {
64+
this.schema = schema;
65+
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) {
70+
return true;
71+
}
72+
if (o == null || getClass() != o.getClass()) {
73+
return false;
74+
}
75+
FindSchemaIdRequest that = (FindSchemaIdRequest) o;
76+
return Objects.equals(schemaType, that.schemaType)
77+
&& Objects.equals(schema, that.schema);
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(schemaType, schema);
83+
}
84+
85+
@Override
86+
public String toString() {
87+
StringBuilder buf = new StringBuilder();
88+
buf.append("{");
89+
buf.append("schemaType=").append(this.schemaType).append(", ");
90+
buf.append("schema=").append(schema).append("}");
91+
return buf.toString();
92+
}
93+
94+
public String toJson() throws IOException {
95+
return JacksonMapper.INSTANCE.writeValueAsString(this);
96+
}
97+
98+
}

core/generated/swagger-ui/schema-registry-api-spec.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,70 @@ paths:
21312131
Error code 40403 -- Schema not found
21322132
"500":
21332133
description: Internal server error
2134+
/schemas/ids:
2135+
post:
2136+
tags:
2137+
- Schemas (v1)
2138+
summary: Find global schema id
2139+
description: Get the schemas id matching the specified schema.
2140+
operationId: findSchemaId
2141+
requestBody:
2142+
description: Schema
2143+
content:
2144+
application/vnd.schemaregistry.v1+json:
2145+
schema:
2146+
$ref: '#/components/schemas/FindSchemaIdRequest'
2147+
application/vnd.schemaregistry+json:
2148+
schema:
2149+
$ref: '#/components/schemas/FindSchemaIdRequest'
2150+
application/json:
2151+
schema:
2152+
$ref: '#/components/schemas/FindSchemaIdRequest'
2153+
application/octet-stream:
2154+
schema:
2155+
$ref: '#/components/schemas/FindSchemaIdRequest'
2156+
required: true
2157+
responses:
2158+
"200":
2159+
description: Returns the global schema id.
2160+
content:
2161+
application/vnd.schemaregistry.v1+json:
2162+
schema:
2163+
type: integer
2164+
format: int32
2165+
application/vnd.schemaregistry+json; qs=0.9:
2166+
schema:
2167+
type: integer
2168+
format: int32
2169+
application/json; qs=0.5:
2170+
schema:
2171+
type: integer
2172+
format: int32
2173+
"404":
2174+
description: Not Found. Error code 40403 indicates schema not found.
2175+
content:
2176+
application/vnd.schemaregistry.v1+json:
2177+
schema:
2178+
$ref: '#/components/schemas/ErrorMessage'
2179+
application/vnd.schemaregistry+json; qs=0.9:
2180+
schema:
2181+
$ref: '#/components/schemas/ErrorMessage'
2182+
application/json; qs=0.5:
2183+
schema:
2184+
$ref: '#/components/schemas/ErrorMessage'
2185+
"500":
2186+
description: Internal Server Error. Error code 50001 indicates a failure
2187+
in the backend data store.
2188+
content:
2189+
application/vnd.schemaregistry.v1+json:
2190+
schema:
2191+
$ref: '#/components/schemas/ErrorMessage'
2192+
application/vnd.schemaregistry+json; qs=0.9:
2193+
schema:
2194+
$ref: '#/components/schemas/ErrorMessage'
2195+
application/json; qs=0.5:
2196+
schema:
2197+
$ref: '#/components/schemas/ErrorMessage'
21342198
components:
21352199
schemas:
21362200
CompatibilityCheckResponse:
@@ -2495,3 +2559,23 @@ components:
24952559
items:
24962560
$ref: '#/components/schemas/Rule'
24972561
description: Schema rule set
2562+
FindSchemaIdRequest:
2563+
type: object
2564+
properties:
2565+
schemaType:
2566+
type: string
2567+
description: Schema type
2568+
schema:
2569+
type: string
2570+
description: Schema definition string
2571+
description: Find schema id request
2572+
SchemaEntity:
2573+
type: object
2574+
properties:
2575+
entityPath:
2576+
type: string
2577+
entityType:
2578+
type: string
2579+
enum:
2580+
- sr_record
2581+
- sr_field

core/src/main/java/io/confluent/kafka/schemaregistry/rest/resources/SchemasResource.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.confluent.kafka.schemaregistry.client.rest.entities.Schema;
2121
import io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString;
2222
import io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion;
23+
import io.confluent.kafka.schemaregistry.client.rest.entities.requests.FindSchemaIdRequest;
2324
import io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException;
2425
import io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException;
2526
import io.confluent.kafka.schemaregistry.rest.exceptions.Errors;
@@ -33,21 +34,23 @@
3334
import io.swagger.v3.oas.annotations.responses.ApiResponse;
3435
import io.swagger.v3.oas.annotations.tags.Tag;
3536
import io.swagger.v3.oas.annotations.tags.Tags;
37+
import java.util.ArrayList;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
import java.util.Optional;
41+
import java.util.Set;
3642
import java.util.function.Predicate;
37-
import org.slf4j.Logger;
38-
import org.slf4j.LoggerFactory;
39-
43+
import javax.validation.constraints.NotNull;
4044
import javax.ws.rs.Consumes;
45+
import javax.ws.rs.DefaultValue;
4146
import javax.ws.rs.GET;
47+
import javax.ws.rs.POST;
4248
import javax.ws.rs.Path;
49+
import javax.ws.rs.PathParam;
4350
import javax.ws.rs.Produces;
44-
import javax.ws.rs.DefaultValue;
4551
import javax.ws.rs.QueryParam;
46-
import javax.ws.rs.PathParam;
47-
import java.util.ArrayList;
48-
import java.util.Iterator;
49-
import java.util.List;
50-
import java.util.Set;
52+
import org.slf4j.Logger;
53+
import org.slf4j.LoggerFactory;
5154

5255
@Path("/schemas")
5356
@Produces({Versions.SCHEMA_REGISTRY_V1_JSON_WEIGHTED,
@@ -66,6 +69,48 @@ public SchemasResource(KafkaSchemaRegistry schemaRegistry) {
6669
this.schemaRegistry = schemaRegistry;
6770
}
6871

72+
@POST
73+
@Path("/ids")
74+
@DocumentedName("findSchemaId")
75+
@Operation(summary = "Find global schema id",
76+
description = "Get the schemas id matching the specified schema.",
77+
responses = {
78+
@ApiResponse(responseCode = "200",
79+
description = "Returns the global schema id.", content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(
80+
implementation = Integer.class))),
81+
@ApiResponse(responseCode = "404",
82+
description = "Not Found. Error code 40403 indicates schema not found.",
83+
content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation =
84+
ErrorMessage.class))),
85+
@ApiResponse(responseCode = "500",
86+
description = "Internal Server Error. "
87+
+ "Error code 50001 indicates a failure in the backend data store.",
88+
content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation =
89+
ErrorMessage.class)))})
90+
@Tags(@Tag(name = apiTag))
91+
@PerformanceMetric("schemas.find-schema-id")
92+
public Integer findSchemaId(
93+
@Parameter(description = "Schema", required = true)
94+
@NotNull FindSchemaIdRequest request
95+
) {
96+
Optional<Integer> id;
97+
String errorMessage = "Error while finding schema";
98+
Schema schema = new Schema(request);
99+
100+
try {
101+
id = schemaRegistry.findSchemaId(schema);
102+
} catch (SchemaRegistryStoreException e) {
103+
log.debug(errorMessage, e);
104+
throw Errors.storeException(errorMessage, e);
105+
}
106+
107+
if (!id.isPresent()) {
108+
throw Errors.schemaNotFoundException();
109+
}
110+
111+
return id.get();
112+
}
113+
69114
@GET
70115
@DocumentedName("getSchemas")
71116
@Operation(summary = "List schemas",

core/src/main/java/io/confluent/kafka/schemaregistry/storage/KafkaSchemaRegistry.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import java.util.List;
9494
import java.util.Map;
9595
import java.util.Objects;
96+
import java.util.Optional;
9697
import java.util.Properties;
9798
import java.util.Set;
9899
import java.util.concurrent.ConcurrentHashMap;
@@ -1669,6 +1670,20 @@ public Set<String> listSubjectsWithPrefix(String prefix, LookupFilter filter)
16691670
}
16701671
}
16711672

1673+
public Optional<Integer> findSchemaId(Schema schema)
1674+
throws SchemaRegistryStoreException {
1675+
1676+
SchemaIdAndSubjects schemaIdAndSubjects;
1677+
1678+
try {
1679+
schemaIdAndSubjects = this.lookupCache.schemaIdAndSubjects(schema);
1680+
} catch (StoreException e) {
1681+
throw new SchemaRegistryStoreException("Error while retrieving schema", e);
1682+
}
1683+
1684+
return schemaIdAndSubjects!= null ? Optional.of(schemaIdAndSubjects.getSchemaId()) : Optional.empty();
1685+
}
1686+
16721687
public Set<String> listSubjectsForId(int id, String subject) throws SchemaRegistryException {
16731688
return listSubjectsForId(id, subject, false);
16741689
}

0 commit comments

Comments
 (0)