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

incomplete Object information when serialize object with Gson #2485

Open
EvanAatrox opened this issue Jul 24, 2024 · 3 comments
Open

incomplete Object information when serialize object with Gson #2485

EvanAatrox opened this issue Jul 24, 2024 · 3 comments
Labels
bug Unexpected or incorrect behavior help wanted Issues we need help with tackling

Comments

@EvanAatrox
Copy link

    @PostMapping("/message/send?access_token={access_token}")
    MessageResponseDTO send(@RequestParam("access_token") String accessToken, @RequestBody AbsMessage message);

Such as the code, I need to call it,the interface has different performance depends on the Object passed.
However AbsMessage is a abstract class,when I call it need to pass an object which instance of AbsMessage.
But the reuest body doesn't conatin any field from the runtime sub class whic I passed. All fields are from the abstract class.
I find the cause at last.

  GsonEncoder#encode(Object object, Type bodyType, RequestTemplate template)
  @Override
  public void encode(Object object, Type bodyType, RequestTemplate template) {
    template.body(gson.toJson(object, bodyType));
  }

It serializes object with the type which declared in the function,not runtime type of the passed object.
If gson.toJson(object),It appears normal.

@EvanAatrox
Copy link
Author

one two three

It shows the bug above the photos.

@kdavisk6
Copy link
Member

If possible, please provide a minimal reproducible example to help diagnose the issue.

@kdavisk6 kdavisk6 added bug Unexpected or incorrect behavior waiting for feedback Issues waiting for a response from either to the author or other maintainers labels Sep 11, 2024
@EvanAatrox
Copy link
Author

EvanAatrox commented Sep 20, 2024

    @Data
    static abstract class Message {
        String toUser;

        public Message(String toUser) {
            this.toUser = toUser;
        }
    }

    @Getter
    @Setter
    @EqualsAndHashCode(callSuper = true)
    public static class SimpleTextMessage extends Message {
        String content;
        Integer type;

        public SimpleTextMessage(String toUser, String content, Integer type) {
            super(toUser);
            this.content = content;
            this.type = type;
        }
    }

    @Test
    void gsonTest() {
        Gson gson = new GsonBuilder().create();
        SimpleTextMessage message = new SimpleTextMessage("测试用户", "测试消息", Integer.valueOf(1));
        var res = gson.toJson(message, Message.class);
        // the final result only include toUser prop,bus loss content type props.
        // result: {"toUser":"测试用户"}
        System.out.println(res);
     }

Here are two classes,just like the lib feign-gson,it serializes object with the type declared,but not runtime real type.
This leads to a situation the json string not contains the props from the runtime real subclasses.
Thanks to your reply.

                                                        **Wish you happy every day, working smoothly!**

@kdavisk6 kdavisk6 added help wanted Issues we need help with tackling and removed waiting for feedback Issues waiting for a response from either to the author or other maintainers labels Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected or incorrect behavior help wanted Issues we need help with tackling
Projects
None yet
Development

No branches or pull requests

2 participants