Search in sources :

Example 1 with CompatibilityCheckResponse

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

the class CompatibilityResource method createCompatiblityCheckResponse.

private static CompatibilityCheckResponse createCompatiblityCheckResponse(List<String> errorMessages, boolean verbose) {
    CompatibilityCheckResponse compatibilityCheckResponse = new CompatibilityCheckResponse();
    compatibilityCheckResponse.setIsCompatible(errorMessages.isEmpty());
    if (verbose) {
        compatibilityCheckResponse.setMessages(errorMessages);
    }
    return compatibilityCheckResponse;
}
Also used : CompatibilityCheckResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse)

Example 2 with CompatibilityCheckResponse

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

the class CompatibilityResource method testCompatibilityForSubject.

@POST
@Path("/subjects/{subject}/versions")
@Operation(summary = "Test input schema against a subject's schemas for compatibility, " + "based on the compatibility level of the subject configured. In other word, " + "it will perform the same compatibility check as register for that subject", description = "the compatibility level applied for the check is the configured compatibility level " + "for the subject (http:get:: /config/(string: subject)). If this subject's " + "compatibility level was never changed, then the global compatibility level " + "applies (http:get:: /config).", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = CompatibilityCheckResponse.class))), @ApiResponse(responseCode = "422", description = "Error code 42201 -- Invalid schema or schema type\n" + "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the " + "backend data store") })
@PerformanceMetric("compatibility.subjects.versions.verify")
public void testCompatibilityForSubject(@Suspended final AsyncResponse asyncResponse, @Parameter(description = "Subject of the schema version against which compatibility is to " + "be tested", required = true) @PathParam("subject") String subject, @Parameter(description = "Schema", required = true) @NotNull RegisterSchemaRequest request, @Parameter(description = "Whether to return detailed error messages") @QueryParam("verbose") boolean verbose) {
    log.info("Testing schema subject {} compatibility with specified version {}, id {}, type {}", subject, request.getVersion(), request.getId(), request.getSchemaType());
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    // returns true if posted schema is compatible with the specified subject.
    List<String> errorMessages;
    List<Schema> previousSchemas = new ArrayList<>();
    try {
        // Don't check compatibility against deleted schema
        schemaRegistry.getAllVersions(subject, false).forEachRemaining(previousSchemas::add);
    } catch (SchemaRegistryException e) {
        throw Errors.storeException("Error while retrieving schema for subject " + subject, e);
    }
    Schema schema = new Schema(subject, 0, -1, request.getSchemaType() != null ? request.getSchemaType() : AvroSchema.TYPE, request.getReferences(), request.getSchema());
    try {
        errorMessages = schemaRegistry.isCompatible(subject, schema, previousSchemas);
    } catch (InvalidSchemaException e) {
        if (verbose) {
            errorMessages = Collections.singletonList(e.getMessage());
        } else {
            throw Errors.invalidSchemaException(e);
        }
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Error while getting compatibility level for subject " + subject, e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while getting compatibility level for subject " + subject, e);
    }
    CompatibilityCheckResponse compatibilityCheckResponse = createCompatiblityCheckResponse(errorMessages, verbose);
    asyncResponse.resume(compatibilityCheckResponse);
}
Also used : CompatibilityCheckResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) POST(javax.ws.rs.POST) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with CompatibilityCheckResponse

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

the class CompatibilityResource method testCompatibilityBySubjectName.

@POST
@Path("/subjects/{subject}/versions/{version}")
@Operation(summary = "Test input schema against a particular version of a subject's schema for " + "compatibility.", description = "the compatibility level applied for the check is the configured compatibility level " + "for the subject (http:get:: /config/(string: subject)). If this subject's " + "compatibility level was never changed, then the global compatibility level " + "applies (http:get:: /config).", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = CompatibilityCheckResponse.class))), @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42201 -- Invalid schema or schema type\n" + "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the " + "backend data store") })
@PerformanceMetric("compatibility.subjects.versions.verify")
public void testCompatibilityBySubjectName(@Suspended final AsyncResponse asyncResponse, @Parameter(description = "Subject of the schema version against which compatibility is to " + "be tested", required = true) @PathParam("subject") String subject, @Parameter(description = "Version of the subject's schema against which compatibility is to be " + "tested. Valid values for versionId are between [1,2^31-1] or the string " + "\"latest\"." + "\"latest\" checks compatibility of the input schema with the last registered " + "schema " + "under the specified subject", required = true) @PathParam("version") String version, @Parameter(description = "Schema", required = true) @NotNull RegisterSchemaRequest request, @Parameter(description = "Whether to return detailed error messages") @QueryParam("verbose") boolean verbose) {
    log.info("Testing schema subject {} compatibility between existing version {} and " + "specified version {}, id {}, type {}", subject, version, request.getVersion(), request.getId(), request.getSchemaType());
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    // returns true if posted schema is compatible with the specified version. "latest" is
    // a special version
    List<String> errorMessages;
    VersionId versionId = parseVersionId(version);
    Schema schemaForSpecifiedVersion;
    try {
        // Don't check compatibility against deleted schema
        schemaForSpecifiedVersion = schemaRegistry.get(subject, versionId.getVersionId(), false);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.storeException("Error while retrieving schema for subject " + subject + " and version " + versionId.getVersionId(), e);
    }
    if (schemaForSpecifiedVersion == null && !versionId.isLatest()) {
        throw Errors.versionNotFoundException(versionId.getVersionId());
    }
    Schema schema = new Schema(subject, 0, -1, request.getSchemaType() != null ? request.getSchemaType() : AvroSchema.TYPE, request.getReferences(), request.getSchema());
    try {
        errorMessages = schemaRegistry.isCompatible(subject, schema, schemaForSpecifiedVersion != null ? Collections.singletonList(schemaForSpecifiedVersion) : Collections.emptyList());
    } catch (InvalidSchemaException e) {
        if (verbose) {
            errorMessages = Collections.singletonList(e.getMessage());
        } else {
            throw Errors.invalidSchemaException(e);
        }
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Error while getting compatibility level for subject " + subject, e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while getting compatibility level for subject " + subject, e);
    }
    CompatibilityCheckResponse compatibilityCheckResponse = createCompatiblityCheckResponse(errorMessages, verbose);
    asyncResponse.resume(compatibilityCheckResponse);
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) CompatibilityCheckResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) POST(javax.ws.rs.POST) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with CompatibilityCheckResponse

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

the class RestService method testCompatibility.

public List<String> testCompatibility(Map<String, String> requestProperties, RegisterSchemaRequest registerSchemaRequest, String subject, String version, boolean verbose) throws IOException, RestClientException {
    String path;
    if (version != null) {
        path = UriBuilder.fromPath("/compatibility/subjects/{subject}/versions/{version}").queryParam("verbose", verbose).build(subject, version).toString();
    } else {
        path = UriBuilder.fromPath("/compatibility/subjects/{subject}/versions/").queryParam("verbose", verbose).build(subject).toString();
    }
    CompatibilityCheckResponse response = httpRequest(path, "POST", registerSchemaRequest.toJson().getBytes(StandardCharsets.UTF_8), requestProperties, COMPATIBILITY_CHECK_RESPONSE_TYPE_REFERENCE);
    if (verbose) {
        return response.getMessages() == null ? Collections.emptyList() : response.getMessages();
    } else {
        return response.getIsCompatible() ? Collections.emptyList() : Collections.singletonList("Schemas are incompatible");
    }
}
Also used : CompatibilityCheckResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)

Aggregations

CompatibilityCheckResponse (io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse)4 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)2 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)2 InvalidSchemaException (io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException)2 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)2 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)2 PerformanceMetric (io.confluent.rest.annotations.PerformanceMetric)2 Operation (io.swagger.v3.oas.annotations.Operation)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)1 InvalidVersionException (io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException)1 VersionId (io.confluent.kafka.schemaregistry.rest.VersionId)1 ArrayList (java.util.ArrayList)1