Search in sources :

Example 1 with SchemaReference

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));
}
Also used : SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 2 with SchemaReference

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());
        }
    }
}
Also used : SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema)

Example 3 with SchemaReference

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);
}
Also used : SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema)

Example 4 with SchemaReference

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);
}
Also used : SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema)

Example 5 with SchemaReference

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"));
}
Also used : SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient) RegisterSchemaRequest(io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest) SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) Test(org.junit.Test)

Aggregations

SchemaReference (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference)34 Test (org.junit.Test)20 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)15 ArrayList (java.util.ArrayList)9 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)8 RegisterSchemaRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest)8 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)8 HashMap (java.util.HashMap)8 ByteString (com.google.protobuf.ByteString)6 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)6 Map (java.util.Map)6 ProtobufSchema (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)5 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)4 IOException (java.io.IOException)4 Collections (java.util.Collections)4 List (java.util.List)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)3 Collectors (java.util.stream.Collectors)3