You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "henryf1991 (via GitHub)" <gi...@apache.org> on 2024/05/01 22:35:13 UTC

[PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

henryf1991 opened a new pull request, #2887:
URL: https://github.com/apache/avro/pull/2887

   Add support for custom AvroTypeName annotation to allow overriding namespace when generating schema with reflection
   
   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   Add support for custom AvroTypeName annotation to allow overriding namespace when generating schema with reflection
   
   
   ## Verifying this change
   
   
   This change added tests and can be verified as follows:
   
   - *Added test that validates that Java throws an AvroRuntimeException on invalid binary data*
   
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (**yes** / no)
   - If yes, how is the feature documented? (not applicable / docs / **JavaDocs** / not documented)
   


-- 
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: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2114512046

   > Regarding you point where we can extend the `@AvroTypeName` annotation functionality to also override the name of the record, in my opinion there is already `@AvroName` annotation that specially does this.
   
   Actually, `@AvroName` if for fields only; not types. In the original discussion we came to the conclusion that adding a `namespace` field to that annotation would lead to confusion. On the other hand, using a separate `@AvroNamespace` annotation would be a start into the annotation problems  ([as written about quite a bit](https://duckduckgo.com/?q=java+annotations+mistake+problem)).
   
   With this, I think we should make a choice:
   
   1. Use `@AvroName` for field names, and `@AvroTypeName` for the full type names (i.e., including namespace)
   2. Adjust `@AvroName` for fields and types, and `@AvroNamespace` for type namespaces
   3. Use `@AvroName` only, but add a special syntax like `my.namespace._` to override the namespace only
   
   The advantage of option 2 is that this sidesteps this entire discussion, but it also means there are 2 annotations to adjust the full type name. The latter is what will stay with us forever.
   
   The advantage of option 3 is that we don't have two annotations on full type names, but I'm not happy with the extra syntax.


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2113007382

   > Sorry for barging into the discussion uninvited, but I am following this PR, since I like the idea of such an annotation. If I understood the comments on the original issue correctly, then the annotation should not only allow to specify the namespace, but also the name of a record/enum - hence the name `@AvroTypeName` for the annotation instead of `@AvroNamespace`. The following suggestions are from Oscar's comment on the issue, I just adjusted the name of the annotation:
   > 
   > 1. `@AvroTypeName("simpleOrFullName")` for a named type would set its name, optionally with namespace
   > 2. `@AvroTypeName(value="simpleName", namespace="my.space")` for a named type would set its name and namespace
   > 3. `@AvroTypeName(namespace="my.space")` for a named type would set only the namespace and let the name be inferred
   > 
   > I guess this is what @opwvhk is referring to.
   > 
   > On a related note: I really like the idea of this kind of annotation, because namespace & name of a record schema can be independent from package & name of the corresponding class. However, it does not end there: `ReflectData` and `ReflectDatumReader` are not able to deserialize data when the schema of a record/enum has a changed name or namespace. Currently, it assumes that the class can be inferred from namespace & name of a record, see `SpecificData.getClass(Schema)`, which is used by `SpecificData.newRecord(Object, Schema)` (and `ReflectData` derives from `SpecificData`). I stumbled upon this issue this week when trying to implement an `@AvroNamespace` annotation myself.
   > 
   > What solved this issue for me was to add a property to the generated schema (`SpecificData.CLASS_PROP`, which is already used to specify classes for Avro arrays) and override `ReflectData.getClass(Schema)` to read this property (and cache the loaded class, just like `ReflectData.getClassProp(Schema,String)`).
   > 
   > I would appreciate an official solution, so I can throw away my code which may break with new versions of the Avro library.
   
   @ex-ratt Thanks for your suggestion and your comment was really helpful for me to understand.
   
   Regarding you point where we can extend the `@AvroTypeName` annotation functionality to also override the name of the record, in my opinion there is already `@AvroName` annotation that specially does this. Letting the users override the record name using both `@AvroTypeName` and `@AvroName` might be confusing and may also cause conflicts. Take the example below
   
   `
   @AvroTypeName(value = "OverridenAddressValue1")
   class Address {
   string address;
   }
   `
   
   `
   class Person {
   @AvroName(value = "OverridenAddressValue2")
   string address;
   }
   `
   
   When the Person schema is generated, what should the name of the `Address` record be, OverridenAddressValue1 or OverridenAddressValue2?
   
   Hence I would rather have @AvroTypeName only applied at class  level to only override the namespace. Having thought about its better to rename @AvroTypeName to @AvroNamespace. @opwvhk  what are your thoughts on this?
   
   Regarding your second point about deserialization failing when using the annotation, that a good point, I l write a testcase to confirm and fix if necessary.
   


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "ex-ratt (via GitHub)" <gi...@apache.org>.
ex-ratt commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1667628999


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -802,6 +800,21 @@ private String simpleName(Class<?> c) {
     return simpleName;
   }
 
+  /*
+   * Function checks if there is @AvroTypeName annotation on the class. If present
+   * then returns the value of the annotation else returns the package of the
+   * class
+   */
+  private String getNamespace(Class<?> c) {
+    AvroTypeName avroTypeName = c.getAnnotation(AvroTypeName.class);
+    if (avroTypeName != null) {
+      return avroTypeName.value();
+    }
+    if (c.getEnclosingClass() != null) // nested class
+      return c.getEnclosingClass().getName().replace('$', '.');

Review Comment:
   I'm not a reviewer, but have a few questions.
   
   1. Classes can be nested further, e.g. a class within a class within a top-level class. Would this method need recursion? Or should only one level of nesting be supported?
   2. Without the namespace annotation, the namespace of a top-level class is its package, and the namespace of a nested class is package plus name of its enclosing class. In case the enclosing class has the namespace annotation, should its name also be added to the nested namespace in the same way?



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1588318900


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -802,6 +800,21 @@ private String simpleName(Class<?> c) {
     return simpleName;
   }
 
+  /*
+   * Function checks if there is @AvroTypeName annotation on the class. If present
+   * then returns the value of the annotation else returns the package of the
+   * class
+   */
+  private String getNamespace(Class<?> c) {
+    AvroTypeName avroTypeName = c.getAnnotation(AvroTypeName.class);
+    if (avroTypeName != null) {
+      return avroTypeName.value();
+    }
+    if (c.getEnclosingClass() != null) // nested class
+      return c.getEnclosingClass().getName().replace('$', '.');

Review Comment:
   @opwvhk Thats a great point! I was assuming that we should only be overriding the namespace when the class has the AvroTypeName annotation. 
   
   Just to clarify if the class structure is as below:
   
   ```
   @AvroTypeName("org.apache.avro.reflect.OverrideNamespace")
     private static class NamespaceTest {
       private static class EnclosingNamespaceTest {
       }
     }
   ```
   When the schema definition is generated for EnclosingNamespaceTest.class, the namespace should be org.apache.avro.reflect.OverrideNamespace ?



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1588320384


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -706,9 +706,7 @@ protected Schema createSchema(Type type, Map<String, Schema> names) {
         AvroDoc annotatedDoc = c.getAnnotation(AvroDoc.class); // Docstring
         String doc = (annotatedDoc != null) ? annotatedDoc.value() : null;
         String name = c.getSimpleName();
-        String space = c.getPackage() == null ? "" : c.getPackage().getName();
-        if (c.getEnclosingClass() != null) // nested class
-          space = c.getEnclosingClass().getName().replace('$', '.');
+        String space = getNamespace(c);

Review Comment:
   I am not clear on this. Could you please explain this?
   
   Do you mean to rename the function getNamespace to getTypeName ?



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2116024471

   > 1. Use `@AvroName` for field names, and `@AvroTypeName` for the full type names (i.e., including namespace)
   > 2. Adjust `@AvroName` for fields and types, and `@AvroNamespace` for type namespaces
   > 3. Use `@AvroName` only, but add a special syntax like `my.namespace._` to override the namespace only
   > 
   > The advantage of option 2 is that this sidesteps this entire discussion, but it also means there are 2 annotations to adjust the full type name. The latter is what will stay with us forever.
   > 
   > The advantage of option 3 is that we don't have two annotations on full type names, but I'm not happy with the extra syntax.
   
   I prefer the option 2 where `@AvroNamespace` would let you only override the namespace only and can only be applied at class level. And use the existing `@AvroName ` annotation let you override the name only (no changes to be done for this as this is an existing functionality). 
   
   I dont think option 3 works too well because `@AvroName` annotation can be applied on primitive types as well today and primitive types dont have a namespace attribute on their own.
   
   If we agree that option 2 is the best course of action to implement this feature, then the current code anyways does that today with the only difference being the name of the annotation is `@AvroTypeName` instead of `@AvroNamespace`.
   
   @opwvhk if thats fine I l just update the name to `@AvroNamespace` and the code in its current state does what is expected.
   
   


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2131403080

   Hi @opwvhk. 
   
   Kind reminder. Awaiting your response so I can make the changes required


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1667657527


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -802,6 +800,21 @@ private String simpleName(Class<?> c) {
     return simpleName;
   }
 
+  /*
+   * Function checks if there is @AvroTypeName annotation on the class. If present
+   * then returns the value of the annotation else returns the package of the
+   * class
+   */
+  private String getNamespace(Class<?> c) {
+    AvroTypeName avroTypeName = c.getAnnotation(AvroTypeName.class);
+    if (avroTypeName != null) {
+      return avroTypeName.value();
+    }
+    if (c.getEnclosingClass() != null) // nested class
+      return c.getEnclosingClass().getName().replace('$', '.');

Review Comment:
   Please find my comments below
   
   1. Classes can be nested further, e.g. a class within a class within a top-level class. Would this method need recursion? Or should only one level of nesting be supported? - _To avoid any deviation I have kept the implementation same as the current implementation (without the namespace annotation) which only looks at the immediate top enclosing class for forming the namepsace._
   
   2. Without the namespace annotation, the namespace of a top-level class is its package, and the namespace of a nested class is package plus name of its enclosing class. In case the enclosing class has the namespace annotation, should its name also be added to the nested namespace in the same way? - _Thats a valid point. Happy to change if the reviewer suggests so_



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2091832610

   @opwvhk regarding your comment -> _I'm missing the option to override the type name, as the annotation name suggests._
   
   Based on my understanding the ask was the ability to allow overriding the namespace when schema is created using java reflection. The change does this at the moment by removing the default assignment of namepsace which was previously done as below
   
   ```
   String space = c.getPackage() == null ? "" : c.getPackage().getName();
    if (c.getEnclosingClass() != null) // nested class
             space = c.getEnclosingClass().getName().replace('$', '.');
   ```
   
   Kindly let me know if my understanding is incorrect


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2142550055

   Although I really prefer option 1, I can also live with option 2. Can you please make the changes for `@AvroName`, `@AvroNamespace` and the `java-class` property?


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "ex-ratt (via GitHub)" <gi...@apache.org>.
ex-ratt commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2112901411

   Sorry for barging into the discussion uninvited, but I am following this PR, since I like the idea of such an annotation. If I understood the comments on the original issue correctly, then the annotation should not only allow to specify the namespace, but also the name of a record/enum - hence the name `@AvroTypeName` for the annotation instead of `@AvroNamespace`. The following suggestions are from Oscar's comment on the issue, I just adjusted the name of the annotation:
   
   1. `@AvroTypeName("simpleOrFullName")` for a named type would set its name, optionally with namespace
   2. `@AvroTypeName(value="simpleName", namespace="my.space")` for a named type would set its name and namespace
   3. `@AvroTypeName(namespace="my.space")` for a named type would set only the namespace and let the name be inferred
   
   I guess this is what @opwvhk is referring to.
   
   On a related note: I really like the idea of this kind of annotation, because namespace & name of a record schema can be independent from package & name of the corresponding class. However, it does not end there: `ReflectData` and `ReflectDatumReader` are not able to deserialize data when the schema of a record/enum has a changed name or namespace. Currently, it assumes that the class can be inferred from namespace & name of a record, see `SpecificData.getClass(Schema)`, which is used by `SpecificData.newRecord(Object, Schema)` (and `ReflectData` derives from `SpecificData`). I stumbled upon this issue this week when trying to implement an `@AvroNamespace` annotation myself.
   
   What solved this issue for me was to add a property to the generated schema (`SpecificData.CLASS_PROP`, which is already used to specify classes for Avro arrays) and override `ReflectData.getClass(Schema)` to read this property (and cache the loaded class, just like `ReflectData.getClassProp(Schema,String)`).
   
   I would appreciate an official solution, so I can throw away my code which may break with new versions of the Avro library. 


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2423825602

   Awaiting review on this


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on PR #2887:
URL: https://github.com/apache/avro/pull/2887#issuecomment-2243403375

   @opwvhk awaiting you review


-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1587050639


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/AvroTypeName.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * 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 org.apache.avro.reflect;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Sets the avrotypename for this java type. When reading into this class, a
+ * reflectdatumreader looks for a schema field with the avrotypename.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.TYPE })
+public @interface AvroTypeName {
+  String value() default "";

Review Comment:
   Do we always want to specify the name here, or also allow to override the namespace only? 



##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -706,9 +706,7 @@ protected Schema createSchema(Type type, Map<String, Schema> names) {
         AvroDoc annotatedDoc = c.getAnnotation(AvroDoc.class); // Docstring
         String doc = (annotatedDoc != null) ? annotatedDoc.value() : null;
         String name = c.getSimpleName();
-        String space = c.getPackage() == null ? "" : c.getPackage().getName();
-        if (c.getEnclosingClass() != null) // nested class
-          space = c.getEnclosingClass().getName().replace('$', '.');
+        String space = getNamespace(c);

Review Comment:
   Would the annotation @AvroTypeName not specify the type name? 



##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -802,6 +800,21 @@ private String simpleName(Class<?> c) {
     return simpleName;
   }
 
+  /*
+   * Function checks if there is @AvroTypeName annotation on the class. If present
+   * then returns the value of the annotation else returns the package of the
+   * class
+   */
+  private String getNamespace(Class<?> c) {
+    AvroTypeName avroTypeName = c.getAnnotation(AvroTypeName.class);
+    if (avroTypeName != null) {
+      return avroTypeName.value();
+    }
+    if (c.getEnclosingClass() != null) // nested class
+      return c.getEnclosingClass().getName().replace('$', '.');

Review Comment:
   What if the enclosing class has an @AvroTypeName?



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3968 - [Java] Support for custom @AvroNamespace annotation [avro]

Posted by "henryf1991 (via GitHub)" <gi...@apache.org>.
henryf1991 commented on code in PR #2887:
URL: https://github.com/apache/avro/pull/2887#discussion_r1588322588


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/AvroTypeName.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * 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 org.apache.avro.reflect;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Sets the avrotypename for this java type. When reading into this class, a
+ * reflectdatumreader looks for a schema field with the avrotypename.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.TYPE })
+public @interface AvroTypeName {
+  String value() default "";

Review Comment:
   I feel the responsibility of this annotation should be solely to allow users to override the namespace only given the annotation can only be applied to classes.



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org