DBZ-5229 Restore Kafka-based database history options in SchemaGenerator

This commit is contained in:
Chris Cranford 2022-08-26 13:58:38 -04:00 committed by Chris Cranford
parent 3b11c656fe
commit 92ca57d731
3 changed files with 63 additions and 36 deletions

View File

@ -229,6 +229,20 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>io.debezium</groupId>
<artifactId>debezium-schema-generator</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>generate-connector-metadata</id>
<goals>
<goal>generate-api-spec</goal>
</goals>
<phase>prepare-package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -18,6 +18,10 @@
<groupId>io.debezium</groupId>
<artifactId>debezium-core</artifactId>
</dependency>
<dependency>
<groupId>io.debezium</groupId>
<artifactId>debezium-storage-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>

View File

@ -19,7 +19,9 @@
import io.debezium.config.Field;
import io.debezium.metadata.ConnectorMetadata;
import io.debezium.relational.HistorizedRelationalDatabaseConnectorConfig;
import io.debezium.schemagenerator.schema.Schema.FieldFilter;
import io.debezium.storage.kafka.history.KafkaDatabaseHistory;
import io.smallrye.openapi.api.models.media.SchemaImpl;
public class JsonSchemaCreatorService {
@ -121,7 +123,22 @@ public Schema buildConnectorSchema() {
orderedPropertiesByCategory.put(category, new TreeMap<>());
});
connectorMetadata.getConnectorFields().forEach(field -> {
connectorMetadata.getConnectorFields().forEach(field -> processField(schema, orderedPropertiesByCategory, field));
Arrays.stream(Field.Group.values()).forEach(
group -> orderedPropertiesByCategory.get(group).forEach((position, propertySchema) -> schema.addProperty(propertySchema.getName(), propertySchema)));
// Allow additional properties until OAS 3.1 is not avaialble with Swagger/microprofile-openapi
// We need JSON Schema `patternProperties`, defined here: https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
// previously added to OAS 3.1: https://github.com/OAI/OpenAPI-Specification/pull/2489
// see https://github.com/eclipse/microprofile-open-api/issues/333
// see https://github.com/swagger-api/swagger-core/issues/3913
schema.additionalPropertiesBoolean(true);
return schema;
}
private void processField(Schema schema, Map<Field.Group, SortedMap<Integer, SchemaImpl>> orderedPropertiesByCategory, Field field) {
String propertyName = field.name();
Field checkedField = checkField(field);
if (null != checkedField) {
@ -155,19 +172,11 @@ public Schema buildConnectorSchema() {
else {
groupProperties.put(groupEntry.getPositionInGroup(), propertySchema);
}
}
});
Arrays.stream(Field.Group.values()).forEach(
group -> orderedPropertiesByCategory.get(group).forEach((position, propertySchema) -> schema.addProperty(propertySchema.getName(), propertySchema)));
// Allow additional properties until OAS 3.1 is not avaialble with Swagger/microprofile-openapi
// We need JSON Schema `patternProperties`, defined here: https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
// previously added to OAS 3.1: https://github.com/OAI/OpenAPI-Specification/pull/2489
// see https://github.com/eclipse/microprofile-open-api/issues/333
// see https://github.com/swagger-api/swagger-core/issues/3913
schema.additionalPropertiesBoolean(true);
return schema;
if (propertyName.equals(HistorizedRelationalDatabaseConnectorConfig.DATABASE_HISTORY.name())) {
// todo: how to eventually support varied storage modules
KafkaDatabaseHistory.ALL_FIELDS.forEach(historyField -> processField(schema, orderedPropertiesByCategory, historyField));
}
}
}
}