Skip to content
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-3641: Adds support for nullSafeAnnotations to java SpecificCompiler #2142

Merged
merged 13 commits into from
Oct 23, 2023

Conversation

peknu
Copy link
Contributor

@peknu peknu commented Mar 13, 2023

What is the purpose of the change

*This pull request adds support for JetBrains @Nullable and @NotNull annotations based on schema.isNullable(), fixing AVRO-3641.

Verifying this change

This change added tests and can be verified as follows:

Run TestSpecificCompilerTool.compileSchemaWithNullSafeAnnotationsFields() to fully verify the generated java code

Documentation

  • This pull request introduces a new feature for null safety and is documented using JavaDoc in SpecificCompiler class.
    Adding configuration option <createNullSafeAnnotations>true<createNullSafeAnnotations/> enables this feature.
    It is disabled by default to conform to the default behavior.

@github-actions github-actions bot added build Java Pull Requests for Java binding labels Mar 13, 2023
# Conflicts:
#	lang/java/pom.xml
#	lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
@artheus
Copy link

artheus commented Mar 13, 2023

👍🏻

@opwvhk
Copy link
Contributor

opwvhk commented Mar 14, 2023

Given that this package main use case are the command line tools / build plugins, I'm fine with adding the jetbrains dependency.

@phile314
Copy link

I was just now looking for this feature, great to see that this is already worked on :-)

As there are many nullable annotations around, would it make sense to make it configurable?
image

Comment on lines 171 to 174
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dependency actually used here?

Comment on lines 235 to 238
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see the dependency being used in the test code (which is perfect IMHO). Can we please reduce the scope to test?

@opwvhk
Copy link
Contributor

opwvhk commented Mar 16, 2023

As there are many nullable annotations around, would it make sense to make it configurable?

I think it does. Also, your question triggered me to take a closer look at the code & dependencies. It would actually be a good change, and relatively easy to do as well.

Copy link
Contributor

@opwvhk opwvhk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a few comments wrt use of dependencies.

I think a configurable approach, where the test configuration explicitly names the org.jetbrains.annotations.Nullable and org.jetbrains.annotations.NotNull annotations.

@karlll
Copy link

karlll commented Mar 25, 2023

👍

@peknu
Copy link
Contributor Author

peknu commented May 10, 2023

The JSR to develop standard annotations (such as @nonnull) for Java is dead since August 2006: https://jcp.org/en/jsr/detail?id=305

Maybe this PR can be merged so that the most commonly used @nullable and @NotNull annotations can be used. This would be especially useful for Kotlin based projects, since it would add compile time safety when using Avro.

@github-actions github-actions bot removed the build label May 10, 2023
@peknu peknu requested a review from opwvhk May 30, 2023 05:55
Copy link
Contributor

@opwvhk opwvhk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, this code looks good (nice, simple & clean).

As per the comments (also left by others), I think it would be better to prepare the template for using different annotation classes (i.e., remove the import statement). Actually implementing for other @Nullable/@NoptNull annotations is a lot easier then.

Copy link
Contributor

@opwvhk opwvhk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, this code looks good (nice, simple & clean).

As per the comments (also left by others), I think it would be better to prepare the template for using different annotation classes (i.e., remove the import statement). Actually implementing for other @Nullable/@NoptNull annotations is a lot easier then.

@peknu peknu requested a review from opwvhk June 2, 2023 15:08
@peknu
Copy link
Contributor Author

peknu commented Aug 16, 2023

Is there anything I can do to get this PR merged? It has been ready to merge for quite some time now?

/**
* The createNullSafeAnnotations parameters adds @Nullable and @NotNull
* annotations for fhe fields of the record. The default is to not include
* annotations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please mention here that the annotations are JetBrains annotations?

Then it'll automatically be documented by the plugin help. Ideally, it also lists the dependency coordinates / package-url (pkg:maven/org.jetbrains/[email protected]).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point in time, there are no other places on the website that document the maven plugin, or code generation options. It's only mentioned in passing on the "Getting started" page for Java.

@github-actions github-actions bot added the build label Sep 25, 2023
@peknu
Copy link
Contributor Author

peknu commented Oct 5, 2023

Any chance to get this merged?

@opwvhk opwvhk merged commit 2b19559 into apache:main Oct 23, 2023
13 checks passed
@m-kay
Copy link

m-kay commented Jan 18, 2024

@opwvhk Is there any planned version where this will be released? This would really help us a lot when working with Avro in Kotlin projects.

@opwvhk
Copy link
Contributor

opwvhk commented Jan 23, 2024

@m-kay It will be in the next release, version 1.12.0. The most authoritative source on it is the dev mailing list.

RanbirK pushed a commit to RanbirK/avro that referenced this pull request May 13, 2024
…ler (apache#2142)

* AVRO-3641: Adds support for nullSafeAnnotations to java SpecificCompiler

* AVRO-3641: Removes maven dependency to org.jetbrains:annotations since it is only used in the produced jar

* AVRO-3641: Removes the unused property for jetbrains-annotations version

* AVRO-3641: Removes empty lines from expected output file

* Revert "AVRO-3641: Removes empty lines from expected output file"

This reverts commit f99cf16.

* AVRO-3641: Use full class name for null safe annotations instead of imports

* AVRO-3641: Adds avro-maven-plugin config parameter for createNullSafeAnnotations

* AVRO-3641: Clarifies the source of the nullability annotations

* AVRO-3641: Minor javadoc formatting.

* AVRO-3641: Fixes spotless formatting

* AVRO-3641: Adds license exclude for NullSafeAnnotationsFieldsTest.java

---------

Co-authored-by: pknu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Java Pull Requests for Java binding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants