use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference in project schema-registry by confluentinc.
the class KafkaAvroSerializerTest method testKafkaAvroSerializerWithMultiTypeUnionSpecific.
@Test
public void testKafkaAvroSerializerWithMultiTypeUnionSpecific() throws IOException, RestClientException {
Map serializerConfigs = ImmutableMap.of(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "bogus", KafkaAvroSerializerConfig.AUTO_REGISTER_SCHEMAS, false, KafkaAvroSerializerConfig.USE_LATEST_VERSION, true);
Map deserializerConfigs = ImmutableMap.of(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "bogus", KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
IndexedRecord record = createSpecificAvroRecord();
AvroSchema schema = new AvroSchema(record.getSchema());
schemaRegistry.register("user", schema);
schemaRegistry.register("account", new AvroSchema(createAccountSchema()));
schemaRegistry.register(topic + "-value", new AvroSchema("[ \"io.confluent.kafka.example.User\", \"example.avro.Account\" ]", ImmutableList.of(new SchemaReference("io.confluent.kafka.example.User", "user", 1), new SchemaReference("example.avro.Account", "account", 1)), ImmutableMap.of("io.confluent.kafka.example.User", schema.toString(), "example.avro.Account", createAccountSchema().toString()), null));
avroSerializer.configure(serializerConfigs, false);
avroDeserializer.configure(deserializerConfigs, false);
byte[] bytes1 = avroSerializer.serialize(topic, record);
assertEquals(record, avroDeserializer.deserialize(topic, bytes1));
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference in project schema-registry by confluentinc.
the class AbstractSchemaProvider method resolveReferences.
private void resolveReferences(List<SchemaReference> references, Map<String, String> schemas) {
for (SchemaReference reference : references) {
if (reference.getName() == null || reference.getSubject() == null || reference.getVersion() == null) {
throw new IllegalStateException("Invalid reference: " + reference);
}
String subject = reference.getSubject();
if (!schemas.containsKey(reference.getName())) {
Schema schema = schemaVersionFetcher().getByVersion(subject, reference.getVersion(), true);
if (schema == null) {
throw new IllegalStateException("No schema reference found for subject \"" + subject + "\" and version " + reference.getVersion());
}
if (reference.getVersion() == -1) {
// Update the version with the latest
reference.setVersion(schema.getVersion());
}
resolveReferences(schema.getReferences(), schemas);
schemas.put(reference.getName(), schema.getSchema());
}
}
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference in project schema-registry by confluentinc.
the class CachedSchemaRegistryClient method getLatestSchemaMetadata.
@Override
public SchemaMetadata getLatestSchemaMetadata(String subject) throws IOException, RestClientException {
io.confluent.kafka.schemaregistry.client.rest.entities.Schema response = restService.getLatestVersion(subject);
int id = response.getId();
int version = response.getVersion();
String schemaType = response.getSchemaType();
String schema = response.getSchema();
List<SchemaReference> references = response.getReferences();
return new SchemaMetadata(id, version, schemaType, references, schema);
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference in project schema-registry by confluentinc.
the class CachedSchemaRegistryClient method getSchemaMetadata.
@Override
public SchemaMetadata getSchemaMetadata(String subject, int version) throws IOException, RestClientException {
io.confluent.kafka.schemaregistry.client.rest.entities.Schema response = restService.getVersion(subject, version);
int id = response.getId();
String schemaType = response.getSchemaType();
String schema = response.getSchema();
List<SchemaReference> references = response.getReferences();
return new SchemaMetadata(id, version, schemaType, references, schema);
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference 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"));
}
Aggregations