lkuchars commented on code in PR #10105:
URL: https://github.com/apache/nifi/pull/10105#discussion_r2270402371
##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-schema-registry-service/src/main/java/org/apache/nifi/confluent/schemaregistry/client/RestSchemaRegistryClient.java:
##########
@@ -211,6 +221,91 @@ public RecordSchema getSchema(final int schemaId) throws
IOException, SchemaNotF
return createRecordSchema(completeSchema);
}
+ @Override
+ public SchemaDefinition getSchemaDefinition(SchemaIdentifier identifier)
throws SchemaNotFoundException {
+ JsonNode schemaJson;
+ String subject = null;
+ Integer version = null;
+
+ // If we have an ID, get the schema by ID first
+ // Using schemaVersionId, because that is what is set by
ConfluentEncodedSchemaReferenceReader.
+ // probably identifier field should be used, but I'm not changing
ConfluentEncodedSchemaReferenceReader for backward compatibility reasons.
+ if (identifier.getSchemaVersionId().isPresent()) {
+ long schemaId = identifier.getSchemaVersionId().getAsLong();
+ String schemaPath = getSchemaPath(schemaId);
+ schemaJson = fetchJsonResponse(schemaPath, "id " + schemaId);
+ } else if (identifier.getName().isPresent()) {
+ // If we have a name or (name and version), get the schema by those
+ subject = identifier.getName().get();
+ version = identifier.getVersion().isPresent() ?
identifier.getVersion().getAsInt() : null;
+ // if no version was specified, the latest version will be used.
See @getSubjectPath method.
+ String pathSuffix = getSubjectPath(subject, version);
+ schemaJson = fetchJsonResponse(pathSuffix, "name " + subject);
+ } else {
+ throw new SchemaNotFoundException("Schema identifier must contain
either a version identifier or a subject name");
+ }
+
+ // Extract schema information
+ String schemaText = schemaJson.get(SCHEMA_TEXT_FIELD_NAME).asText();
+ String schemaTypeText =
schemaJson.get(SCHEMA_TYPE_FIELD_NAME).asText();
+ SchemaType schemaType = toSchemaType(schemaTypeText);
+
+ long schemaId;
+ if (schemaJson.has(ID_FIELD_NAME)) {
+ schemaId = schemaJson.get(ID_FIELD_NAME).asLong();
+ } else {
+ schemaId = identifier.getSchemaVersionId().getAsLong();
+ }
+
+ if (subject == null && schemaJson.has(SUBJECT_FIELD_NAME)) {
+ subject = schemaJson.get(SUBJECT_FIELD_NAME).asText();
+ }
+
+ if (version == null && schemaJson.has(VERSION_FIELD_NAME)) {
+ version = schemaJson.get(VERSION_FIELD_NAME).asInt();
+ }
+
+ // Build schema identifier with all available information
+ SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder()
+ .id(schemaId)
+ .name(subject)
+ .version(version)
+ .build();
+
+ // Process references if present
+ Map<String, SchemaDefinition> references = new HashMap<>();
+ if (schemaJson.has(REFERENCES_FIELD_NAME) &&
!schemaJson.get(REFERENCES_FIELD_NAME).isNull()) {
+ ArrayNode refsArray = (ArrayNode)
schemaJson.get(REFERENCES_FIELD_NAME);
+ for (JsonNode ref : refsArray) {
+ String refName = ref.get(REFERENCE_NAME_FIELD_NAME).asText();
+ String refSubject =
ref.get(REFERENCE_SUBJECT_FIELD_NAME).asText();
+ int refVersion = ref.get(REFERENCE_VERSION_FIELD_NAME).asInt();
+
+ // Recursively get referenced schema
+ SchemaIdentifier refId = SchemaIdentifier.builder()
+ .name(refSubject)
+ .version(refVersion)
+ .build();
+ SchemaDefinition refSchema = getSchemaDefinition(refId);
+ references.put(refName, refSchema);
+ }
+ }
+
+ return new StandardSchemaDefinition(schemaIdentifier, schemaText,
schemaType, references);
+ }
+
+ private SchemaType toSchemaType(final String schemaTypeText) {
+ try {
+ if (schemaTypeText == null || schemaTypeText.isEmpty()) {
+ return SchemaType.AVRO; // Default schema type for confluent
schema registry is AVRO.
+ }
+ return SchemaType.valueOf(schemaTypeText.toUpperCase().trim());
+ } catch (final Exception e) {
+ final String error = String.format("Could not convert schema type
'%s' to SchemaType enum.", schemaTypeText);
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]