palantir / palantir/conjure-java

Having an optional<any> generates an unchecked cast

Open
#390 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
39
Forks
49
Avg merge
8h 22m
Merged PRs (30d)
32

Description

What happened?

Defining an object like this:

MyObject:
  fields:
    myfield: optional<any>

Results in the following Java code:

    @Generated("com.palantir.conjure.java.types.BeanBuilderGenerator")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static final class Builder {
        ...
        @JsonSetter("detail")
        public Builder detail(Optional<?> detail) {
            this.detail =
                    (Optional<Object>) Preconditions.checkNotNull(detail, "detail cannot be null");
            return this;
        }

        public Builder detail(Object detail) {
            this.detail = Optional.of(Preconditions.checkNotNull(detail, "detail cannot be null"));
            return this;
        }
    }

Where Optional<?> is cast into an Optional<Object>.

What did you want to happen?

    public static final class Builder {
        ...
        @JsonSetter("detail")
        public Builder detail(Optional<Object> detail) {
            this.detail = Preconditions.checkNotNull(detail, "detail cannot be null");
            return this;
        }
		...
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the BeanBuilderGenerator named in the generated annotation and reproduce the optional example from the issue. Check the generated builder method for Optional<?> and verify that the result uses Optional without an unchecked cast; the Java output shown in the issue defines the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.