@@ -82,20 +82,19 @@ public BeanForGH311(@JsonProperty("b") int b, @JsonProperty("a") int a) { //b an
82
82
public int getB () { return b ; }
83
83
}
84
84
85
- static class BeanForStrictOrdering {
86
- private final int a ;
87
- private int b ;
88
- private final int c ;
85
+ // We'll expect ordering of "FUBAR"
86
+ @ JsonPropertyOrder ({ "f" })
87
+ static class OrderingByIndexBean {
88
+ public int r ;
89
+ public int a ;
89
90
90
- @ JsonCreator
91
- public BeanForStrictOrdering (@ JsonProperty ("c" ) int c , @ JsonProperty ("a" ) int a ) { //b and a are out of order, although alphabetic = true
92
- this .a = a ;
93
- this .c = c ;
94
- }
91
+ @ JsonProperty (index = 1 )
92
+ public int b ;
95
93
96
- public int getA () { return a ; }
97
- public int getB () { return b ; }
98
- public int getC () { return c ; }
94
+ @ JsonProperty (index = 0 )
95
+ public int u ;
96
+
97
+ public int f ;
99
98
}
100
99
101
100
// For [databind#2879]
@@ -115,19 +114,21 @@ public BeanFor2879(@JsonProperty("a") int a,
115
114
}
116
115
}
117
116
118
- // We'll expect ordering of "FUBAR"
119
- @ JsonPropertyOrder ({ "f" })
120
- static class OrderingByIndexBean {
121
- public int r ;
122
- public int a ;
123
-
124
- @ JsonProperty (index = 1 )
125
- public int b ;
117
+ // For [databind#2879]
118
+ static class BeanForStrictOrdering {
119
+ private final int a ;
120
+ private int b ;
121
+ private final int c ;
126
122
127
- @ JsonProperty (index = 0 )
128
- public int u ;
123
+ @ JsonCreator
124
+ public BeanForStrictOrdering (@ JsonProperty ("c" ) int c , @ JsonProperty ("a" ) int a ) { //b and a are out of order, although alphabetic = true
125
+ this .a = a ;
126
+ this .c = c ;
127
+ }
129
128
130
- public int f ;
129
+ public int getA () { return a ; }
130
+ public int getB () { return b ; }
131
+ public int getC () { return c ; }
131
132
}
132
133
133
134
/*
@@ -142,11 +143,6 @@ static class OrderingByIndexBean {
142
143
.configure (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY , true )
143
144
.build ();
144
145
145
- private final ObjectMapper STRICT_ALPHA_MAPPER = jsonMapperBuilder ()
146
- .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
147
- .enable (MapperFeature .STRICT_PROPERTIES_ORDERING )
148
- .build ();
149
-
150
146
public void testImplicitOrderByCreator () throws Exception {
151
147
assertEquals ("{\" c\" :1,\" a\" :2,\" b\" :0}" ,
152
148
MAPPER .writeValueAsString (new BeanWithCreator (1 , 2 )));
@@ -209,9 +205,24 @@ public void testOrderByIndexEtc() throws Exception
209
205
ALPHA_MAPPER .writeValueAsString (new OrderingByIndexBean ()));
210
206
}
211
207
208
+ // [databind#2879]: allow preventing Creator properties from overriding
209
+ // alphabetic ordering
212
210
public void testStrictAlphaAndCreatorOrdering () throws Exception
213
211
{
214
- String json = STRICT_ALPHA_MAPPER .writeValueAsString (new BeanForStrictOrdering (1 , 2 ));
215
- assertEquals ("{\" a\" :2,\" b\" :0,\" c\" :1}" , json );
212
+ // without changing defaults, creators are sorted before other properties
213
+ // BUT are sorted within their own category
214
+ assertTrue (ALPHA_MAPPER .isEnabled (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY ));
215
+ assertTrue (ALPHA_MAPPER .isEnabled (MapperFeature .SORT_CREATOR_PROPERTIES_FIRST ));
216
+ assertEquals (a2q ("{'a':3,'c':2,'b':0}" ),
217
+ ALPHA_MAPPER .writeValueAsString (new BeanForStrictOrdering (2 , 3 )));
218
+
219
+ // but can change that
220
+ final ObjectMapper STRICT_ALPHA_MAPPER = jsonMapperBuilder ()
221
+ .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
222
+ .disable (MapperFeature .SORT_CREATOR_PROPERTIES_FIRST )
223
+ .build ();
224
+
225
+ assertEquals (a2q ("{'a':2,'b':0,'c':1}" ),
226
+ STRICT_ALPHA_MAPPER .writeValueAsString (new BeanForStrictOrdering (1 , 2 )));
216
227
}
217
228
}
0 commit comments