Search in sources :

Example 1 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project kafka-rest by confluentinc.

the class SchemaManagerImpl method findLatestSchema.

private RegisteredSchema findLatestSchema(String topicName, Optional<String> subject, Optional<SubjectNameStrategy> subjectNameStrategy, boolean isKey) {
    String actualSubject = subject.orElse(getSchemaSubjectUnsafe(topicName, isKey, subjectNameStrategy));
    SchemaMetadata metadata;
    try {
        metadata = schemaRegistryClient.getLatestSchemaMetadata(actualSubject);
    } catch (IOException | RestClientException e) {
        throw Errors.messageSerializationException(String.format("Error when fetching latest schema version. subject = %s", actualSubject), e);
    }
    SchemaProvider schemaProvider;
    try {
        schemaProvider = EmbeddedFormat.forSchemaType(metadata.getSchemaType()).getSchemaProvider();
    } catch (UnsupportedOperationException e) {
        throw new BadRequestException(String.format("Schema subject not supported for schema type = %s", metadata.getSchemaType()), e);
    }
    ParsedSchema schema;
    try {
        schema = schemaProvider.parseSchema(metadata.getSchema(), metadata.getReferences(), /* isNew= */
        false).orElseThrow(() -> Errors.invalidSchemaException(String.format("Error when fetching latest schema version. subject = %s", actualSubject)));
    } catch (SchemaParseException e) {
        throw new BadRequestException(String.format("Error parsing schema type = %s", metadata.getSchemaType()), e);
    }
    return RegisteredSchema.create(actualSubject, metadata.getId(), metadata.getVersion(), schema);
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) SchemaParseException(org.apache.avro.SchemaParseException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) IOException(java.io.IOException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema)

Example 2 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorRawSchemaNotSupportedWithSchema.

@Test
public void errorRawSchemaNotSupportedWithSchema() {
    EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
    SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
    expect(embeddedFormatMock.requiresSchema()).andReturn(true);
    expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Reason here"));
    expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawSchema").toString(), emptyList(), true)).andReturn(Optional.empty());
    replay(embeddedFormatMock, schemaProviderMock);
    BadRequestException bre = assertThrows(BadRequestException.class, () -> schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(embeddedFormatMock), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of(TextNode.valueOf("rawSchema").toString()), /* isKey= */
    true));
    assertEquals("Raw schema not supported with format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat", bre.getMessage());
    assertEquals(400, bre.getCode());
}
Also used : EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 3 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorRegisteringSchema.

@Test
public void errorRegisteringSchema() throws RestClientException, IOException {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    ParsedSchema parsedSchemaMock = mock(ParsedSchema.class);
    EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
    SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
    expect(embeddedFormatMock.requiresSchema()).andReturn(true);
    expect(embeddedFormatMock.getSchemaProvider()).andReturn(schemaProviderMock);
    expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawString").toString(), emptyList(), true)).andReturn(Optional.of(parsedSchemaMock));
    expect(schemaRegistryClientMock.getId("subject1", parsedSchemaMock)).andThrow(new IOException("Can't get Schema"));
    expect(schemaRegistryClientMock.register("subject1", parsedSchemaMock)).andThrow(new IOException("Can't register Schema"));
    replay(schemaRegistryClientMock, embeddedFormatMock, schemaProviderMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException rcve = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(embeddedFormatMock), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of(TextNode.valueOf("rawString").toString()), /* isKey= */
    true));
    assertEquals("Error serializing message. Error when registering schema. format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat, subject = subject1, schema = null\n" + "Can't register Schema", rcve.getMessage());
    assertEquals(42207, rcve.getErrorCode());
}
Also used : EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) IOException(java.io.IOException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 4 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorRawSchemaCantParseSchema.

@Test
public void errorRawSchemaCantParseSchema() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
    SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
    expect(embeddedFormatMock.requiresSchema()).andReturn(true);
    expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Unsupported"));
    replay(embeddedFormatMock, schemaProviderMock, schemaRegistryClientMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    BadRequestException rcve = assertThrows(BadRequestException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(embeddedFormatMock), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of(TextNode.valueOf("rawSchema").toString()), /* isKey= */
    true));
    assertEquals("Raw schema not supported with format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat", rcve.getMessage());
    assertEquals(400, rcve.getCode());
}
Also used : EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 5 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project schema-registry by confluentinc.

the class KafkaSchemaRegistry method registerProviders.

private void registerProviders(Map<String, SchemaProvider> providerMap, List<SchemaProvider> schemaProviders) {
    for (SchemaProvider schemaProvider : schemaProviders) {
        log.info("Registering schema provider for {}: {}", schemaProvider.schemaType(), schemaProvider.getClass().getName());
        providerMap.put(schemaProvider.schemaType(), schemaProvider);
    }
}
Also used : ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)

Aggregations

SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)22 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)9 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)7 JsonSchemaProvider (io.confluent.kafka.schemaregistry.json.JsonSchemaProvider)7 ProtobufSchemaProvider (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider)7 BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 CachedSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)4 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)4 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)3 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)3 Test (org.junit.jupiter.api.Test)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)2 StoreException (io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)2 TopicNameStrategy (io.confluent.kafka.serializers.subject.TopicNameStrategy)2 HashMap (java.util.HashMap)2 SchemaParseException (org.apache.avro.SchemaParseException)2 CompatibilityChecker (io.confluent.kafka.schemaregistry.CompatibilityChecker)1