/
BeanIntrospectionModuleSpec.groovy
1093 lines (853 loc) · 30.8 KB
/
BeanIntrospectionModuleSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.micronaut.jackson.modules
import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.annotation.JsonView
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonNaming
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper
import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope
import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.Creator
import io.micronaut.core.annotation.Introspected
import io.micronaut.http.hateoas.JsonError
import io.micronaut.jackson.JacksonConfiguration
import io.micronaut.jackson.modules.testcase.EmailTemplate
import io.micronaut.jackson.modules.testcase.Notification
import io.micronaut.jackson.modules.wrappers.*
import spock.lang.Issue
import spock.lang.Unroll
import spock.lang.Specification
import java.beans.ConstructorProperties
import java.time.LocalDateTime
class BeanIntrospectionModuleSpec extends Specification {
void "Bean introspection works with a bean without JsonIgnore annotations"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
IgnoreTest guide = new IgnoreTest(name:"Test", code: 9999)
String json = objectMapper.writeValueAsString(guide)
then:
noExceptionThrown()
json == '{"name":"Test"}'
cleanup:
ctx.close()
}
void "Bean introspection works with a bean without JsonInclude annotations"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
GuideWithoutJsonIncludeAnnotations guide = new GuideWithoutJsonIncludeAnnotations()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)
then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide"}'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
void "Bean introspection works with a bean without JsonInclude annotations - serializationInclusion ALWAYS"() {
given:
ApplicationContext ctx = ApplicationContext.run(
'jackson.serializationInclusion':'ALWAYS'
)
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
GuideWithoutJsonIncludeAnnotations guide = new GuideWithoutJsonIncludeAnnotations()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)
then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide","author":null}'
cleanup:
ctx.close()
}
void "Bean introspection works with JsonInclude.Include.NON_NULL"() {
given:
ApplicationContext ctx = ApplicationContext.run(
'jackson.serializationInclusion':'ALWAYS'
)
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
Guide guide = new Guide()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)
then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide"}'
cleanup:
ctx.close()
}
void "Bean introspection works with JsonInclude.Include.ALWAYS"() {
given:
ApplicationContext ctx = ApplicationContext.run(
'jackson.serializationInclusion':'ALWAYS'
)
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
GuideWithNull guide = new GuideWithNull()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)
then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide","author":null}'
cleanup:
ctx.close()
}
void "Bean introspection with empty optional"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
def value = new OptionalAuthor(name: Optional.<String>empty())
String json = objectMapper.writeValueAsString(value)
then:
noExceptionThrown()
json == '{}'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
void "test that introspected serialization works"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:"json unwrapped is used"
def plant = new Plant(name: "Rose", attributes: new Attributes(hasFlowers: true, color: "green"))
def str = objectMapper.writeValueAsString(plant)
then:"The result is correct"
str == '{"name":"Rose","color":"green","hasFlowers":true}'
when:"deserializing"
def read = objectMapper.readValue(str, Plant)
then:
read == plant
read.attributes.color == 'green'
read.attributes.hasFlowers
when:
Book b = objectMapper.readValue('{"opt":null,"book_title":"The Stand", "book_pages":1000,"author":{"name":"Fred"}}', Book)
then:
ctx.getBean(JacksonConfiguration).beanIntrospectionModule
ctx.containsBean(BeanIntrospectionModule)
b.title == 'The Stand'
b.pages == 1000
b.author.name == "Fred"
!b.opt.isPresent()
when:
def sw = new StringWriter()
objectMapper.writeValue(sw, b)
def result = sw.toString()
then:
!result.contains('"opt":{"present":false}')
result.contains('"book_title":"The Stand"') // '{"book_title":"The Stand","book_pages":1000,author":{"name":"Fred"}}'
result.contains('"book_pages":1000')
result.contains('"author":{"name":"Fred"}')
when:
result = objectMapper.writerWithView(PublicView).writeValueAsString(b)
then:
result.contains('"book_title":')
!result.contains('"book_pages":')
when:
result = objectMapper.writerWithView(AllView).writeValueAsString(b)
then:
result.contains('"book_title":')
result.contains('"book_pages":')
cleanup:
ctx.close()
}
void "test that introspected serialization works with @JsonAnyGetter"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
def plant = new PlantWithAnyGetter(name: "Rose", attributes: [color: "green", hasFlowers: true])
def str = objectMapper.writeValueAsString(plant)
then:
str == '{"name":"Rose","color":"green","hasFlowers":true}'
when:"deserializing"
def read = objectMapper.readValue(str, PlantWithAnyGetter)
then:
read == plant
read.attributes.color == 'green'
read.attributes.hasFlowers
cleanup:
ctx.close()
}
void "test that introspected serialization of errors works"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
JsonError error = objectMapper.readValue('{"message":"Page Not Found","_links":{"self":{"href":"/","templated":false}}}', JsonError)
then:
ctx.getBean(JacksonConfiguration).beanIntrospectionModule
ctx.containsBean(BeanIntrospectionModule)
error.message == 'Page Not Found'
error.links.size() == 1
error.links.get("self").isPresent()
error.links.get("self").get().first().href == '/'
when:
def sw = new StringWriter()
objectMapper.writeValue(sw, error)
def result = sw.toString()
then:
result == '{"message":"Page Not Found","_links":{"self":{"href":"/","templated":false}}}'
cleanup:
ctx.close()
}
void "test that introspected serialization works for JsonCreator.Mode.DELEGATING"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
Edition e = objectMapper.readValue('{"book_title":"The Stand"}', Edition)
then:
ctx.getBean(JacksonConfiguration).beanIntrospectionModule
ctx.containsBean(BeanIntrospectionModule)
e.title == 'The Stand'
when:
def sw = new StringWriter()
objectMapper.writeValue(sw, e)
def result = sw.toString()
then:
result.contains('"title":"The Stand"')
cleanup:
ctx.close()
}
def "should deserialize field with hierarchy"() {
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
def notif = """
{"id":586387198220282880, "template":{"templateType":"email","textTemplate":"Ahoj"}}
"""
def value = objectMapper.readValue(notif, Notification.class)
then:
value.getTemplate() instanceof EmailTemplate
cleanup:
ctx.close()
}
void "test deserializing from basic types"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
def wrapper = objectMapper.readValue("\"string\"", StringWrapper)
then:
noExceptionThrown()
wrapper.value == "string"
when:
wrapper = objectMapper.readValue("32", IntWrapper)
then:
noExceptionThrown()
wrapper.value == 32I
when:
wrapper = objectMapper.readValue("32", IntegerWrapper)
then:
noExceptionThrown()
wrapper.value == 32I
when:
wrapper = objectMapper.readValue("32", LongWrapper)
then:
noExceptionThrown()
wrapper.value == 32L
when:
wrapper = objectMapper.readValue("false", BooleanWrapper)
then:
noExceptionThrown()
!wrapper.value
when:
wrapper = objectMapper.readValue("23.23", DoubleWrapper)
then:
noExceptionThrown()
wrapper.value == 23.23D
cleanup:
ctx.close()
}
void "test deserializing with a json naming strategy"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
NamingStrategy instance = objectMapper.readValue("{ \"FooBar\": \"bad\" }", NamingStrategy)
then:
instance.fooBar == "bad"
cleanup:
ctx.close()
}
void "test deserializing from a list"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
ListWrapper instance = objectMapper.readValue("[\"bad\"]", ListWrapper)
then:
instance.value == ["bad"]
cleanup:
ctx.close()
}
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/5160")
void "test that snake_case with non-introspected beans works"() {
given:
ApplicationContext ctx = ApplicationContext.run([
'jackson.property-naming-strategy': 'SNAKE_CASE',
'jackson.json-view.enabled': true,
'jackson.bean-introspection-module': true
])
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
Publisher p = objectMapper.readValue('{"publisher_name":"RandomHouse"}', Publisher)
then:
ctx.getBean(JacksonConfiguration).beanIntrospectionModule
ctx.containsBean(BeanIntrospectionModule)
p.publisherName == 'RandomHouse'
when:
def result = objectMapper.writerWithView(PublicView).writeValueAsString(p)
then:
!result.contains('"publisher_name":')
when:
result = objectMapper.writerWithView(AllView).writeValueAsString(p)
then:
result.contains('"publisher_name":')
cleanup:
ctx.close()
}
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/5088")
void "test deserializing from a list of pojos"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
MyReqBody reqBody = objectMapper.readValue('[{"name":"Joe"},{"name":"Sally"}]', MyReqBody)
then:
reqBody.getItems().size() == 2
reqBody.getItems()[0].name == "Joe"
reqBody.getItems()[1].name == "Sally"
when:
MyItemBody itemBody = objectMapper.readValue('[{"name":"Joe"},{"name":"Sally"}]', MyItemBody)
then:
itemBody.getItems().size() == 2
itemBody.getItems()[0].name == "Joe"
itemBody.getItems()[1].name == "Sally"
cleanup:
ctx.close()
}
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/5078")
void "test more list wrapping scenarios"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
when:
OuterList outerList = objectMapper.readValue("{\"wrapper\":{\"inner\":[]}}", OuterList.class)
then:
noExceptionThrown()
outerList.wrapper.inner.isEmpty()
when:
OuterArray outerArray = objectMapper.readValue("{\"wrapper\":{\"inner\":[]}}", OuterArray.class)
then:
noExceptionThrown()
outerArray.wrapper.inner.length == 0
}
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/6472")
void "@JsonProperty annotation on setter"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.readValue('{"bar":"baz"}', JsonPropertyOnSetter.class).foo == 'baz'
objectMapper.writeValueAsString(new JsonPropertyOnSetter(foo: 'baz')) == '{"bar":"baz"}'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/6309")
void "@JsonSerialize annotation"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.readValue('{"foo":"Bar"}', JsonSerializeAnnotated.class).foo == 'bar'
objectMapper.writeValueAsString(new JsonSerializeAnnotated(foo: 'Bar')) == '{"foo":"BAR"}'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
void "creator property that doesn't have a getter"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.writeValueAsString(new DifferentCreator('baz')) == '{"fooBar":"baz"}'
objectMapper.readValue('{"foo_bar":"baz"}', DifferentCreator).fooBar == 'baz'
// this is currently broken with ignoreReflectiveProperties
ignoreReflectiveProperties || objectMapper.readValue('{"fooBar":"baz"}', DifferentCreator).fooBar == 'baz'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
void "introspection creator property that doesn't have a getter"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.writeValueAsString(new IntrospectionCreator('baz')) == '{"label":"BAZ"}'
objectMapper.readValue('{"name":"baz","label":"foo"}', IntrospectionCreator).name == 'baz'
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
@Introspected
static class Book {
@JsonProperty("book_title")
@JsonView(PublicView)
String title
@JsonProperty("book_pages")
@JsonView(AllView)
int pages
Author author
Optional<String> opt
@JsonCreator Book(@JsonProperty("book_title") String title) {
this.title = title
}
}
@Introspected
static class Publisher {
@JsonView(AllView)
String publisherName
}
@Introspected
static class GuideWithoutJsonIncludeAnnotations {
String name
String author
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@Introspected
static class Guide {
String name
String author
}
@JsonInclude(JsonInclude.Include.ALWAYS)
@Introspected
static class GuideWithNull {
String name
String author
}
@Introspected
static class Author {
String name
}
@Introspected
static class OptionalAuthor {
Optional<String> name
}
@Introspected
static class Edition {
String title
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static Edition fromBook(Book book) {
return new Edition(title: book.title)
}
}
@Introspected
@EqualsAndHashCode
static class Plant {
String name
@JsonUnwrapped
Attributes attributes
}
@Introspected
@EqualsAndHashCode
static class Attributes {
String color
boolean hasFlowers
}
@Introspected
@EqualsAndHashCode
static class PlantWithAnyGetter {
String name
private Map<String, Object> attributes = [:]
@JsonAnyGetter
Map<String, Object> getAttributes() {
return attributes
}
@JsonAnySetter
void addAttribute(String key, Object value) {
attributes[key] = value
}
}
//Used for @JsonView
static class PublicView {}
static class AllView extends PublicView {}
@Introspected
@JsonNaming(value = PropertyNamingStrategies.UpperCamelCaseStrategy.class)
static class NamingStrategy {
@PackageScope
final String fooBar
@ConstructorProperties(["fooBar"])
NamingStrategy(String fooBar) {
this.fooBar = fooBar
}
}
@Introspected
static class ListWrapper {
final List<String> value
@JsonCreator
ListWrapper(List<String> value) {
this.value = value
}
}
@Introspected
static class IgnoreTest {
String name
@JsonIgnore
int code
}
@Introspected
static class MyReqBody {
private final List<MyItem> items
@JsonCreator
MyReqBody(final List<MyItem> items) {
this.items = items
}
List<MyItem> getItems() {
items
}
}
@Introspected
static class MyItem {
String name
}
@Introspected
static class MyGenericBody<T> {
private final List<T> items
MyGenericBody(final List<T> items) {
this.items = items
}
List<T> getItems() {
items
}
}
@Introspected
static class MyItemBody extends MyGenericBody<MyItem> {
@JsonCreator
MyItemBody(final List<MyItem> items) {
super(items)
}
}
@Introspected
static class WrapperList {
public final List<String> inner
@ConstructorProperties(["inner"])
@JsonCreator
WrapperList(List<String> inner) {
this.inner = inner
}
}
@Introspected
static class OuterList {
public final WrapperList wrapper
@ConstructorProperties(["wrapper"])
@JsonCreator
OuterList(WrapperList wrapper) {
this.wrapper = wrapper
}
}
@Introspected
static class WrapperArray {
public final String[] inner
@ConstructorProperties(["inner"])
@JsonCreator
WrapperArray(String[] inner) {
this.inner = inner
}
}
@Introspected
static class OuterArray {
public final WrapperArray wrapper
@ConstructorProperties(["wrapper"])
@JsonCreator
OuterArray(WrapperArray wrapper) {
this.wrapper = wrapper
}
}
@Introspected
static class JsonPropertyOnSetter {
private String foo
public String getFoo() {
return foo
}
@JsonProperty("bar")
public void setFoo(String foo) {
this.foo = foo
}
}
@Introspected
static class JsonSerializeAnnotated {
@JsonSerialize(using = UpperCaseSerializer)
@JsonDeserialize(using = LowerCaseDeserializer)
String foo
@Introspected
static class UpperCaseSerializer extends JsonSerializer<String> {
@Override
void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.toUpperCase(Locale.ENGLISH))
}
}
@Introspected
static class LowerCaseDeserializer extends JsonDeserializer<String> {
@Override
String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return p.valueAsString.toLowerCase(Locale.ENGLISH)
}
}
}
@Introspected
static class DifferentCreator {
private final String fooBar;
@JsonCreator
public DifferentCreator(@JsonProperty('foo_bar') String fooBar) {
this.fooBar = fooBar
}
public String getFooBar() {
return fooBar;
}
}
@Introspected
static class IntrospectionCreator {
private final String name
@Creator
public IntrospectionCreator(String name) {
this.name = name
}
public String getLabel() {
name.toUpperCase()
}
}
void "JsonFormat"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
def ldt = LocalDateTime.of(2021, 11, 22, 10, 37)
expect:
objectMapper.writeValueAsString(new FormatBean(ldt: ldt)) == '{"ldt":"2021-11-22 10:37:00"}'
objectMapper.readValue('{"ldt":"2021-11-22 10:37:00"}', FormatBean).ldt == ldt
cleanup:
ctx.close()
where:
ignoreReflectiveProperties << [true, false]
}
@Introspected
static class FormatBean {
private LocalDateTime ldt
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime getLdt() {
return ldt
}
public void setLdt(LocalDateTime ldt) {
this.ldt = ldt
}
}
@Issue('https://github.com/micronaut-projects/micronaut-core/issues/6625')
void "JsonIgnore on main accessor, with secondary override"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.writeValueAsString(new IgnoreBean()) == '{"foo":"bar"}'
objectMapper.readValue('{"foo":"bar"}', IgnoreBean).foo == 'foo'
objectMapper.readValue('{"foo":"bar"}', IgnoreBean).secondary == 'bar'
cleanup:
ctx.close()
where:
// need reflective access here, because with only introspections, the secondary methods arent available.
ignoreReflectiveProperties << [false]//[true, false]
}
@Introspected
static class IgnoreBean {
private String foo = 'foo'
private String secondary = 'bar'
@JsonProperty('foo')
public String secondary() {
return secondary
}
@JsonProperty('foo')
public void secondary(String s) {
this.secondary = s
}
@JsonIgnore
public String getFoo() {
return foo
}
@JsonIgnore
public void setFoo(String s) {
this.foo = s
}
}
@Unroll("JsonIgnore is supported with ignoreReflectiveProperties: #ignoreReflectiveProperties")
void "JsonIgnore on one accessor"(boolean ignoreReflectiveProperties) {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)
expect:
objectMapper.writeValueAsString(new IgnoreBean2(foo: 'x', bar: 'y')) == '{"bar":"y"}'
objectMapper.readValue('{"foo":"x","bar":"y"}', IgnoreBean2).foo == 'x'
objectMapper.readValue('{"foo":"x","bar":"y"}', IgnoreBean2).bar == null
cleanup:
ctx.close()
where:
// without reflection we only see JsonIgnore on the whole property
ignoreReflectiveProperties << [false]
}
@Introspected
static class IgnoreBean2 {
@JsonIgnore
private String foo
@JsonIgnore
private String bar
@JsonIgnore
public String getFoo() {
return foo
}
@JsonProperty('foo')
public void setFoo(String s) {
this.foo = s
}
@JsonProperty('bar')
public String getBar() {
return bar
}
@JsonIgnore
public void setBar(String s) {
this.bar = s
}
}
@Issue('https://github.com/micronaut-projects/micronaut-core/issues/5907')
void "xml modifier"() {
given:
ApplicationContext ctx = ApplicationContext.run()
XmlMapper objectMapper = new XmlMapper()
objectMapper.registerModule(ctx.getBean(BeanIntrospectionModule))
expect:
objectMapper.writeValueAsString(new UsesXmlElementWrapper()) ==
'<UsesXmlElementWrapper><nestedItems><strings>foo</strings><strings>bar</strings></nestedItems></UsesXmlElementWrapper>'
cleanup:
ctx.close()
}
@Introspected
static class UsesXmlElementWrapper {
private List<String> strings = ["foo", "bar"];
@JacksonXmlElementWrapper(/*useWrapping = false, */localName = "nestedItems")
public List<String> getStrings() {
return strings
}
}
@Unroll("JsonProperty annotation is supported with ignoreReflectiveProperties: #ignoreReflectiveProperties")
void "JsonProperty support"(boolean ignoreReflectiveProperties) {
given:
ApplicationContext ctx = ApplicationContext.run()
ctx.getBean(BeanIntrospectionModule).ignoreReflectiveProperties = ignoreReflectiveProperties
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)