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

Registered type handler is not used for anonymous enums #2956

Closed
xrayw opened this issue Sep 16, 2023 · 4 comments · Fixed by #2957
Closed

Registered type handler is not used for anonymous enums #2956

xrayw opened this issue Sep 16, 2023 · 4 comments · Fixed by #2957
Assignees
Labels
Milestone

Comments

@xrayw
Copy link

xrayw commented Sep 16, 2023

Example:

@AllArgsConstructor
@Getter
public enum GcType {
    G1(1),
    ZGC(2) {
        @Override
        public String getName() {
            return "zgc";
        }
    },
    ;

    public String getName() {
        return this.name();
    }

    private int value;
}

Create a custom enum TypeHandler without MappedJdbcTypes for enum GcType named CustomEnumTypeHandler.

@MappedTypes(GcType.class)
public class CustomEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
   // xxx
}

now the typeHandlerRegistry.typeHandlerMap should be {xx, GcType={null=CustomEnumTypeHandler} , xx}

next step:

use GcType.ZGC as the object param to execute db operation.

class Param {
  private GcType gcType;
}

Mapper {
    @Select("select xxx from xxx where xxx=#{param.gcType}")
    xxx selectXXX(Param param);
}

then call it: selectXXX(new Param(GcType.ZGC))

Because the GcType.ZGC's type is GcType$1, not GcType, so the typeHandlerMap can't find the typeHandler for GcType$1,

TypeHandlerRegistry#getJdbcHandlerMap

// when type = GcType$1
private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
    Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = typeHandlerMap.get(type);
    if (jdbcHandlerMap != null) {
      return NULL_TYPE_HANDLER_MAP.equals(jdbcHandlerMap) ? null : jdbcHandlerMap;
    }
    if (type instanceof Class) {
      Class<?> clazz = (Class<?>) type;
      if (Enum.class.isAssignableFrom(clazz)) {
        Class<?> enumClass = clazz.isAnonymousClass() ? clazz.getSuperclass() : clazz;    // GcType$1.isAnonymousClass() is true, so enumClass=GcType
        jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(enumClass, enumClass);
        if (jdbcHandlerMap == null) {
          register(enumClass, getInstance(enumClass, defaultEnumTypeHandler));    // here will replace the previous enumClass's[jdbcType=null]  typehandler
          return typeHandlerMap.get(enumClass);
        }
      } else {
        jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
      }
    }
    typeHandlerMap.put(type, jdbcHandlerMap == null ? NULL_TYPE_HANDLER_MAP : jdbcHandlerMap);
    return jdbcHandlerMap;
  }

after the getJdbcHandlerMap method, the typeHandlerMap should be {xx, GcType={null=EnumTypeHandler} , xx}, the original {xx, GcType={null=CustomEnumTypeHandler} , xx} was replaced.

I think we' better keep it use the GcType's jdbcType TypeHandler[CustomEnumTypeHandler] if the jdbcType's TypeHandler exists , instead of replace it..

the original pr change about getJdbcHandlerMap anonymous enum #1490

MyBatis version

3.5.3

@harawata
Copy link
Member

Hello @xrayw ,

Could you provide a complete test case. or example project?
Thank you!

xrayw added a commit to xrayw/mybatis-issue-2956 that referenced this issue Sep 17, 2023
@xrayw
Copy link
Author

xrayw commented Sep 17, 2023

Hello, @harawata
I create a small unittest maven project at my repo.. please check

https://github.com/xrayw/mybatis-issue-2956

harawata added a commit to harawata/mybatis-3 that referenced this issue Sep 18, 2023
harawata added a commit to harawata/mybatis-3 that referenced this issue Sep 18, 2023
@harawata harawata changed the title TypeHandler issue with Anonymous Enum Registered type handler is not used for anonymous enums Sep 18, 2023
@harawata harawata self-assigned this Sep 18, 2023
@harawata harawata added this to the 3.5.14 milestone Sep 18, 2023
@harawata
Copy link
Member

Thank you for the test, @xrayw !

I could verify the issue and merged the fix ( #2957 ).
It would be great if you could verify the fix using the latest 3.5.14-SNAPSHOT [1].

[1] https://github.com/mybatis/mybatis-3/wiki/Maven

@xrayw
Copy link
Author

xrayw commented Sep 18, 2023

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants