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

Add @Nullable annotations for pegasus java getters and setters with mode #932

Merged
merged 3 commits into from
Sep 11, 2023

Conversation

macklee
Copy link
Contributor

@macklee macklee commented Sep 5, 2023

Background

This PR adds @Nullable annotation to the Java data template generator that generates getters and setters for pegasus data models. Specifically:

  1. For setters with mode, add @Nullable annotations. E.g. Foo.setBar(blah, SetMode mode) => Foo.setBar(@Nullable blah, SetMode mode) since Foo.setBar(blah, SetMode.REMOVE_IF_NULL) is null-safe.
  2. For getters with mode, add @Nullable annotation. E.g. Foo.getBar(GetMode mode) => @Nullable Foo.getBar(GetMode mode) since Foo.getBar(GetMode.IGNORE_NULL) is null-safe.

Context

PSM recently introduced NullAway (https://github.com/uber/NullAway) as a requirement for LMS MPs. However, the library caught several false positives on Rest.Li generated java code, specifically on the cases mentioned above due to missing annotations. Adding these annotations should reduce the false positives from the library.

Testing Done

Build in Java 8:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/
./gradlew clean build

Generated local release and tested in Java 11 consumer (following workaround in SI-31264 and SI-30640
./scripts/local-release -s

Example Getter with Mode (required field):


    /**
     * Getter for id
     * 
     * @see Post.Fields#id
     */
    @Nullable
    public com.linkedin.common.urn.Urn getId(GetMode mode) {
        switch (mode) {
            case STRICT:
                return getId();
            case DEFAULT:
            case NULL:
                if (_idField!= null) {
                    return _idField;
                } else {
                    Object __rawValue = super._map.get("id");
                    _idField = DataTemplateUtil.coerceCustomOutput(__rawValue, com.linkedin.common.urn.Urn.class);
                    return _idField;
                }
        }
        throw new IllegalStateException(("Unknown mode "+ mode));
    }

Example Setter with Mode (required field)

    /**
     * Setter for id
     * 
     * @see Post.Fields#id
     */
    public Post setId(
        @Nullable
        com.linkedin.common.urn.Urn value, SetMode mode) {
        switch (mode) {
            case DISALLOW_NULL:
                return setId(value);
            case REMOVE_OPTIONAL_IF_NULL:
                if (value == null) {
                    throw new IllegalArgumentException("Cannot remove mandatory field id of com.linkedin.contentapi.Post");
                } else {
                    CheckedUtil.putWithoutChecking(super._map, "id", DataTemplateUtil.coerceCustomInput(value, com.linkedin.common.urn.Urn.class));
                    _idField = value;
                }
                break;
            case REMOVE_IF_NULL:
                if (value == null) {
                    removeId();
                } else {
                    CheckedUtil.putWithoutChecking(super._map, "id", DataTemplateUtil.coerceCustomInput(value, com.linkedin.common.urn.Urn.class));
                    _idField = value;
                }
                break;
            case IGNORE_NULL:
                if (value!= null) {
                    CheckedUtil.putWithoutChecking(super._map, "id", DataTemplateUtil.coerceCustomInput(value, com.linkedin.common.urn.Urn.class));
                    _idField = value;
                }
                break;
        }
        return this;
    }

@zhihaodev
Copy link

Could you follow the previous PRs (for example: https://github.com/linkedin/rest.li/pull/927/files) to also update CHANGELOG.md and gradle.properties?

CHANGELOG.md Show resolved Hide resolved
@zhihaodev zhihaodev merged commit 1fce408 into linkedin:master Sep 11, 2023
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants