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

Optional<Boolean> is not recognized as boolean field #3836

Closed
thnaeff opened this issue Mar 20, 2023 · 4 comments
Closed

Optional<Boolean> is not recognized as boolean field #3836

thnaeff opened this issue Mar 20, 2023 · 4 comments
Milestone

Comments

@thnaeff
Copy link

thnaeff commented Mar 20, 2023

A method like this

  public Optional<Boolean> isUsed() {
    return Optional.ofNullable(used);
  }

does not seem to get automatically recognized by Jackson as a field to include for serialization, meaning that it does not appear in the serialized JSON at all.

Changing it to this works since it is now a standard boolean return type

  public Boolean isUsed() {
    return used;
  }

What also works is annotating it. However, this then produces a field isUsed (it does not strip the is as it generally happens for boolean properties). Annotating a Optional<String> get... method with @JsonProperty works as expected with stripping the get.

  @JsonProperty
  public Optional<Boolean> isUsed() {
    return Optional.ofNullable(used);
  }

So to get the "standard" boolean field behavior it needs

  @JsonProperty("used")
  public Optional<Boolean> isUsed() {
    return Optional.ofNullable(used);
  }

Any get methods are recognized by default, so I would think this is an issue specific to boolean properties.

I should note that this has been run with jackson-datatype-jdk8/-annotations/-core v2.13.3 as well as v2.15.0-rc1, both with same behavior

@cowtowncoder
Copy link
Member

Hmmh. Yes, I can see this occurring. And I can see why is non-intuitive.

The challenge is that of core databind recognizing structure type that it does not directly support. This might be possible to recognize given Optional is a ReferenceType, although it's hard to say before trying to make it work.

@cowtowncoder
Copy link
Member

Although this is correctly filed here wrt initial symptom, the fix (if any) needs to go in jackson-databind so I will transfer the issue there. It can be verified with AtomicReference<Boolean> which behaves similar to Optional<Boolean> and is directly supported by jackson-databind itself.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-modules-java8 Mar 20, 2023
@cowtowncoder
Copy link
Member

Ok so the location for the fix is method findNameForIsGetter() in DefaultAccessorNamingStrategy:

        if (_isGetterPrefix != null) {
            final Class<?> rt = am.getRawType();
            if (_isGettersNonBoolean || rt == Boolean.class || rt == Boolean.TYPE) {
                if (name.startsWith(_isGetterPrefix)) {
                    return _stdBeanNaming
                            ? stdManglePropertyName(name, 2)
                            : legacyManglePropertyName(name, 2);
                }
            }
        }

where we need to change things a bit. But it seems pretty much doable.

@cowtowncoder cowtowncoder changed the title Optional<Boolean> is not recognized as boolean field Optional<Boolean> is not recognized as boolean field Mar 29, 2023
@cowtowncoder cowtowncoder added this to the 2.15.0 milestone Mar 29, 2023
@cowtowncoder
Copy link
Member

Fixed, tested with AtomicBoolean and AtomicReference<Boolean> -- should work for Optional<Boolean> too.

Actually will verify in master.

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