|
|
@@ -18,12 +18,14 @@
|
|
|
package org.apache.ambari.swagger;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
|
+import org.apache.ambari.annotations.SwaggerOverwriteNestedAPI;
|
|
|
import org.apache.ambari.annotations.SwaggerPreferredParent;
|
|
|
import org.apache.maven.plugin.logging.Log;
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -44,6 +46,7 @@ import io.swagger.models.Swagger;
|
|
|
import io.swagger.models.Tag;
|
|
|
import io.swagger.models.parameters.Parameter;
|
|
|
import io.swagger.models.parameters.PathParameter;
|
|
|
+import io.swagger.models.properties.StringProperty;
|
|
|
|
|
|
/**
|
|
|
* Customized {@link com.github.kongchen.swagger.docgen.reader.ClassSwaggerReader} implementation to
|
|
|
@@ -95,6 +98,8 @@ public class AmbariSwaggerReader extends JaxrsReader {
|
|
|
else {
|
|
|
boolean skipAdd = false;
|
|
|
Class<?> preferredParentClass = cls;
|
|
|
+ String parentApiPath;
|
|
|
+ String methodPathAsString = methodPath.value();
|
|
|
|
|
|
// API is a nested API of multiple top level APIs
|
|
|
if (nestedAPIs.containsKey(returnType)) {
|
|
|
@@ -128,9 +133,20 @@ public class AmbariSwaggerReader extends JaxrsReader {
|
|
|
nestedAPIs.remove(returnType);
|
|
|
}
|
|
|
|
|
|
+ // API parent customization by @SwaggerOverwriteNestedAPI
|
|
|
+ SwaggerOverwriteNestedAPI swaggerOverwriteNestedAPI = AnnotationUtils.findAnnotation(returnType,
|
|
|
+ SwaggerOverwriteNestedAPI.class);
|
|
|
+ if (null != swaggerOverwriteNestedAPI) {
|
|
|
+ preferredParentClass = swaggerOverwriteNestedAPI.parentApi();
|
|
|
+ parentApiPath = swaggerOverwriteNestedAPI.parentApiPath();
|
|
|
+ methodPathAsString = swaggerOverwriteNestedAPI.parentMethodPath();
|
|
|
+ } else {
|
|
|
+ parentApiPath = validateParentApiPath(preferredParentClass);
|
|
|
+ }
|
|
|
+
|
|
|
logger.info("Registering nested API: {}", returnType);
|
|
|
- NestedApiRecord nar = new NestedApiRecord(returnType, preferredParentClass,
|
|
|
- validateParentApiPath(preferredParentClass), method, methodPath.value());
|
|
|
+ NestedApiRecord nar = new NestedApiRecord(returnType, preferredParentClass, parentApiPath, method,
|
|
|
+ methodPathAsString);
|
|
|
nestedAPIs.put(returnType, nar);
|
|
|
}
|
|
|
}
|
|
|
@@ -168,13 +184,27 @@ public class AmbariSwaggerReader extends JaxrsReader {
|
|
|
NestedApiRecord nestedApiRecord = nestedAPIs.get(cls);
|
|
|
if (null != nestedApiRecord) {
|
|
|
logger.info("Processing nested API: {}", nestedApiRecord);
|
|
|
- // Get the path parameters of the parent API method. All methods of the nested API class should include these
|
|
|
- // parameters.
|
|
|
- Operation operation = parseMethod(nestedApiRecord.parentMethod);
|
|
|
- List<Parameter> pathParameters = ImmutableList.copyOf(
|
|
|
+ List<Parameter> pathParameters = new ArrayList<>();
|
|
|
+ SwaggerOverwriteNestedAPI swaggerOverwriteNestedAPI = AnnotationUtils.findAnnotation(nestedApiRecord.nestedApi,
|
|
|
+ SwaggerOverwriteNestedAPI.class);
|
|
|
+ if (null != swaggerOverwriteNestedAPI) {
|
|
|
+ logger.info("Will use path params from @SwaggerOverwriteNestedAPI: {}", (Object[]) swaggerOverwriteNestedAPI
|
|
|
+ .pathParameters());
|
|
|
+ for (String param : swaggerOverwriteNestedAPI.pathParameters()) {
|
|
|
+ PathParameter pathParam = new PathParameter();
|
|
|
+ pathParam.setName(param);
|
|
|
+ pathParam.setType(StringProperty.TYPE);
|
|
|
+ pathParameters.add(pathParam);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // Get the path parameters of the parent API method. All methods of the nested API class should include these
|
|
|
+ // parameters.
|
|
|
+ Operation operation = parseMethod(nestedApiRecord.parentMethod);
|
|
|
+ pathParameters = ImmutableList.copyOf(
|
|
|
Collections2.filter(operation.getParameters(), Predicates.instanceOf(PathParameter.class)));
|
|
|
- logger.info("Will copy path params from parent method: {}",
|
|
|
+ logger.info("Will copy path params from parent method: {}",
|
|
|
Lists.transform(pathParameters, new ParameterToName()));
|
|
|
+ }
|
|
|
return super.read(cls,
|
|
|
joinPaths(nestedApiRecord.parentApiPath, nestedApiRecord.parentMethodPath, parentPath),
|
|
|
parentMethod, readHidden,
|