ebAtUelzener commented on code in PR #3614: URL: https://github.com/apache/avro/pull/3614#discussion_r3325140758
########## lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/CompileAvroSchemaTask.kt: ########## @@ -0,0 +1,86 @@ +/* +* 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 +* +* https://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 eu.eventloopsoftware.avro.gradle.plugin.tasks + +import java.io.File +import java.io.IOException +import org.apache.avro.Protocol +import org.apache.avro.SchemaParseException +import org.apache.avro.SchemaParser +import org.apache.avro.compiler.specific.SpecificCompiler +import org.gradle.api.GradleException +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.SkipWhenEmpty +import org.gradle.api.tasks.TaskAction + +abstract class CompileAvroSchemaTask : AbstractCompileTask() { + + @get:InputFiles @get:SkipWhenEmpty abstract val schemaFiles: ConfigurableFileCollection + + @get:InputFiles @get:SkipWhenEmpty abstract val protocolFiles: ConfigurableFileCollection + + @TaskAction + fun compileSchema() { + logger.info("Generating Java files from ${schemaFiles.files.size} Avro schemas...") + + compileSchemas(schemaFiles, outputDirectory.get().asFile) + + logger.info("Done generating Java files from Avro schemas...") + } + + private fun compileSchemas(schemaFileTree: ConfigurableFileCollection, outputDirectory: File) { + val sourceFileForModificationDetection: File? = + schemaFileTree + .asFileTree + .files + .filter { file: File -> file.lastModified() > 0 } + .maxByOrNull { it.lastModified() } + + // Need to register custom logical type factories before schema compilation. + try { + loadLogicalTypesFactories() + } catch (e: IOException) { + throw GradleException("Error while loading logical types factories ", e) + } + + try { + val parser = SchemaParser() + for (sourceFile in schemaFileTree.files) { + parser.parse(sourceFile) + } Review Comment: Thanks for clearing that up, then i assumed to early by just looking at the plugins at too high of a level. This does _not_ mean that there is no problem at all however. Perhaps the cause lies inside the validator/ how it is used which is how the problem i mentioned occurs. For reference, when a problem like i initially mentioned as an example arises, you can observe that the task fails with a cause inside the validation `Schema.validateDefault`: ``` org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':avroGenerateJavaClasses' <gradle stack> Caused by: org.apache.avro.AvroTypeException: Invalid default for field myEnum: "NONE" not a {"type":"record","name":"UnresolvedSchema_1","namespace":"org.apache.avro.compiler","doc":"unresolved schema","fields":[],"org.apache.avro.idl.unresolved.name":"some.MyEnum"} at org.apache.avro.Schema.validateDefault(Schema.java:1720) at org.apache.avro.Schema$Field.<init>(Schema.java:579) at org.apache.avro.Schema.parseField(Schema.java:1904) at org.apache.avro.Schema.parseRecord(Schema.java:1872) at org.apache.avro.Schema.parse(Schema.java:1836) at org.apache.avro.Schema$Parser.parse(Schema.java:1539) at org.apache.avro.Schema$Parser.parseInternal(Schema.java:1524) at org.apache.avro.JsonSchemaParser.parse(JsonSchemaParser.java:86) at org.apache.avro.JsonSchemaParser.parse(JsonSchemaParser.java:77) at org.apache.avro.SchemaParser.parse(SchemaParser.java:245) at org.apache.avro.SchemaParser.parse(SchemaParser.java:137) at org.apache.avro.SchemaParser.parse(SchemaParser.java:103) at org.apache.avro.SchemaParser.parse(SchemaParser.java:88) at eu.eventloopsoftware.avro.gradle.plugin.tasks.CompileAvroSchemaTask.compileSchemas(CompileAvroSchemaTask.kt:65) at eu.eventloopsoftware.avro.gradle.plugin.tasks.CompileAvroSchemaTask.compileSchema(CompileAvroSchemaTask.kt:42) <gradle stack> ``` This can be reproduced (somewhat) by having two files, an enum and a record like so: Enum schema: ```json { "type": "enum", "name": "MyEnum", "namespace": "some", "symbols": [ "SOME" "NONE" ] } ``` Record schema: ```json { "type": "record", "name": "MyRecord", "namespace": "some", "fields": [ { "name": "myEnum", "type": "MyEnum", "default": "NONE" } ] } ``` Question is, is this a bug inside the avro-compiler package rather than an issue here and if so should the plugin accomodate for this bug in the meantime or not? -- 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]
