'Class mapping using mapstruct that has an attribute as enum

I have a class called BalanceTarget

 public class BalanceTarget   {
  @JsonProperty("amount")
  private String amount;

  public enum CreditDebitIndicatorEnum {
    CREDIT("Credit"),
    
    DEBIT("Debit");

    private String value;

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

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

    @Override
    public String toString() {
      return String.valueOf(value);
    }

    @JsonCreator
    public static CreditDebitIndicatorEnum fromValue(String value) {
      for (CreditDebitIndicatorEnum b : CreditDebitIndicatorEnum.values()) {
        if (b.value.equals(value)) {
          return b;
        }
      }
      throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
  }

  @JsonProperty("creditDebitIndicator")
  private CreditDebitIndicatorEnum creditDebitIndicator;

  //getter and setters
 
}

Another class BalanceSource

public class BalanceSource   {
      @JsonProperty("amount")
      private String amount;
    
      public enum CreditDebitIndicatorEnum {
        C("C"),
        
        D("D");
    
        private String value;
    
        CreditDebitIndicatorEnum(String value) {
          this.value = value;
        }
    
        @JsonValue
        public String getValue() {
          return value;
        }
    
        @Override
        public String toString() {
          return String.valueOf(value);
        }
    
        @JsonCreator
        public static CreditDebitIndicatorEnum fromValue(String value) {
          for (CreditDebitIndicatorEnum b : CreditDebitIndicatorEnum.values()) {
            if (b.value.equals(value)) {
              return b;
            }
          }
          throw new IllegalArgumentException("Unexpected value '" + value + "'");
        }
      }
    
      @JsonProperty("creditDebitIndicator")
      private CreditDebitIndicatorEnum creditDebitIndicator;

     //getter and setters
    }

I have a mapper for Balance

@Mapper(componentModel = "spring", uses = CreditDebitIndicatorMapper.class)
public interface BalanceMapper {

    BalanceTarget convert(BalanceSource a);
}

I have another mapper for CreditDebitIndicator

    @Mapper(componentModel = "spring")
    public interface CreditDebitIndicatorMapper {
    
        @ValueMapping(source = "C", target = "CREDIT")
        @ValueMapping(source = "D", target = "DEBIT")
        BalanceTarget.CreditDebitIndicatorEnum convert(BalanceSource.CreditDebitIndicatorEnum a);
}

On building the code it gives out this error

The following constants from the property "BalanceSource.CreditDebitIndicatorEnum balances[].creditDebitIndicator" enum have no corresponding constant in the "BalanceTarget.CreditDebitIndicatorEnum balances[].creditDebitIndicator" enum and must be be mapped via adding additional mappings: C, D.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source