1 package io.swagger.v3.core.jackson;
2
3 //import io.swagger.annotations.ApiModel;
4
5 import com.fasterxml.jackson.databind.JavaType;
6 import io.swagger.v3.core.util.AnnotationsUtils;
7 import io.swagger.v3.core.util.PrimitiveType;
8 import org.apache.commons.lang3.StringUtils;
9 import org.apache.commons.lang3.text.WordUtils;
10
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.EnumSet;
14 import java.util.Set;
15
16 /**
17  * Helper class used for converting well-known (property) types into
18  * Swagger type names.
19  */

20 public class TypeNameResolver {
21     public final static TypeNameResolver std = new TypeNameResolver();
22
23     protected TypeNameResolver() {
24     }
25
26     public String nameForType(JavaType type, Options... options) {
27         return nameForType(type, options.length == 0 ? Collections.<Options>emptySet() :
28                 EnumSet.copyOf(Arrays.asList(options)));
29     }
30
31     public String nameForType(JavaType type, Set<Options> options) {
32         if (type.hasGenericTypes()) {
33             return nameForGenericType(type, options);
34         }
35         final String name = findStdName(type);
36         return (name == null) ? nameForClass(type, options) : name;
37     }
38
39     protected String nameForClass(JavaType type, Set<Options> options) {
40         return nameForClass(type.getRawClass(), options);
41     }
42
43     protected String nameForClass(Class<?> cls, Set<Options> options) {
44         if (options.contains(Options.SKIP_API_MODEL)) {
45             return cls.getSimpleName();
46         }
47
48         io.swagger.v3.oas.annotations.media.Schema mp = AnnotationsUtils.getSchemaDeclaredAnnotation(cls);
49
50         final String modelName = mp == null ? null : StringUtils.trimToNull(mp.name());
51         return modelName == null ? cls.getSimpleName() : modelName;
52     }
53
54     protected String nameForGenericType(JavaType type, Set<Options> options) {
55         final StringBuilder generic = new StringBuilder(nameForClass(type, options));
56         final int count = type.containedTypeCount();
57         for (int i = 0; i < count; ++i) {
58             final JavaType arg = type.containedType(i);
59             final String argName = PrimitiveType.fromType(arg) != null ? nameForClass(arg, options) :
60                     nameForType(arg, options);
61             generic.append(WordUtils.capitalize(argName));
62         }
63         return generic.toString();
64     }
65
66     protected String findStdName(JavaType type) {
67         return PrimitiveType.getCommonName(type);
68     }
69
70     public enum Options {
71         SKIP_API_MODEL;
72     }
73 }
74