-
Notifications
You must be signed in to change notification settings - Fork 0
Regenerate with decimal_string enabled for v2 APIs #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,42 @@ | |
| import com.google.gson.FieldNamingPolicy; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| final class JsonEncoder { | ||
| /** | ||
| * Serializes BigDecimal as a JSON string (e.g., "25.5") rather than a JSON number (25.5). All | ||
| * BigDecimal fields in the Stripe API use format: decimal, and the V2 API expects them as strings | ||
| * on the wire. | ||
| */ | ||
| private static final TypeAdapter<BigDecimal> BIG_DECIMAL_STRING_ADAPTER = | ||
| new TypeAdapter<BigDecimal>() { | ||
| @Override | ||
| public void write(JsonWriter out, BigDecimal value) throws IOException { | ||
| if (value == null) { | ||
| out.nullValue(); | ||
| } else { | ||
| out.value(value.toPlainString()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal read(JsonReader in) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question — In If this was intentional, I'll note it and won't flag it as a bug. If not, I can flag the related usages as blockers. Reply here and I'll learn from your answer. |
||
| return new BigDecimal(in.nextString()); | ||
| } | ||
| }; | ||
|
|
||
| private static final Gson BODY_GSON = | ||
| new GsonBuilder() | ||
| .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | ||
| .serializeNulls() | ||
| .registerTypeAdapter(BigDecimal.class, BIG_DECIMAL_STRING_ADAPTER) | ||
| .create(); | ||
|
|
||
| public static HttpContent createHttpContent(Map<String, Object> params) throws IOException { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛑 Blocker · Bug · The
readmethod callsin.nextString()unconditionally, but Gson's JsonReader.nextString() only works when the current token is a JSON string. If the Stripe API returnspercent_ownershipas a JSON number (e.g.,25.5) instead of a string (e.g.,"25.5"), this will throw IllegalStateException because the current token is NUMBER, not STRING. The adapter assumes all BigDecimal values come as strings, but APIs can return them as either strings or numbers.