Skip to content

Encode Gson request bodies using the runtime class - #3535

Open
tanvir-ux wants to merge 1 commit into
OpenFeign:masterfrom
tanvir-ux:fix/gson-encoder-runtime-type
Open

Encode Gson request bodies using the runtime class#3535
tanvir-ux wants to merge 1 commit into
OpenFeign:masterfrom
tanvir-ux:fix/gson-encoder-runtime-type

Conversation

@tanvir-ux

Copy link
Copy Markdown

Fixes #2485.

Problem
When a Feign method declares an abstract (or superclass) body type, GsonEncoder serializes only the fields on that declared type. Fields on the concrete subclass never appear in the JSON.

Root cause
GsonEncoder.encode called gson.toJson(object, bodyType). Gson treats the second argument as the schema, so a SimpleTextMessage passed as Message is written as {"toUser":"..."}.

Fix
If bodyType is a raw Class, serialize with object.getClass(). Parameterized types (TypeToken / generics) still use the declared type so type arguments are not erased. Jackson and the other encoders are unchanged.

Tests
GsonCodecTest.encodesSubclassFieldsWhenBodyTypeIsAbstract encodes a subclass as the abstract type and asserts the JSON includes the subclass fields. Existing TypeToken encoding tests still pass.

./mvnw test -pl gson -am (gson: 11 tests, 0 failures).

GsonEncoder passed the declared parameter type into gson.toJson, so
subclass fields on an abstract request body type were dropped.

When bodyType is a raw Class, encode with object.getClass().
Parameterized types keep the declared TypeToken so generic arguments
are not erased.

Fixes OpenFeign#2485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

incomplete Object information when serialize object with Gson

1 participant