-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
AutoValue.Builder doesn't handle OptionalLong
correctly.
#1198
Comments
Thanks, that does look like a bug. The fix is not completely straightforward, since I think we want it to generate this: public Builder setLongField(@Nullable Long value) {
this.longField = (value == null) ? OptionalLong.empty() : OptionalLong.of(value);
return this;
} The current code structure doesn't make that easy. |
I'm not able to reproduce this problem. Could you give complete code that produces it? |
i misread the version in my project. i had 1.6.2, not 1.8.2. upgrading to 1.8.2 (which i thought i did in my testing, probably forgot to refresh intellij's maven stuff) gives me different problems. the primitive one still works but still has the same problem with jackson, but the object wrapper won't compile at all.
if i switch to a builder method that takes an @interface Nullable {}
interface Parent {
OptionalLong getValue();
}
@AutoValue
@JsonDeserialize(builder = AutoValue_PrimitiveBuilderField.Builder.class)
abstract class PrimitiveBuilderField implements Parent {
@AutoValue.Builder
abstract static class PrimitiveBuilder {
@JsonProperty("value")
abstract PrimitiveBuilder value(long value);
abstract PrimitiveBuilderField build();
}
}
@AutoValue
//@JsonDeserialize(builder = AutoValue_ObjectWrapperBuilderField.Builder.class)
abstract class ObjectWrapperBuilderField implements Parent {
// @AutoValue.Builder
abstract static class Builder {
@JsonProperty("value")
abstract Builder value(@Nullable Long value);
abstract ObjectWrapperBuilderField build();
}
}
@AutoValue
@JsonDeserialize(builder = AutoValue_OptionalFieldBuilderField.Builder.class)
abstract class OptionalFieldBuilderField implements Parent {
@AutoValue.Builder
abstract static class Builder {
@JsonProperty("value")
abstract Builder value(OptionalLong value);
abstract OptionalFieldBuilderField build();
}
}
public class Main {
public static void main(String[] args) throws Exception {
final ObjectMapper objectMapper = new ObjectMapper().registerModule(new Jdk8Module());
final PrimitiveBuilderField primitiveBuilderField = objectMapper.readValue("{\"value\": null}", PrimitiveBuilderField.class);
System.out.println(primitiveBuilderField); // output: PrimitiveBuilderField{value=OptionalLong[0]}
final OptionalFieldBuilderField objectWrapperBuilderField = objectMapper.readValue("{\"value\": null}", OptionalFieldBuilderField.class);
System.out.println(objectWrapperBuilderField); // output: OptionalFieldBuilderField{value=OptionalLong.empty}
}
} Here's the full maven project |
On the one hand, a It's fairly straightforward to achieve the API you want with overloading: @AutoValue
abstract class OptionalFieldBuilderField implements Parent {
abstract Builder value(OptionalLong value);
Builder value(@Nullable Long value) {
return value(value == null ? OptionalLong.empty() : OptionalLong.of(value));
}
abstract OptionalFieldBuilderField build();
} I don't know if that would do what you want with Jackson, though. |
yeah, that was basically how i ended up working around it already. it's not ideal, since you get sonar and IDE warnings about having an optional as a parameter, but it is a viable workaround. |
@eamonnmcmanus I am having an issue on Builder method, it generates the json with all the fields instead of the values we set on builder method. these are my dependencies
here is my data class
while accessing the builder method
it generates this
instead of this
Can you help me where i get wrong on this ? thanks in advance. |
@ratheeshcn I think this is an issue with auto-value-gson rather than AutoValue itself. It would make sense for auto-value-gson to omit null values from the serialized JSON string unless |
I'm going to close the original issue on the grounds that |
It seems to treat it like it's just a regular
Optional
, butOptionalLong
doesn't have the same methods.If I have a builder class with
the generated code works fine:
This caused a problem with jackson because when it tries to parse an object with the field set as
null
, jackson fills in a 0 for the value and it turns intoOptionalLong.of(0)
- which is not the same asOptional.empty()
. So I switched it to an object wrapper and marked it as@Nullable
:but now it generates invalid java:
OptionalLong
doesn't have afromNullable
static constructor. This is broken up through at least 1.8.2The text was updated successfully, but these errors were encountered: