spring-projects / spring-projects/spring-hateoas

EntityModel enum map key not support

Open
#1,990 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

spring-hateoas 1.1.2

code like this

    public enum EnumKey {
        K1,
        K2
    }

    @GetMapping("/test")
    @ResponseBody
    public ResponseEntity<EntityModel<Map<EnumKey,String>>> test() {
        Map<EnumKey,String> map = new HashMap<>();
        map.put(EnumKey.K1,"1");
        map.put(EnumKey.K2,"2");

        return ok(EntityModel.of(map));
    }

error

Could not write JSON: ****Controller$EnumKey incompatible with java.lang.String; nested exception is com.fasterxml.jackson.databind.JsonMappingException: ****Controller$EnumKey incompatible with java.lang.String (through reference chain: org.springframework.hateoas.EntityModel["[anySetter]"]->java.util.HashMap["K2"])

i think hateoas use @JsonAnyGetter and com.fasterxml.jackson.databind.ser.AnyGetterWriter use string key MapSerializer
https://github.com/spring-projects/spring-hateoas/blob/3b1559fc94127cb20d386b5b0a1ba582467f385b/src/main/java/org/springframework/hateoas/EntityModel.java#L135-L140

already use this added enum key serializer

still doesn't work, what is the correct way?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example against spring-hateoas 1.1.2, then inspect EntityModel.java around the @JsonAnyGetter and Jackson's AnyGetterWriter and MapSerializer behavior. Compare the linked Jackson enum-key serializer discussion; done means an EntityModel containing a Map<EnumKey,String> serializes successfully without the reported key-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.