Search in sources :

Example 1 with Schema

use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchemaFromSchemaVersionThrowsInvalidSchemaException.

@Test
public void getSchemaFromSchemaVersionThrowsInvalidSchemaException() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    Schema schemaMock = mock(Schema.class);
    expect(schemaRegistryClientMock.getByVersion("subject1", 0, false)).andReturn(schemaMock);
    expect(schemaMock.getSchemaType()).andReturn(EmbeddedFormat.AVRO.toString());
    expect(schemaMock.getSchema()).andReturn(null);
    expect(schemaMock.getReferences()).andReturn(Collections.emptyList());
    replay(schemaRegistryClientMock, schemaMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException iae = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(0), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertEquals("Invalid schema: Error when fetching schema by version. subject = subject1, version = 0", iae.getMessage());
    assertEquals(42205, iae.getErrorCode());
}
Also used : RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 2 with Schema

use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorFetchingSchemaBySchemaVersion.

@Test
public void errorFetchingSchemaBySchemaVersion() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    Schema schemaMock = mock(Schema.class);
    expect(schemaRegistryClientMock.getByVersion("subject1", 123, false)).andReturn(schemaMock);
    expect(schemaMock.getSchemaType()).andReturn(EmbeddedFormat.JSON.toString());
    expect(schemaMock.getSchema()).andReturn(null);
    expect(schemaMock.getReferences()).andReturn(Collections.emptyList());
    replay(schemaRegistryClientMock, schemaMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException iae = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(123), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertEquals("Invalid schema: Error when fetching schema by version. subject = subject1, version = 123", iae.getMessage());
    assertEquals(42205, iae.getErrorCode());
}
Also used : RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 3 with Schema

use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema 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 4 with Schema

use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project schema-registry by confluentinc.

the class CachedSchemaRegistryClient method getIdFromRegistry.

private int getIdFromRegistry(String subject, ParsedSchema schema, boolean normalize) throws IOException, RestClientException {
    checkMissingSchemaCache(subject, schema, normalize);
    io.confluent.kafka.schemaregistry.client.rest.entities.Schema response;
    try {
        response = restService.lookUpSubjectVersion(schema.canonicalString(), schema.schemaType(), schema.references(), subject, normalize, false);
    } catch (RestClientException rce) {
        if (isSchemaNotFoundException(rce)) {
            missingSchemaCache.put(new SubjectAndSchema(subject, schema, normalize), System.currentTimeMillis());
        }
        throw rce;
    }
    return response.getId();
}
Also used : RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema)

Example 5 with Schema

use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema 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)

Aggregations

Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)49 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)31 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)21 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)18 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)17 Test (org.junit.Test)13 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)11 ArrayList (java.util.ArrayList)11 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)10 SchemaReference (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference)9 Operation (io.swagger.v3.oas.annotations.Operation)9 PerformanceMetric (io.confluent.rest.annotations.PerformanceMetric)8 Path (javax.ws.rs.Path)6 RegisterSchemaRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest)5 VersionId (io.confluent.kafka.schemaregistry.rest.VersionId)5 RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)5 HashMap (java.util.HashMap)5 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)4 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)4 InvalidSchemaException (io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException)4