1
1
package io .dropwizard .configuration ;
2
2
3
- import static org .assertj .core .api .Assertions .assertThat ;
4
-
3
+ import com .fasterxml .jackson .annotation .JsonIgnore ;
5
4
import com .fasterxml .jackson .annotation .JsonProperty ;
6
5
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
6
+ import com .fasterxml .jackson .databind .ObjectMapper ;
7
7
import io .dropwizard .jackson .Jackson ;
8
+ import org .junit .jupiter .api .Test ;
9
+ import org .junit .jupiter .params .ParameterizedTest ;
10
+ import org .junit .jupiter .params .provider .Arguments ;
11
+ import org .junit .jupiter .params .provider .MethodSource ;
12
+
8
13
import java .util .ArrayList ;
9
14
import java .util .Collections ;
10
15
import java .util .List ;
11
16
import java .util .Set ;
12
17
import java .util .stream .Stream ;
13
- import org . junit . jupiter . params . ParameterizedTest ;
14
- import org .junit . jupiter . params . provider . Arguments ;
15
- import org .junit . jupiter . params . provider . MethodSource ;
18
+
19
+ import static org .assertj . core . api . Assertions . assertThat ;
20
+ import static org .assertj . core . api . Assertions . assertThatNoException ;
16
21
17
22
class ConfigurationMetadataTest {
18
23
@@ -60,7 +65,7 @@ public interface ExampleInterfaceWithDefaultImpl {
60
65
61
66
@ SuppressWarnings ("UnusedDeclaration" )
62
67
public static class DefaultExampleInterface implements ExampleInterface ,
63
- ExampleInterfaceWithDefaultImpl {
68
+ ExampleInterfaceWithDefaultImpl {
64
69
65
70
@ JsonProperty
66
71
private String [] array = new String []{};
@@ -84,18 +89,64 @@ public Set<String> getSet() {
84
89
}
85
90
}
86
91
92
+ public static class Issue3528Configuration {
93
+ @ JsonProperty
94
+ public ObjectMapper getMapper () {
95
+ return new ObjectMapper ();
96
+ }
97
+ }
98
+
99
+ public static class SelfReferencingConfiguration {
100
+ private String str = "test" ;
101
+
102
+ @ JsonProperty
103
+ public SelfReferencingConfiguration getSelfReferencingConfiguration () {
104
+ return new SelfReferencingConfiguration ();
105
+ }
106
+
107
+ @ JsonProperty
108
+ public String getStr () {
109
+ return str ;
110
+ }
111
+ }
112
+
113
+ public static class SelfReferencingIgnoredConfiguration {
114
+ private String str = "test" ;
115
+ private Long number = 42L ;
116
+ @ JsonIgnore
117
+ private SelfReferencingConfiguration ignored = new SelfReferencingConfiguration ();
118
+
119
+ @ JsonIgnore
120
+ public SelfReferencingConfiguration getSelfReferencingConfiguration () {
121
+ return new SelfReferencingConfiguration ();
122
+ }
123
+
124
+ @ JsonProperty
125
+ public String getStr () {
126
+ return str ;
127
+ }
128
+
129
+ public Long getNumber () {
130
+ return number ;
131
+ }
132
+
133
+ public SelfReferencingConfiguration getIgnored () {
134
+ return ignored ;
135
+ }
136
+ }
137
+
87
138
@ ParameterizedTest
88
139
@ MethodSource ("provideArgsForShouldDiscoverAllFields" )
89
140
public void shouldDiscoverAllFields (String name , boolean isPrimitive ,
90
- boolean isCollectionOrArrayType ,
91
- Class <?> klass ) {
141
+ boolean isCollectionOrArrayType ,
142
+ Class <?> klass ) {
92
143
final ConfigurationMetadata metadata = new ConfigurationMetadata (
93
- Jackson .newObjectMapper (), ExampleConfiguration .class );
144
+ Jackson .newObjectMapper (), ExampleConfiguration .class );
94
145
95
146
assertThat (metadata .fields .get (name )).isNotNull ().satisfies ((f ) -> {
96
147
assertThat (f .isPrimitive ()).isEqualTo (isPrimitive );
97
148
assertThat (f .isCollectionLikeType () || f .isArrayType ())
98
- .isEqualTo (isCollectionOrArrayType );
149
+ .isEqualTo (isCollectionOrArrayType );
99
150
100
151
if (isCollectionOrArrayType ) {
101
152
assertThat (f .getContentType ().isTypeOrSubTypeOf (klass )).isTrue ();
@@ -107,40 +158,61 @@ public void shouldDiscoverAllFields(String name, boolean isPrimitive,
107
158
108
159
private static Stream <Arguments > provideArgsForShouldDiscoverAllFields () {
109
160
return Stream .of (
110
- Arguments .of ("port" , true , false , Integer .TYPE ),
111
- Arguments .of ("example" , false , false , ExampleInterface .class ),
112
- Arguments .of ("exampleWithDefault.array" , false , true , String .class ),
113
- Arguments .of ("exampleWithDefault.list" , false , true , String .class ),
114
- Arguments .of ("exampleWithDefault.set" , false , true , String .class ),
115
- Arguments .of ("exampleWithDefaults[*].array" , false , true , String .class ),
116
- Arguments .of ("exampleWithDefaults[*].list" , false , true , String .class ),
117
- Arguments .of ("exampleWithDefaults[*].set" , false , true , String .class )
161
+ Arguments .of ("port" , true , false , Integer .TYPE ),
162
+ Arguments .of ("example" , false , false , ExampleInterface .class ),
163
+ Arguments .of ("exampleWithDefault.array" , false , true , String .class ),
164
+ Arguments .of ("exampleWithDefault.list" , false , true , String .class ),
165
+ Arguments .of ("exampleWithDefault.set" , false , true , String .class ),
166
+ Arguments .of ("exampleWithDefaults[*].array" , false , true , String .class ),
167
+ Arguments .of ("exampleWithDefaults[*].list" , false , true , String .class ),
168
+ Arguments .of ("exampleWithDefaults[*].set" , false , true , String .class )
118
169
);
119
170
}
120
171
121
172
@ ParameterizedTest
122
173
@ MethodSource ("provideArgsForIsCollectionOfStringsShouldWork" )
123
174
public void isCollectionOfStringsShouldWork (String name , boolean isCollectionOfStrings ) {
124
175
final ConfigurationMetadata metadata = new ConfigurationMetadata (
125
- Jackson .newObjectMapper (), ExampleConfiguration .class );
176
+ Jackson .newObjectMapper (), ExampleConfiguration .class );
126
177
127
178
assertThat (metadata .isCollectionOfStrings (name )).isEqualTo (isCollectionOfStrings );
128
179
}
129
180
130
-
131
181
private static Stream <Arguments > provideArgsForIsCollectionOfStringsShouldWork () {
132
182
return Stream .of (
133
- Arguments .of ("doesnotexist" , false ),
134
- Arguments .of ("port" , false ),
135
- Arguments .of ("example.array" , false ),
136
- Arguments .of ("example.list" , false ),
137
- Arguments .of ("example.set" , false ),
138
- Arguments .of ("exampleWithDefault.array" , true ),
139
- Arguments .of ("exampleWithDefault.list" , true ),
140
- Arguments .of ("exampleWithDefault.set" , true ),
141
- Arguments .of ("exampleWithDefaults[0].array" , true ),
142
- Arguments .of ("exampleWithDefaults[0].list" , true ),
143
- Arguments .of ("exampleWithDefaults[0].set" , true )
183
+ Arguments .of ("doesnotexist" , false ),
184
+ Arguments .of ("port" , false ),
185
+ Arguments .of ("example.array" , false ),
186
+ Arguments .of ("example.list" , false ),
187
+ Arguments .of ("example.set" , false ),
188
+ Arguments .of ("exampleWithDefault.array" , true ),
189
+ Arguments .of ("exampleWithDefault.list" , true ),
190
+ Arguments .of ("exampleWithDefault.set" , true ),
191
+ Arguments .of ("exampleWithDefaults[0].array" , true ),
192
+ Arguments .of ("exampleWithDefaults[0].list" , true ),
193
+ Arguments .of ("exampleWithDefaults[0].set" , true )
144
194
);
145
195
}
196
+
197
+ @ Test
198
+ void issue3528ShouldNotProduceOutOfMemoryError () {
199
+ assertThatNoException ().isThrownBy (
200
+ () -> new ConfigurationMetadata (Jackson .newObjectMapper (), Issue3528Configuration .class ));
201
+ }
202
+
203
+ @ Test
204
+ void fieldsAnnotatedWithJsonIgnoreShouldBeIgnored () {
205
+ final ConfigurationMetadata metadata =
206
+ new ConfigurationMetadata (Jackson .newObjectMapper (), SelfReferencingIgnoredConfiguration .class );
207
+
208
+ assertThat (metadata .fields ).containsOnlyKeys ("str" , "number" );
209
+ }
210
+
211
+ @ Test
212
+ void selfReferencingConfigurationShouldNotLoop () {
213
+ final ConfigurationMetadata metadata =
214
+ new ConfigurationMetadata (Jackson .newObjectMapper (), SelfReferencingConfiguration .class );
215
+
216
+ assertThat (metadata .fields ).containsOnlyKeys ("selfReferencingConfiguration.str" , "str" );
217
+ }
146
218
}
0 commit comments