FasterXML / FasterXML/jackson-databind

@JsonAlias ignored in enum if @JsonValue is present

Open
#6,210 1 comment 0 reactions 0 assignees View on GitHub
to-evaluate
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### Search before asking

- [x] I searched in the [issues](https://github.com/FasterXML/jackson-databind/issues) and found nothing similar.

### Describe the bug

@JsonAlias is ignored when using @JsonValue, while it works if @JsonValue is not present.
This is against the documentation.

### Version Information

3.1.5

### Reproduction

Using this code passing the value "V" in a json raises this exception

tools.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `mypackage.enumeration.Versus` from String "V": not one of the values accepted for Enum class: [B, S]

```java
package mypackage.enumeration.Versus;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonValue;

public enum Versus {

  @JsonAlias({ "A", "B" }) BUY("B"),
  @JsonAlias({ "V", "S" }) SELL("S");

  private String value;

  Versus(String value) {
    this.value = value;
  }

@JsonValue
  public String getValue() {
    return value;
  }

}

```

### Expected behavior

Passing "V" or "S" it must return "S".

### Additional context

After some researches it seems that EnumResolver.constructUsingMethod() (called only if @JsonValue is present) doesn't fire findEnumAliases().

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at EnumResolver.constructUsingMethod(), which is used when @JsonValue is present, and compare its alias handling with findEnumAliases(). Use the supplied Versus enum reproduction to check deserialization of "V" and "S"; done means both values resolve to SELL without breaking the existing "B" and "S" behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.