use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestService method getId.
public SchemaString getId(Map<String, String> requestProperties, int id, String subject, boolean fetchMaxId) throws IOException, RestClientException {
UriBuilder builder = UriBuilder.fromPath("/schemas/ids/{id}").queryParam("fetchMaxId", fetchMaxId);
if (subject != null) {
builder.queryParam("subject", subject);
}
String path = builder.build(id).toString();
SchemaString response = httpRequest(path, "GET", null, requestProperties, GET_SCHEMA_BY_ID_RESPONSE_TYPE);
return response;
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class CachedSchemaRegistryClientTest method testMissingIdCache.
@Test
public void testMissingIdCache() throws Exception {
Map<String, Object> configs = new HashMap<>();
configs.put(SchemaRegistryClientConfig.MISSING_ID_CACHE_TTL_CONFIG, 60L);
configs.put(SchemaRegistryClientConfig.MISSING_SCHEMA_CACHE_TTL_CONFIG, 60L);
FakeTicker fakeTicker = new FakeTicker();
client = new CachedSchemaRegistryClient(restService, CACHE_CAPACITY, null, configs, null, fakeTicker);
expect(restService.getId(ID_25, SUBJECT_0)).andThrow(new RestClientException("Schema 25 not found", 404, 40403)).andReturn(new SchemaString(SCHEMA_STR_0));
replay(restService);
try {
client.getSchemaBySubjectAndId(SUBJECT_0, ID_25);
fail();
} catch (RestClientException rce) {
assertEquals("Schema 25 not found; error code: 40403", rce.getMessage());
}
fakeTicker.advance(59, TimeUnit.SECONDS);
// Should hit the cache
try {
client.getSchemaBySubjectAndId(SUBJECT_0, ID_25);
fail();
} catch (RestClientException rce) {
assertEquals("Schema 25 not found; error code: 40403", rce.getMessage());
}
fakeTicker.advance(2, TimeUnit.SECONDS);
Thread.sleep(100);
assertNotNull(client.getSchemaBySubjectAndId(SUBJECT_0, ID_25));
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class KafkaSchemaRegistry method get.
public SchemaString get(int id, String subject, String format, boolean fetchMaxId) throws SchemaRegistryException {
SchemaValue schema = null;
try {
SchemaKey subjectVersionKey = getSchemaKeyUsingContexts(id, subject);
if (subjectVersionKey == null) {
return null;
}
schema = (SchemaValue) kafkaStore.get(subjectVersionKey);
if (schema == null) {
return null;
}
} catch (StoreException e) {
throw new SchemaRegistryStoreException("Error while retrieving schema with id " + id + " from the backend Kafka" + " store", e);
}
SchemaString schemaString = new SchemaString();
schemaString.setSchemaType(schema.getSchemaType());
List<io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference> refs = schema.getReferences() != null ? schema.getReferences().stream().map(ref -> new io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference(ref.getName(), ref.getSubject(), ref.getVersion())).collect(Collectors.toList()) : null;
schemaString.setReferences(refs);
if (format != null && !format.trim().isEmpty()) {
ParsedSchema parsedSchema = parseSchema(schema.getSchemaType(), schema.getSchema(), refs, false);
schemaString.setSchemaString(parsedSchema.formattedString(format));
} else {
schemaString.setSchemaString(schema.getSchema());
}
if (fetchMaxId) {
schemaString.setMaxId(idGenerator.getMaxId(schema));
}
return schemaString;
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestApiTest method testSchemaReferences.
@Test
public void testSchemaReferences() throws Exception {
Map<String, String> schemas = getJsonSchemaWithReferences();
String subject = "reference";
registerAndVerifySchema(restApp.restClient, schemas.get("ref.json"), 1, subject);
RegisterSchemaRequest request = new RegisterSchemaRequest();
request.setSchema(schemas.get("main.json"));
request.setSchemaType(JsonSchema.TYPE);
SchemaReference ref = new SchemaReference("ref.json", "reference", 1);
request.setReferences(Collections.singletonList(ref));
int registeredId = restApp.restClient.registerSchema(request, "referrer", false);
assertEquals("Registering a new schema should succeed", 2, registeredId);
SchemaString schemaString = restApp.restClient.getId(2);
// the newly registered schema should be immediately readable on the leader
assertEquals("Registered schema should be found", MAPPER.readTree(schemas.get("main.json")), MAPPER.readTree(schemaString.getSchemaString()));
assertEquals("Schema references should be found", Collections.singletonList(ref), schemaString.getReferences());
List<Integer> refs = restApp.restClient.getReferencedBy("reference", 1);
assertEquals(2, refs.get(0).intValue());
CachedSchemaRegistryClient schemaRegistryClient = new CachedSchemaRegistryClient(restApp.restClient, 10, Collections.singletonList(new JsonSchemaProvider()), new HashMap<>(), null);
SchemaHolder holder = new SchemaHolder();
JsonSchema schema = JsonSchemaUtils.getSchema(holder, schemaRegistryClient);
Schema registeredSchema = restApp.restClient.lookUpSubjectVersion(schema.canonicalString(), JsonSchema.TYPE, schema.references(), "referrer", false);
assertEquals("Registered schema should be found", 2, registeredSchema.getId().intValue());
try {
restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "reference", String.valueOf(1));
fail("Deleting reference should fail with " + Errors.REFERENCE_EXISTS_ERROR_CODE);
} catch (RestClientException rce) {
assertEquals("Reference found", Errors.REFERENCE_EXISTS_ERROR_CODE, rce.getErrorCode());
}
assertEquals((Integer) 1, restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "referrer", "1"));
refs = restApp.restClient.getReferencedBy("reference", 1);
assertTrue(refs.isEmpty());
assertEquals((Integer) 1, restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "reference", "1"));
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestApiTest method getJsonSchemaWithReferences.
public static Map<String, String> getJsonSchemaWithReferences() {
Map<String, String> schemas = new HashMap<>();
String reference = "{\"type\":\"object\",\"additionalProperties\":false,\"definitions\":" + "{\"ExternalType\":{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"}}," + "\"additionalProperties\":false}}}";
schemas.put("ref.json", new JsonSchema(reference).canonicalString());
String schemaString = "{" + "\"$id\": \"https://acme.com/referrer.json\"," + "\"$schema\": \"http://json-schema.org/draft-07/schema#\"," + "\"type\":\"object\",\"properties\":{\"Ref\":" + "{\"$ref\":\"ref.json#/definitions/ExternalType\"}},\"additionalProperties\":false}";
schemas.put("main.json", schemaString);
return schemas;
}
Aggregations