exceptionfactory commented on code in PR #11710: URL: https://github.com/apache/nifi/pull/11710#discussion_r4083890346
########## nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/test/java/org/apache/nifi/services/protobuf/TestProtobufSchemaCompiler.java: ########## @@ -0,0 +1,393 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.services.protobuf; + +import com.squareup.wire.schema.EnumConstant; +import com.squareup.wire.schema.EnumType; +import com.squareup.wire.schema.Field; +import com.squareup.wire.schema.MessageType; +import com.squareup.wire.schema.OneOf; +import com.squareup.wire.schema.Options; +import com.squareup.wire.schema.ProtoFile; +import com.squareup.wire.schema.Rpc; +import com.squareup.wire.schema.Schema; +import com.squareup.wire.schema.SchemaException; +import com.squareup.wire.schema.Service; +import com.squareup.wire.schema.internal.parser.OptionElement; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.schemaregistry.services.SchemaDefinition; +import org.apache.nifi.schemaregistry.services.StandardSchemaDefinition; +import org.apache.nifi.serialization.record.SchemaIdentifier; +import org.apache.nifi.util.MockComponentLog; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class TestProtobufSchemaCompiler { + + private static final ComponentLog COMPONENT_LOG = new MockComponentLog("test", TestProtobufSchemaCompiler.class); + + private final ProtobufSchemaCompiler compiler = new ProtobufSchemaCompiler("test", COMPONENT_LOG); + + @Test + void testCompileSchemaWithCustomOptionWithoutImport() { + final String schemaText = """ + syntax = "proto3"; + package example; + + message Sample { + optional int32 enabled = 1 [(confluent.field_meta) = { + params: [ + { + key: "connect.type", + value: "int16" + } + ] + }]; + } + """; + final SchemaDefinition schemaDefinition = schemaDefinition("sample.proto", schemaText); + + final Schema schema = compiler.compileOrGetFromCache(schemaDefinition); + + final Field enabled = schema.getField("example.Sample", "enabled"); + + assertNotNull(enabled); + assertNoParenthesizedOptions(enabled.getOptions()); + } + + @Test + void testCompileNestedSchemaWithRepeatedFieldMetaOptions() throws IOException { + final String schemaText = readTestResource("/org/apache/nifi/protobuf/test/nested_wrapper_with_field_meta.proto"); + final SchemaDefinition schemaDefinition = schemaDefinition("nested_wrapper_with_field_meta.proto", schemaText); + + final Schema schema = compiler.compileOrGetFromCache(schemaDefinition); + final String payloadMessageName = "example.sample.Wrapper.Payload"; + + final Field flagA = schema.getField(payloadMessageName, "flag_a"); + final Field flagB = schema.getField(payloadMessageName, "flag_b"); + final Field flagC = schema.getField(payloadMessageName, "flag_c"); + + assertNotNull(schema.getType("example.sample.Wrapper")); + assertNotNull(schema.getType("example.sample.Wrapper.Origin")); + assertNotNull(schema.getType("example.sample.Wrapper.chunk")); + assertNotNull(flagA); + assertNotNull(flagB); + assertNotNull(flagC); + assertNoParenthesizedOptions(flagA.getOptions()); + assertNoParenthesizedOptions(flagB.getOptions()); + assertNoParenthesizedOptions(flagC.getOptions()); + } + + @Test + void testCompileKeepsPackedWhenCustomFieldOptionPresent() { + final String schemaText = """ + syntax = "proto2"; + package example; + + message Sample { + repeated int32 values = 1 [packed = true, (confluent.field_meta) = { + tags: "x" + }]; + } + """; + final SchemaDefinition schemaDefinition = schemaDefinition("packed.proto", schemaText); + + final Schema schema = compiler.compileOrGetFromCache(schemaDefinition); + final Field values = schema.getField("example.Sample", "values"); Review Comment: This `example.Sample` is one of several repeated literals throughout the test methods. Recommend declaring this and others applicable as static final variables. ########## nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/main/java/org/apache/nifi/services/protobuf/ProtobufSchemaCompiler.java: ########## @@ -195,10 +196,10 @@ private void writeSchemaToTempDirectory(final Path tempDir, final SchemaDefiniti final String schemaFileName = generateSchemaFileName(schemaDefinition); final Path schemaFile = tempDir.resolve(schemaFileName); - // Write schema text to file - Files.write(schemaFile, schemaDefinition.getText().getBytes(), CREATE, WRITE, TRUNCATE_EXISTING); + final String cleanedUpSchemaText = ProtobufCustomOptionRemover.remove(schemaDefinition.getText(), Location.get(schemaFileName), logger); Review Comment: I recommend renaming since the original definition text may be returned ```suggestion final String preparedSchemaDefinition = ProtobufCustomOptionRemover.remove(schemaDefinition.getText(), Location.get(schemaFileName), logger); ``` -- 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]
