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

NullableAnnotations other than CheckerFramework's @Nullable #1057

Open
BudWhiteStudying opened this issue Apr 9, 2024 · 1 comment
Open

Comments

@BudWhiteStudying
Copy link

BudWhiteStudying commented Apr 9, 2024

This is similar to #889, but I couldn't find my answer there.

I am trying to generate a TS file from a bunch of Java classes through the typescript-generator-maven-plugin.

The documentation states that I need to:

  1. tell the plugin which annotation(s) represent nullable fields, through the <nullableAnnotations> property
  2. select how to represent nullability, through the <nullabilityDefinition> property

for example:

<nullableAnnotations>
    <nullableAnnotation>my.own.annotation.Nullable</nullableAnnotation>
</nullableAnnotations>
<nullabilityDefinition>
    nullAndUndefinedInlineUnion
</nullabilityDefinition>
  1. the documentation also specifies that the annotation must have target set to TYPE_PARAMETER or TYPE_USE, and mentions org.checkerframework.checker.nullness.qual.Nullable as an example of a suitable annotation for this purpose.

Indeed, if I use ChecherFramework's @Nullable annotation, everything works fine.

export interface MyInterface {
    id?: string;
    booleanOrNull?: boolean | null;
}

This is ChecherFramework's @Nullable annotation, for reference:

package org.checkerframework.checker.nullness.qual;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.checkerframework.framework.qual.DefaultFor;
import org.checkerframework.framework.qual.LiteralKind;
import org.checkerframework.framework.qual.QualifierForLiterals;
import org.checkerframework.framework.qual.SubtypeOf;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@SubtypeOf({})
@QualifierForLiterals({LiteralKind.NULL})
@DefaultFor(
    types = {Void.class}
)
public @interface Nullable {
}

If instead I use my own annotation, copied one-to-one from ChecherFramework's @Nullable annotation removing CF's own annotations, the feature stops working. This is my @Nullable annotation:

package my.own.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
public @interface Nullable {
}

and this is the output:

export interface MyInterface {
    id?: string;
    booleanOrNull?: boolean;
}

....sooooo what is it that org.checkerframework.checker.nullness.qual.Nullable has, that my.own.annotation.Nullable is missing in order to work? The only difference seems to be the additional annotations that come from CF's own package, but then is ChecherFramework's @Nullable annotation the only annotation to be used for this feature?

Thanks, great tool anyway.

@runderworld
Copy link

@vojtechhabarta same for me; any idea why this might be the case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants