5
5
import java .lang .reflect .Method ;
6
6
import java .lang .reflect .Modifier ;
7
7
import java .lang .reflect .Type ;
8
+ import java .util .LinkedHashSet ;
9
+ import java .util .Set ;
8
10
9
11
import com .fasterxml .jackson .core .*;
10
12
import com .fasterxml .jackson .databind .*;
11
13
import com .fasterxml .jackson .databind .annotation .JacksonStdImpl ;
12
14
import com .fasterxml .jackson .databind .jsonFormatVisitors .JsonFormatVisitable ;
13
15
import com .fasterxml .jackson .databind .jsonFormatVisitors .JsonFormatVisitorWrapper ;
16
+ import com .fasterxml .jackson .databind .jsonFormatVisitors .JsonStringFormatVisitor ;
14
17
import com .fasterxml .jackson .databind .jsonschema .SchemaAware ;
15
18
import com .fasterxml .jackson .databind .jsontype .TypeSerializer ;
16
19
import com .fasterxml .jackson .databind .ser .BeanSerializer ;
@@ -246,19 +249,34 @@ public JsonNode getSchema(SerializerProvider provider, Type typeHint)
246
249
}
247
250
return com .fasterxml .jackson .databind .jsonschema .JsonSchema .getDefaultSchemaNode ();
248
251
}
249
-
252
+
250
253
@ Override
251
254
public void acceptJsonFormatVisitor (JsonFormatVisitorWrapper visitor , JavaType typeHint )
252
255
throws JsonMappingException
253
256
{
257
+ /* 27-Apr-2015, tatu: First things first; for JSON Schema introspection,
258
+ * Enums are special, and unfortunately we will need to add special
259
+ * handling here (see https://github.com/FasterXML/jackson-module-jsonSchema/issues/57
260
+ * for details).
261
+ */
262
+ Class <?> decl = (typeHint == null ) ? null : typeHint .getRawClass ();
263
+ if (decl == null ) {
264
+ decl = _accessorMethod .getDeclaringClass ();
265
+ }
266
+ if ((decl != null ) && (decl .isEnum ())) {
267
+ if (_acceptJsonFormatVisitorForEnum (visitor , typeHint , decl )) {
268
+ return ;
269
+ }
270
+ }
271
+
254
272
JsonSerializer <Object > ser = _valueSerializer ;
255
273
if (ser == null ) {
256
274
if (typeHint == null ) {
257
275
if (_property != null ) {
258
276
typeHint = _property .getType ();
259
277
}
260
278
if (typeHint == null ) {
261
- typeHint = visitor .getProvider ().constructType (_accessorMethod . getReturnType () );
279
+ typeHint = visitor .getProvider ().constructType (_handledType );
262
280
}
263
281
}
264
282
ser = visitor .getProvider ().findTypedValueSerializer (typeHint , false , _property );
@@ -270,6 +288,43 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
270
288
ser .acceptJsonFormatVisitor (visitor , null );
271
289
}
272
290
291
+ /**
292
+ * Overridable helper method used for special case handling of schema information for
293
+ * Enums
294
+ *
295
+ * @return True if method handled callbacks; false if not; in latter case caller will
296
+ * send default callbacks
297
+ *
298
+ * @since 2.6
299
+ */
300
+ protected boolean _acceptJsonFormatVisitorForEnum (JsonFormatVisitorWrapper visitor ,
301
+ JavaType typeHint , Class <?> enumType )
302
+ throws JsonMappingException
303
+ {
304
+ // Copied from EnumSerializer#acceptJsonFormatVisitor
305
+ JsonStringFormatVisitor stringVisitor = visitor .expectStringFormat (typeHint );
306
+ if (stringVisitor != null ) {
307
+ Set <String > enums = new LinkedHashSet <String >();
308
+ for (Object en : enumType .getEnumConstants ()) {
309
+ try {
310
+ enums .add (String .valueOf (_accessorMethod .invoke (en )));
311
+ } catch (Exception e ) {
312
+ Throwable t = e ;
313
+ while (t instanceof InvocationTargetException && t .getCause () != null ) {
314
+ t = t .getCause ();
315
+ }
316
+ if (t instanceof Error ) {
317
+ throw (Error ) t ;
318
+ }
319
+ throw JsonMappingException .wrapWithPath (t , en , _accessorMethod .getName () + "()" );
320
+ }
321
+ }
322
+ stringVisitor .enumTypes (enums );
323
+ }
324
+ return true ;
325
+
326
+ }
327
+
273
328
protected boolean isNaturalTypeWithStdHandling (Class <?> rawType , JsonSerializer <?> ser )
274
329
{
275
330
// First: do we have a natural type being handled?
0 commit comments