-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-3968 - [Java] Support for custom @AvroNamespace annotation #2887
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 AvroNamespace { | ||
String value() default ""; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -531,6 +531,11 @@ public Class getClass(Schema schema) { | |
return Short.TYPE; | ||
if (Character.class.getName().equals(intClass)) | ||
return Character.TYPE; | ||
case RECORD: | ||
case ENUM: | ||
Class className = getClassProp(schema, CLASS_PROP); | ||
if (className != null) | ||
return className; | ||
default: | ||
return super.getClass(schema); | ||
} | ||
|
@@ -706,9 +711,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); | ||
Union union = c.getAnnotation(Union.class); | ||
if (union != null) { // union annotated | ||
return getAnnotatedUnion(union, names); | ||
|
@@ -722,17 +725,20 @@ protected Schema createSchema(Type type, Map<String, Schema> names) { | |
for (Enum constant : constants) | ||
symbols.add(constant.name()); | ||
schema = Schema.createEnum(name, doc, space, symbols); | ||
schema.addProp(CLASS_PROP, c.getName()); | ||
consumeAvroAliasAnnotation(c, schema); | ||
} else if (GenericFixed.class.isAssignableFrom(c)) { // fixed | ||
int size = c.getAnnotation(FixedSize.class).value(); | ||
schema = Schema.createFixed(name, doc, space, size); | ||
schema.addProp(CLASS_PROP, c.getName()); | ||
consumeAvroAliasAnnotation(c, schema); | ||
} else if (IndexedRecord.class.isAssignableFrom(c)) { // specific | ||
return super.createSchema(type, names); | ||
} else { // record | ||
List<Schema.Field> fields = new ArrayList<>(); | ||
boolean error = Throwable.class.isAssignableFrom(c); | ||
schema = Schema.createRecord(name, doc, space, error); | ||
schema.addProp(CLASS_PROP, c.getName()); | ||
consumeAvroAliasAnnotation(c, schema); | ||
names.put(c.getName(), schema); | ||
for (Field field : getCachedFields(c)) | ||
|
@@ -802,6 +808,27 @@ 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) { | ||
AvroNamespace avroNamespace = c.getAnnotation(AvroNamespace.class); | ||
if (avroNamespace != null) { | ||
return avroNamespace.value(); | ||
} | ||
if (c.getEnclosingClass() != null) { // nested class | ||
AvroNamespace enclosingClassAvroNamespace = c.getEnclosingClass().getAnnotation(AvroNamespace.class); | ||
if (enclosingClassAvroNamespace != null) { | ||
return enclosingClassAvroNamespace.value(); | ||
} | ||
return c.getEnclosingClass().getName().replace('$', '.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the enclosing class has an @AvroTypeName? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @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:
When the schema definition is generated for EnclosingNamespaceTest.class, the namespace should be org.apache.avro.reflect.OverrideNamespace ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a reviewer, but have a few questions.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please find my comments below
|
||
} | ||
|
||
return c.getPackage() == null ? "" : c.getPackage().getName(); | ||
} | ||
|
||
private static final Schema THROWABLE_MESSAGE = makeNullable(Schema.create(Schema.Type.STRING)); | ||
|
||
// if array element type is a class with a union annotation, note it | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the annotation @AvroTypeName not specify the type name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not clear on this. Could you please explain this?
Do you mean to rename the function getNamespace to getTypeName ?