Skip to content

Commit

Permalink
Treat kotlin's kotlin.Deprecated as a deprecated operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhael Sokolov authored and frantuma committed Nov 21, 2023
1 parent 2fbef29 commit 03976ef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2016 SmartBear Software
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.swagger.util;

import java.lang.annotation.Annotation;

public class KotlinDetector {
private static final Boolean kotlinAvailable;
private static final Class<? extends Annotation> kotlinDeprecated;

static {
kotlinAvailable = loadByClassOrNull("kotlin.Metadata") != null;
kotlinDeprecated = loadByClassOrNull("kotlin.Deprecated");
}

private static <T> Class<T> loadByClassOrNull(String className) {
try {
return (Class<T>) ReflectionUtils.loadClassByName(className);
} catch (ClassNotFoundException ex) {
return null;
}
}

public static boolean isKotlinPresent() {
return kotlinAvailable;
}

public static Class<? extends Annotation> getKotlinDeprecated() {
return kotlinDeprecated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.util.BaseReaderUtils;
import io.swagger.util.Json;
import io.swagger.util.KotlinDetector;
import io.swagger.util.ParameterProcessor;
import io.swagger.util.PathUtils;
import io.swagger.util.ReflectionUtils;
Expand Down Expand Up @@ -917,7 +918,8 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
addResponse(operation, apiResponse, jsonViewAnnotation);
}

if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) {
if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null
|| (KotlinDetector.isKotlinPresent() && ReflectionUtils.getAnnotation(method, KotlinDetector.getKotlinDeprecated()) != null)) {
operation.setDeprecated(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.servlet.ReaderContext;
import io.swagger.util.BaseReaderUtils;
import io.swagger.util.KotlinDetector;
import io.swagger.util.ParameterProcessor;
import io.swagger.util.PathUtils;
import io.swagger.util.ReflectionUtils;
Expand Down Expand Up @@ -271,7 +272,8 @@ public void applySchemes(ReaderContext context, Operation operation, Method meth

@Override
public void setDeprecated(Operation operation, Method method) {
if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) {
if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null
|| (KotlinDetector.isKotlinPresent() && ReflectionUtils.getAnnotation(method, KotlinDetector.getKotlinDeprecated()) != null)) {
operation.deprecated(true);
}
}
Expand Down

0 comments on commit 03976ef

Please sign in to comment.