conjure-java icon indicating copy to clipboard operation
conjure-java copied to clipboard

Having an optional<any> generates an unchecked cast

Open gatesn opened this issue 5 years ago • 1 comments

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;
        }
		...
    }

gatesn avatar May 22 '19 08:05 gatesn

Possibly relevant PR: https://github.com/palantir/conjure-java/pull/9

iamdanfox avatar May 22 '19 14:05 iamdanfox