Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51a7619

Browse files
committed
bson4gson adapter throw illegal state exeception instead of reading incorrect type (eg boolean vs readInt32)
1 parent70f4d03 commit51a7619

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

‎mongo/src/org/immutables/mongo/bson4gson/BsonReader.java‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ public String nextString() throws IOException {
201201
* @see com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT
202202
*/
203203
privateStringscalarToString() {
204-
switch (delegate.getCurrentBsonType()) {
204+
finalBsonTypetype =delegate.getCurrentBsonType();
205+
switch (type) {
205206
caseSTRING:
206207
returndelegate.readString();
207208
caseSYMBOL:
@@ -219,8 +220,7 @@ private String scalarToString() {
219220
caseOBJECT_ID:
220221
returndelegate.readObjectId().toHexString();
221222
default:
222-
thrownewIllegalStateException(
223-
"Unknown scalar type to be converted to string: " +delegate.getCurrentBsonType());
223+
thrownewIllegalStateException("Unknown scalar type to be converted to string: " +type);
224224
}
225225
}
226226

@@ -236,7 +236,10 @@ public void nextNull() throws IOException {
236236

237237
@Override
238238
publicdoublenextDouble()throwsIOException {
239-
switch (delegate.getCurrentBsonType()) {
239+
finalBsonTypetype =delegate.getCurrentBsonType();
240+
switch (type) {
241+
caseDOUBLE:
242+
returndelegate.readDouble();
240243
caseINT32:
241244
returndelegate.readInt32();
242245
caseINT64:
@@ -248,43 +251,49 @@ public double nextDouble() throws IOException {
248251
caseTIMESTAMP:
249252
returndelegate.readTimestamp().getValue();
250253
default:
251-
returndelegate.readDouble();
254+
thrownewIllegalStateException(String.format("Expected numeric bson type (double) but got %s (as json:%s)",type,toGsonToken(type)));
252255
}
253256
}
254257

255258
@Override
256259
publiclongnextLong()throwsIOException {
257-
switch (delegate.getCurrentBsonType()) {
258-
caseDOUBLE:
259-
return (long)delegate.readDouble();
260+
BsonTypetype =delegate.getCurrentBsonType();
261+
switch (type) {
262+
caseINT64:
263+
returndelegate.readInt64();
260264
caseINT32:
261265
returndelegate.readInt32();
266+
caseDOUBLE:
267+
return (long)delegate.readDouble();
262268
caseDECIMAL128:
263269
returndelegate.readDecimal128().bigDecimalValue().longValueExact();
264270
caseDATE_TIME:
265271
returndelegate.readDateTime();
266272
caseTIMESTAMP:
267273
returndelegate.readTimestamp().getValue();
268274
default:
269-
returndelegate.readInt64();
275+
thrownewIllegalStateException(String.format("Expected numeric bson type (long) but got %s (as json:%s)",type,toGsonToken(type)));
270276
}
271277
}
272278

273279
@Override
274280
publicintnextInt()throwsIOException {
275-
switch (delegate.getCurrentBsonType()) {
276-
caseDOUBLE:
277-
return (int)delegate.readDouble();
281+
finalBsonTypetype =delegate.getCurrentBsonType();
282+
switch (type) {
283+
caseINT32:
284+
returndelegate.readInt32();
278285
caseINT64:
279286
return (int)delegate.readInt64();
287+
caseDOUBLE:
288+
return (int)delegate.readDouble();
280289
caseDECIMAL128:
281290
returndelegate.readDecimal128().bigDecimalValue().intValueExact();
282291
caseDATE_TIME:
283292
return (int)delegate.readDateTime();
284293
caseTIMESTAMP:
285294
return (int)delegate.readTimestamp().getValue();
286295
default:
287-
returndelegate.readInt32();
296+
thrownewIllegalStateException(String.format("Expected numeric bson type (int) but got %s (as json:%s)",type,toGsonToken(type)));
288297
}
289298
}
290299

‎mongo/test/org/immutables/mongo/bson4gson/TypeConversionTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818

1919
importcom.google.gson.stream.JsonReader;
2020
importcom.google.gson.stream.JsonToken;
21+
importorg.bson.BsonBoolean;
2122
importorg.bson.BsonDateTime;
2223
importorg.bson.BsonDocument;
2324
importorg.bson.BsonDocumentReader;
2425
importorg.bson.BsonDouble;
2526
importorg.bson.BsonInt32;
2627
importorg.bson.BsonInt64;
28+
importorg.bson.BsonNull;
2729
importorg.bson.BsonObjectId;
2830
importorg.bson.BsonRegularExpression;
2931
importorg.bson.BsonTimestamp;
3032
importorg.bson.BsonValue;
3133
importorg.bson.types.ObjectId;
34+
importorg.junit.Assert;
3235
importorg.junit.Test;
3336

3437
importjava.io.IOException;
@@ -64,6 +67,22 @@ public void bsonDouble() throws IOException {
6467
check(readerFor(newBsonDouble(1.1)).nextString()).is(Double.toString(1.1));
6568
}
6669

70+
@Test
71+
publicvoidexceptions()throwsIOException {
72+
try {
73+
readerFor(newBsonBoolean(true)).nextInt();
74+
Assert.fail("didn't fail");
75+
}catch (IllegalStateExceptionignore) {
76+
}
77+
78+
try {
79+
readerFor(BsonNull.VALUE).nextInt();
80+
Assert.fail("didn't fail");
81+
}catch (IllegalStateExceptionignore) {
82+
}
83+
84+
}
85+
6786
@Test
6887
publicvoiddateTime()throwsIOException {
6988
finallongepoch =System.currentTimeMillis();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp