Search in sources :

Example 1 with SchemaString

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

Example 2 with SchemaString

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));
}
Also used : SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) HashMap(java.util.HashMap) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) EasyMock.anyObject(org.easymock.EasyMock.anyObject) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) EasyMock.anyString(org.easymock.EasyMock.anyString) FakeTicker(com.google.common.testing.FakeTicker) Test(org.junit.Test)

Example 3 with SchemaString

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;
}
Also used : SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema)

Example 4 with 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"));
}
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)

Example 5 with SchemaString

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;
}
Also used : HashMap(java.util.HashMap) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)

Aggregations

SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)21 Test (org.junit.Test)14 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)10 SchemaReference (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference)5 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)4 RegisterSchemaRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest)4 ArrayList (java.util.ArrayList)4 ByteString (com.google.protobuf.ByteString)3 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)3 HashMap (java.util.HashMap)3 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)2 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)2 JsonSchema (io.confluent.kafka.schemaregistry.json.JsonSchema)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 EasyMock.anyString (org.easymock.EasyMock.anyString)2 FakeTicker (com.google.common.testing.FakeTicker)1 CachedSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)1 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)1