Skip to content

Document how to add JSpecify nullability annotations to existing public API #26792

In review

Description

@totally-not-ai

Problem

guidelines/design.md ("Nullability discipline") and CONVENTIONS.md describe how to annotate new code with JSpecify, but say nothing about adding @NullMarked/@Nullable to existing public API. Retrofitting annotations onto a class that already ships has compatibility traps that are not obvious from the current rules. In the review of #26320 (Element), each of these came up as a separate finding, several only after an earlier suggestion turned out to break something too:

  • Kotlin source compatibility. Kotlin treats JSpecify annotations as strict, so marking existing getters @Nullable (getAttribute, getParent, getProperty, …) breaks Kotlin callers such as el.parent.tag. Removing the annotation later breaks the adapted callers the other way. The change is source-breaking in a minor release, which affects the risk assessment and needs a release note.
  • Generic wildcards vs. overriding. Changing a parameter of a non-final method from SerializableFunction<UI, Registration> to SerializableFunction<UI, ? extends @Nullable Registration> makes existing overrides fail with "name clash: … have the same erasure, yet neither overrides the other". Putting the annotation on the type argument, without a wildcard, avoids that.
  • Overload delegation. Splitting getProperty(String) from getProperty(String, String) to give them different nullness changed which overload a subclass override affects. getProperty(name, null) is also a common idiom, so a non-null defaultValue contract does not hold.
  • @NullUnmarked scope. @NullUnmarked on a method turns off checking for every parameter and the return type, not just the one generic argument that was problematic. Annotating the type argument (Signal<@Nullable String>) keeps the rest checked.
  • Consistency across sibling methods. addJsInitializer accepted a @Nullable varargs array while executeJs/callJsFunction still threw an NPE for it.
  • package-info.java. The package (com.vaadin.flow.dom) had no package-info.java, so there was nowhere for package-level @NullMarked.

Suggested fix

Add a short "Annotating existing API" subsection under "Nullability discipline" in guidelines/design.md (and a pointer in CONVENTIONS.md), covering:

  1. Adding @Nullable to the return value or parameter of an existing public method is a Kotlin source break. Call it out as one in the PR risk section and in the release notes.
  2. Never introduce a wildcard (? extends @Nullable T) into the parameter type of an overridable method. Annotate the type argument instead (Function<UI, @Nullable Registration>), or keep the signature and document that null is allowed.
  3. Keep existing overload delegation as it is. Don't split overloads to give them different nullness.
  4. Prefer annotating the specific type argument over @NullUnmarked. If @NullUnmarked is unavoidable, reference one tracking issue instead of repeating the same comment on every method.
  5. Keep nullness of the same parameter consistent across sibling methods (e.g. all executeJs-style varargs either accept or reject a null array).
  6. Add package-info.java with @NullMarked when annotating the first class in a package.

Activity

  1. totally-not-ai commented on Nov 2, 2026

    @totally-not-ai
    ContributorAuthor

    Thanks for the write-up. This is a valid documentation gap: the "Nullability discipline" section of guidelines/design.md and the Nullability rules in CONVENTIONS.md only cover annotating new code, and say nothing about the compatibility traps of retrofitting JSpecify annotations onto API that already ships.

    All six points hold up technically:

    • Kotlin reads JSpecify annotations as strict, so adding @Nullable to an existing return/parameter type is a source break for Kotlin callers even though Java callers are unaffected.
    • Changing F<UI, R> to F<UI, ? extends @Nullable R> on an overridable method keeps the erasure but changes the signature, so existing overrides hit "same erasure, yet neither overrides the other"; F<UI, @Nullable R> avoids it.
    • Splitting delegating overloads changes which subclass override takes effect.
    • @NullUnmarked on a method disables checking for all parameters and the return type.
    • Sibling methods taking the same parameter should agree on its nullness.
    • com.vaadin.flow.dom has no package-info.java yet, so there is nowhere for package-level @NullMarked.

    Since this is docs-only and the content is clear-cut, I've opened a draft PR: #26793. Following the repo's split between rules and reasoning, it adds the checkable rules to the Nullability section of CONVENTIONS.md and a new "Annotating existing API" subsection under "Nullability discipline" in guidelines/design.md that explains each rule with the failure it prevents.

  2. added theissue type on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions