From dc658751a040ed2ad66bfb8d1c05532f61d6926d Mon Sep 17 00:00:00 2001 From: Mikhael Sokolov Date: Tue, 15 Aug 2023 18:44:04 +0400 Subject: [PATCH] Treat kotlin's `kotlin.Deprecated` as a deprecated operation --- .../swagger/v3/core/util/KotlinDetector.java | 29 +++++++++++++++++++ .../java/io/swagger/v3/jaxrs2/Reader.java | 7 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 modules/swagger-core/src/main/java/io/swagger/v3/core/util/KotlinDetector.java diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/KotlinDetector.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/KotlinDetector.java new file mode 100644 index 0000000000..a861fd89d7 --- /dev/null +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/KotlinDetector.java @@ -0,0 +1,29 @@ +package io.swagger.v3.core.util; + +import java.lang.annotation.Annotation; + +public class KotlinDetector { + private static final Boolean kotlinAvailable; + private static final Class kotlinDeprecated; + + static { + kotlinAvailable = loadByClassOrNull("kotlin.Metadata") != null; + kotlinDeprecated = loadByClassOrNull("kotlin.Deprecated"); + } + + private static Class loadByClassOrNull(String className) { + try { + return (Class) ReflectionUtils.loadClassByName(className); + } catch (ClassNotFoundException ex) { + return null; + } + } + + public static boolean isKotlinPresent() { + return kotlinAvailable; + } + + public static Class getKotlinDeprecated() { + return kotlinDeprecated; + } +} diff --git a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java index 6b92eb7c2b..e7396b9f46 100644 --- a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java +++ b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java @@ -12,6 +12,7 @@ import io.swagger.v3.core.util.AnnotationsUtils; import io.swagger.v3.core.util.Json; import io.swagger.v3.core.util.Json31; +import io.swagger.v3.core.util.KotlinDetector; import io.swagger.v3.core.util.ParameterProcessor; import io.swagger.v3.core.util.PathUtils; import io.swagger.v3.core.util.ReflectionUtils; @@ -305,7 +306,8 @@ public OpenAPI read(Class cls, javax.ws.rs.Consumes classConsumes = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Consumes.class); javax.ws.rs.Produces classProduces = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Produces.class); - boolean classDeprecated = ReflectionUtils.getAnnotation(cls, Deprecated.class) != null; + boolean classDeprecated = ReflectionUtils.getAnnotation(cls, Deprecated.class) != null + || (KotlinDetector.isKotlinPresent() && ReflectionUtils.getAnnotation(cls, KotlinDetector.getKotlinDeprecated()) != null); // OpenApiDefinition OpenAPIDefinition openAPIDefinition = ReflectionUtils.getAnnotation(cls, OpenAPIDefinition.class); @@ -434,7 +436,8 @@ public OpenAPI read(Class cls, continue; } - boolean methodDeprecated = ReflectionUtils.getAnnotation(method, Deprecated.class) != null; + boolean methodDeprecated = ReflectionUtils.getAnnotation(method, Deprecated.class) != null + || (KotlinDetector.isKotlinPresent() && ReflectionUtils.getAnnotation(method, KotlinDetector.getKotlinDeprecated()) != null); javax.ws.rs.Path methodPath = ReflectionUtils.getAnnotation(method, javax.ws.rs.Path.class);