Skip to content

Commit 4dcc2cc

Browse files
skel2007frantuma
authored andcommitted
add support to exclude and override mappings to primitive types
1 parent 0dd7052 commit 4dcc2cc

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

modules/swagger-core/src/main/java/io/swagger/util/PrimitiveType.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.util.Collections;
88
import java.util.HashMap;
99
import java.util.Map;
10+
import java.util.Set;
1011
import java.util.TreeMap;
12+
import java.util.concurrent.ConcurrentHashMap;
1113

1214
/**
1315
* The <code>PrimitiveType</code> enumeration defines a mapping of limited set
@@ -175,6 +177,24 @@ public ObjectProperty createProperty() {
175177
* Joda lib.
176178
*/
177179
private static final Map<String, PrimitiveType> EXTERNAL_CLASSES;
180+
181+
/**
182+
* Allows to exclude specific classes from KEY_CLASSES mappings to primitive
183+
*
184+
*/
185+
private static Set<String> customExcludedClasses = ConcurrentHashMap.newKeySet();
186+
187+
/**
188+
* Allows to exclude specific classes from EXTERNAL_CLASSES mappings to primitive
189+
*
190+
*/
191+
private static Set<String> customExcludedExternalClasses = ConcurrentHashMap.newKeySet();
192+
193+
/**
194+
* Adds support for custom mapping of classes to primitive types
195+
*/
196+
private static Map<String, PrimitiveType> customClasses = new ConcurrentHashMap<>();
197+
178198
/**
179199
* Alternative names for primitive types that have to be supported for
180200
* backward compatibility.
@@ -212,7 +232,7 @@ public ObjectProperty createProperty() {
212232
addKeys(externalClasses, DATE_TIME, "org.joda.time.DateTime", "org.joda.time.ReadableDateTime",
213233
"javax.xml.datatype.XMLGregorianCalendar", "java.time.LocalDateTime", "java.time.ZonedDateTime",
214234
"java.time.OffsetDateTime");
215-
addKeys(externalClasses, LONG, "java.time.Instant");
235+
addKeys(externalClasses, LONG, "java.time.Instant");
216236
EXTERNAL_CLASSES = Collections.unmodifiableMap(externalClasses);
217237

218238
final Map<String, PrimitiveType> names = new TreeMap<String, PrimitiveType>(String.CASE_INSENSITIVE_ORDER);
@@ -236,15 +256,50 @@ private PrimitiveType(Class<?> keyClass, String commonName) {
236256
this.commonName = commonName;
237257
}
238258

259+
/**
260+
* Adds support for custom mapping of classes to primitive types
261+
*
262+
* @return Set of custom classes to primitive type
263+
*/
264+
public static Set<String> customExcludedClasses() {
265+
return customExcludedClasses;
266+
}
267+
268+
/**
269+
* Adds support for custom mapping of classes to primitive types
270+
*
271+
* @return Set of custom classes to primitive type
272+
*/
273+
public static Set<String> customExcludedExternalClasses() {
274+
return customExcludedExternalClasses;
275+
}
276+
277+
/**
278+
* Adds support for custom mapping of classes to primitive types
279+
*
280+
* @return Map of custom classes to primitive type
281+
*/
282+
public static Map<String, PrimitiveType> customClasses() {
283+
return customClasses;
284+
}
285+
239286
public static PrimitiveType fromType(Type type) {
240287
final Class<?> raw = TypeFactory.defaultInstance().constructType(type).getRawClass();
241288
final PrimitiveType key = KEY_CLASSES.get(raw);
242289
if (key != null) {
243-
return key;
290+
if (!customExcludedClasses.contains(raw.getName())) {
291+
return key;
292+
}
293+
}
294+
final PrimitiveType custom = customClasses.get(raw.getName());
295+
if (custom != null) {
296+
return custom;
244297
}
245298
final PrimitiveType external = EXTERNAL_CLASSES.get(raw.getName());
246299
if (external != null) {
247-
return external;
300+
if (!customExcludedExternalClasses().contains(raw.getName())) {
301+
return external;
302+
}
248303
}
249304
for (Map.Entry<Class<?>, PrimitiveType> entry : BASE_CLASSES.entrySet()) {
250305
if (entry.getKey().isAssignableFrom(raw)) {

0 commit comments

Comments
 (0)