5
5
6
6
import org .junit .Test ;
7
7
8
- import com .fasterxml .jackson .annotation .JsonIgnore ;
9
8
import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
10
9
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
11
10
import com .fasterxml .jackson .annotation .JsonTypeInfo .Id ;
11
+
12
12
import com .fasterxml .jackson .core .JsonParseException ;
13
- import com .fasterxml .jackson .databind .DatabindContext ;
14
- import com .fasterxml .jackson .databind .DeserializationFeature ;
15
- import com .fasterxml .jackson .databind .JavaType ;
16
- import com .fasterxml .jackson .databind .JsonMappingException ;
17
- import com .fasterxml .jackson .databind .ObjectMapper ;
18
- import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
13
+
14
+ import com .fasterxml .jackson .databind .*;
19
15
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
20
16
import com .fasterxml .jackson .databind .annotation .JsonPOJOBuilder ;
21
17
import com .fasterxml .jackson .databind .annotation .JsonTypeIdResolver ;
22
- import com .fasterxml .jackson .databind .jsontype .TypeIdResolver ;
23
- import com .fasterxml .jackson .databind .type . TypeFactory ;
18
+ import com .fasterxml .jackson .databind .jsontype .ext . ExternalTypeIdTest1288 . ClassesWithBuilder . PaymentMean ;
19
+ import com .fasterxml .jackson .databind .jsontype . impl . TypeIdResolverBase ;
24
20
25
- public class ExternalTypeIdTest1288 {
21
+ @ SuppressWarnings ("hiding" )
22
+ public class ExternalTypeIdTest1288 extends BaseMapTest
23
+ {
26
24
public static class ClassesWithoutBuilder {
27
25
28
26
public static class CreditCardDetails implements PaymentDetails {
29
27
30
- private String cardHolderFirstName ;
31
- private String cardHolderLastName ;
32
- private String number ;
33
- private String expiryDate ;
34
- private int csc ;
35
- private String address ;
36
- private String zipCode ;
37
- private String city ;
38
- private String province ;
28
+ protected String cardHolderFirstName ;
29
+ protected String cardHolderLastName ;
30
+ protected String number ;
31
+ protected String expiryDate ;
32
+ protected int csc ;
33
+ protected String address ;
34
+ protected String zipCode ;
35
+ protected String city ;
36
+ protected String province ;
39
37
40
- private String countryCode ;
38
+ protected String countryCode ;
41
39
42
- private String description ;
40
+ protected String description ;
43
41
44
42
public void setCardHolderFirstName (String cardHolderFirstName ) {
45
43
this .cardHolderFirstName = cardHolderFirstName ;
@@ -90,9 +88,9 @@ public void setDescription (String description) {
90
88
91
89
public static class EncryptedCreditCardDetails implements PaymentDetails {
92
90
93
- private UUID paymentInstrumentID ;
91
+ protected UUID paymentInstrumentID ;
94
92
95
- private String name ;
93
+ protected String name ;
96
94
97
95
public void setPaymentInstrumentID (UUID paymentInstrumentID ) {
98
96
this .paymentInstrumentID = paymentInstrumentID ;
@@ -137,9 +135,9 @@ public interface Builder {
137
135
138
136
public static class PaymentMean {
139
137
140
- private FormOfPayment formOfPayment ;
138
+ FormOfPayment formOfPayment ;
141
139
142
- private PaymentDetails paymentDetails ;
140
+ PaymentDetails paymentDetails ;
143
141
144
142
public void setFormOfPayment (FormOfPayment formOfPayment ) {
145
143
this .formOfPayment = formOfPayment ;
@@ -153,12 +151,7 @@ public void setPaymentDetails (PaymentDetails paymentDetails) {
153
151
154
152
}
155
153
156
- public static class PaymentDetailsTypeIdResolver implements TypeIdResolver {
157
-
158
- @ Override
159
- public void init (JavaType baseType ) {
160
- }
161
-
154
+ public static class PaymentDetailsTypeIdResolver extends TypeIdResolverBase {
162
155
@ SuppressWarnings ("unchecked" )
163
156
@ Override
164
157
public String idFromValue (Object value ) {
@@ -172,17 +165,12 @@ public String idFromValue (Object value) {
172
165
public String idFromValueAndType (Object value , Class <?> suggestedType ) {
173
166
return this .idFromValue (value );
174
167
}
175
-
176
- @ Override
177
- public String idFromBaseType () {
178
- return null ;
179
- }
180
-
168
+
181
169
@ Override
182
- public JavaType typeFromId (DatabindContext context , String id ) throws IOException {
183
- return TypeFactory . defaultInstance ().constructType (FormOfPayment .valueOf (id ).getDetailsClass ());
170
+ public JavaType typeFromId (DatabindContext context , String id ) {
171
+ return context . getTypeFactory ().constructType (FormOfPayment .valueOf (id ).getDetailsClass ());
184
172
}
185
-
173
+
186
174
@ Override
187
175
public String getDescForKnownTypeIds () {
188
176
return "PaymentDetails" ;
@@ -192,7 +180,6 @@ public String getDescForKnownTypeIds () {
192
180
public Id getMechanism () {
193
181
return JsonTypeInfo .Id .CUSTOM ;
194
182
}
195
-
196
183
}
197
184
}
198
185
@@ -206,25 +193,25 @@ public static class CompanyCreditCardDetailsBuilder implements Builder {
206
193
private String cardHolderLastName ;
207
194
private String number ;
208
195
private String expiryDate ;
209
- private int csc ;
196
+ private int csc ;
210
197
private String address ;
211
198
private String zipCode ;
212
199
private String city ;
213
200
private String province ;
214
201
private String countryCode ;
215
202
216
- public CompanyCreditCardDetailsBuilder address (final String address ) {
217
- this . address = address ;
203
+ public CompanyCreditCardDetailsBuilder address (final String a ) {
204
+ address = a ;
218
205
return this ;
219
206
}
220
207
221
208
@ Override
222
- public CreditCardDetails build () {
209
+ public CreditCardDetails build () {
223
210
return new CreditCardDetails (this .cardHolderFirstName , this .cardHolderLastName , this .number , this .expiryDate , this .csc , this .address , this .zipCode , this .city ,
224
211
this .province , this .countryCode , "COMPANY CREDIT CARD" );
225
212
}
226
213
227
- public CompanyCreditCardDetailsBuilder cardHolderFirstName (final String cardHolderFirstName ) {
214
+ public CompanyCreditCardDetailsBuilder cardHolderFirstName (final String cardHolderFirstName ) {
228
215
this .cardHolderFirstName = cardHolderFirstName ;
229
216
return this ;
230
217
}
@@ -335,31 +322,31 @@ public IndividualCreditCardDetailsBuilder number (final String number) {
335
322
return this ;
336
323
}
337
324
338
- public IndividualCreditCardDetailsBuilder province (final String province ) {
339
- this . province = province ;
325
+ public IndividualCreditCardDetailsBuilder province (final String p ) {
326
+ province = p ;
340
327
return this ;
341
328
}
342
329
343
- public IndividualCreditCardDetailsBuilder zipCode (final String zipCode ) {
344
- this . zipCode = zipCode ;
330
+ public IndividualCreditCardDetailsBuilder zipCode (final String z ) {
331
+ zipCode = z ;
345
332
return this ;
346
333
}
347
334
348
335
}
349
336
350
- private final String cardHolderFirstName ;
351
- private final String cardHolderLastName ;
352
- private final String number ;
353
- private final String expiryDate ;
354
- private final int csc ;
355
- private final String address ;
356
- private final String zipCode ;
357
- private final String city ;
358
- private final String province ;
337
+ protected final String cardHolderFirstName ;
338
+ protected final String cardHolderLastName ;
339
+ protected final String number ;
340
+ protected final String expiryDate ;
341
+ protected final int csc ;
342
+ protected final String address ;
343
+ protected final String zipCode ;
344
+ protected final String city ;
345
+ protected final String province ;
359
346
360
- private final String countryCode ;
347
+ protected final String countryCode ;
361
348
362
- private final String description ;
349
+ protected final String description ;
363
350
364
351
public CreditCardDetails (final String cardHolderFirstName , final String cardHolderLastName , final String number , final String expiryDate , final int csc ,
365
352
final String address , final String zipCode , final String city , final String province , final String countryCode , final String description ) {
@@ -399,13 +386,11 @@ public InstrumentedCreditCardBuilder paymentInstrumentID (final UUID paymentInst
399
386
this .paymentInstrumentID = paymentInstrumentID ;
400
387
return this ;
401
388
}
402
-
403
389
}
404
-
405
- private final UUID paymentInstrumentID ;
406
-
407
- private final String name ;
408
-
390
+
391
+ protected final UUID paymentInstrumentID ;
392
+ protected final String name ;
393
+
409
394
private EncryptedCreditCardDetails (final UUID paymentInstrumentID , final String name ) {
410
395
super ();
411
396
this .paymentInstrumentID = paymentInstrumentID ;
@@ -472,27 +457,21 @@ public Builder paymentDetails (final PaymentDetails val) {
472
457
}
473
458
}
474
459
475
- public static Builder create () {
476
- return new Builder ();
460
+ public static Builder create () {
461
+ return new Builder ();
477
462
}
478
-
479
- private final FormOfPayment formOfPayment ;
480
-
481
- private final PaymentDetails paymentDetails ;
482
-
463
+
464
+ protected final FormOfPayment formOfPayment ;
465
+ protected final PaymentDetails paymentDetails ;
466
+
483
467
private PaymentMean (final FormOfPayment formOfPayment , final PaymentDetails paymentDetails ) {
484
468
super ();
485
469
this .formOfPayment = formOfPayment ;
486
470
this .paymentDetails = paymentDetails ;
487
471
}
488
472
}
489
473
490
- public static class PaymentDetailsTypeIdResolver implements TypeIdResolver {
491
-
492
- @ Override
493
- public void init (JavaType baseType ) {
494
- }
495
-
474
+ public static class PaymentDetailsTypeIdResolver extends TypeIdResolverBase {
496
475
@ SuppressWarnings ("unchecked" )
497
476
@ Override
498
477
public String idFromValue (Object value ) {
@@ -506,15 +485,10 @@ public String idFromValue (Object value) {
506
485
public String idFromValueAndType (Object value , Class <?> suggestedType ) {
507
486
return this .idFromValue (value );
508
487
}
509
-
510
- @ Override
511
- public String idFromBaseType () {
512
- return null ;
513
- }
514
-
488
+
515
489
@ Override
516
- public JavaType typeFromId (DatabindContext context , String id ) throws IOException {
517
- return TypeFactory . defaultInstance ().constructType (FormOfPayment .valueOf (id ).getDetailsClass ());
490
+ public JavaType typeFromId (DatabindContext context , String id ) {
491
+ return context . getTypeFactory ().constructType (FormOfPayment .valueOf (id ).getDetailsClass ());
518
492
}
519
493
520
494
@ Override
@@ -526,23 +500,22 @@ public String getDescForKnownTypeIds () {
526
500
public Id getMechanism () {
527
501
return JsonTypeInfo .Id .CUSTOM ;
528
502
}
529
-
530
503
}
531
504
}
532
-
533
- @ Test
534
- public void tryToDeserialize () throws JsonParseException , JsonMappingException , IOException {
505
+
506
+ public void testVisibleExternalTypeId1288 () throws Exception
507
+ {
535
508
// given
536
509
final String asJson1 = "{\" form_of_payment\" :\" INDIVIDUAL_CREDIT_CARD\" , \" payment_details\" :{\" card_holder_first_name\" :\" John\" , \" card_holder_last_name\" :\" Doe\" , \" number\" :\" XXXXXXXXXXXXXXXX\" , \" expiry_date\" :\" MM/YY\" ,"
537
510
+ "\" csc\" :666,\" address\" :\" 10 boulevard de Sebastopol\" ,\" zip_code\" :\" 75001\" ,\" city\" :\" Paris\" ,\" province\" :\" Ile-de-France\" ,\" country_code\" :\" FR\" ,\" description\" :\" John Doe personal credit card\" }}" ;
538
511
final String asJson2 = "{\" form_of_payment\" :\" INSTRUMENTED_CREDIT_CARD\" ,\" payment_details\" :{\" payment_instrument_id\" :\" 00000000-0000-0000-0000-000000000000\" , \" name\" :\" Mr John Doe encrypted credit card\" }}" ;
539
- final ObjectMapper objectMapper = new ObjectMapper ().setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE )
540
- .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
541
-
542
- // when
543
- objectMapper . readValue ( asJson1 , ClassesWithoutBuilder . PaymentMean . class );
544
- objectMapper .readValue (asJson2 , ClassesWithBuilder .PaymentMean .class );
545
-
546
- // then payment1 and paymentMean2 should be unmarshalled successfully
512
+ final ObjectMapper objectMapper = new ObjectMapper ().setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE );
513
+ // .disable (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
514
+
515
+ ClassesWithoutBuilder . PaymentMean ob1 = objectMapper . readValue ( asJson1 , ClassesWithoutBuilder . PaymentMean . class );
516
+ assertNotNull ( ob1 );
517
+ ClassesWithBuilder . PaymentMean ob2 = objectMapper .readValue (asJson2 , ClassesWithBuilder .PaymentMean .class );
518
+ assertNotNull ( ob2 );
519
+
547
520
}
548
521
}
0 commit comments