@Override public Swagger read(Set<Class<?>> classes) throws GenerateException { // relate all methods to one base request mapping if multiple controllers exist for that mapping // get all methods from each controller & find their request mapping // create map - resource string (after first slash) as key, new SpringResource as value Map<String, SpringResource> resourceMap = generateResourceMap(classes); for (String str : resourceMap.keySet()) { SpringResource resource = resourceMap.get(str); read(resource); } return swagger; }
// Helper method for loadDocuments() private Map<String, SpringResource> analyzeController( Class<?> clazz, Map<String, SpringResource> resourceMap, String description) throws ClassNotFoundException { String controllerCanonicalName = clazz.getCanonicalName(); String[] controllerRequestMappingValues = null; // Determine if we will use class-level requestmapping or dummy string if (clazz.getAnnotation(RequestMapping.class) != null && clazz.getAnnotation(RequestMapping.class).value() != null) { controllerRequestMappingValues = clazz.getAnnotation(RequestMapping.class).value(); } else { controllerRequestMappingValues = new String[1]; controllerRequestMappingValues[0] = ""; } // Iterate over all value attributes of the class-level RequestMapping annotation for (int i = 0; i < controllerRequestMappingValues.length; i++) { // Iterate over all methods inside the controller Method[] methods = clazz.getMethods(); for (Method method : methods) { RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class); // Look for method-level @RequestMapping annotation if (methodRequestMapping instanceof RequestMapping) { RequestMethod[] requestMappingRequestMethods = methodRequestMapping.method(); // For each method-level @RequestMapping annotation, iterate over HTTP Verb for (RequestMethod requestMappingRequestMethod : requestMappingRequestMethods) { String[] methodRequestMappingValues = methodRequestMapping.value(); // Check for cases where method-level @RequestMapping#value is not set, and use the // controllers @RequestMapping if (methodRequestMappingValues == null || methodRequestMappingValues.length == 0) { // The map key is a concat of the following: // 1. The controller package // 2. The controller class name // 3. The controller-level @RequestMapping#value String resourceKey = controllerCanonicalName + controllerRequestMappingValues[i] + requestMappingRequestMethod; if ((!(resourceMap.containsKey(resourceKey)))) { resourceMap.put( resourceKey, new SpringResource( clazz, controllerRequestMappingValues[i], resourceKey, description)); } resourceMap.get(resourceKey).addMethod(method); } else { // Here we know that method-level @RequestMapping#value is populated, so // iterate over all the @RequestMapping#value attributes, and add them to the resource // map. for (String methodRequestMappingValue : methodRequestMappingValues) { String resourceName = methodRequestMappingValue; // The map key is a concat of the following: // 1. The controller package // 2. The controller class name // 3. The controller-level @RequestMapping#value // 4. The method-level @RequestMapping#value // 5. The method-level @RequestMapping#method String resourceKey = controllerCanonicalName + controllerRequestMappingValues[i] + resourceName + requestMappingRequestMethod; if (!(resourceName.equals(""))) { if ((!(resourceMap.containsKey(resourceKey)))) { resourceMap.put( resourceKey, new SpringResource(clazz, resourceName, resourceKey, description)); } resourceMap.get(resourceKey).addMethod(method); } } } } } } } clazz.getFields(); clazz .getDeclaredFields(); // <--In case developer declares a field without an associated // getter/setter. // this will allow NoClassDefFoundError to be caught before it triggers bamboo failure. return resourceMap; }
private Operation parseMethod(Method method) { Operation operation = new Operation(); RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); Class<?> responseClass = null; List<String> produces = new ArrayList<String>(); List<String> consumes = new ArrayList<String>(); String responseContainer = null; String operationId = method.getName(); Map<String, Property> defaultResponseHeaders = null; Set<Map<String, Object>> customExtensions = null; ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); if (apiOperation.hidden()) return null; if (!"".equals(apiOperation.nickname())) operationId = apiOperation.nickname(); defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders()); operation.summary(apiOperation.value()).description(apiOperation.notes()); customExtensions = parseCustomExtensions(apiOperation.extensions()); if (customExtensions != null) { for (Map<String, Object> extension : customExtensions) { if (extension != null) { for (Map.Entry<String, Object> map : extension.entrySet()) { operation.setVendorExtension( map.getKey().startsWith("x-") ? map.getKey() : "x-" + map.getKey(), map.getValue()); } } } } if (apiOperation.response() != null && !Void.class.equals(apiOperation.response())) responseClass = apiOperation.response(); if (!"".equals(apiOperation.responseContainer())) responseContainer = apiOperation.responseContainer(); /// security if (apiOperation.authorizations() != null) { List<SecurityRequirement> securities = new ArrayList<SecurityRequirement>(); for (Authorization auth : apiOperation.authorizations()) { if (auth.value() != null && !"".equals(auth.value())) { SecurityRequirement security = new SecurityRequirement(); security.setName(auth.value()); AuthorizationScope[] scopes = auth.scopes(); for (AuthorizationScope scope : scopes) { if (scope.scope() != null && !"".equals(scope.scope())) { security.addScope(scope.scope()); } } securities.add(security); } } if (securities.size() > 0) { for (SecurityRequirement sec : securities) operation.security(sec); } } if (responseClass == null) { // pick out response from method declaration LOG.info("picking up response class from method " + method); Type t = method.getGenericReturnType(); responseClass = method.getReturnType(); if (responseClass.equals(ResponseEntity.class)) { responseClass = getGenericSubtype(method.getReturnType(), method.getGenericReturnType()); } if (!responseClass.equals(Void.class) && !"void".equals(responseClass.toString()) && responseClass.getAnnotation(Api.class) == null) { LOG.info("reading model " + responseClass); Map<String, Model> models = ModelConverters.getInstance().readAll(t); } } if (responseClass != null && !responseClass.equals(Void.class) && !responseClass.equals(ResponseEntity.class) && responseClass.getAnnotation(Api.class) == null) { if (isPrimitive(responseClass)) { Property responseProperty = null; Property property = ModelConverters.getInstance().readAsProperty(responseClass); if (property != null) { if ("list".equalsIgnoreCase(responseContainer)) responseProperty = new ArrayProperty(property); else if ("map".equalsIgnoreCase(responseContainer)) responseProperty = new MapProperty(property); else responseProperty = property; operation.response( 200, new Response() .description("successful operation") .schema(responseProperty) .headers(defaultResponseHeaders)); } } else if (!responseClass.equals(Void.class) && !"void".equals(responseClass.toString())) { Map<String, Model> models = ModelConverters.getInstance().read(responseClass); if (models.size() == 0) { Property pp = ModelConverters.getInstance().readAsProperty(responseClass); operation.response( 200, new Response() .description("successful operation") .schema(pp) .headers(defaultResponseHeaders)); } for (String key : models.keySet()) { Property responseProperty = null; if ("list".equalsIgnoreCase(responseContainer)) responseProperty = new ArrayProperty(new RefProperty().asDefault(key)); else if ("map".equalsIgnoreCase(responseContainer)) responseProperty = new MapProperty(new RefProperty().asDefault(key)); else responseProperty = new RefProperty().asDefault(key); operation.response( 200, new Response() .description("successful operation") .schema(responseProperty) .headers(defaultResponseHeaders)); swagger.model(key, models.get(key)); } models = ModelConverters.getInstance().readAll(responseClass); for (String key : models.keySet()) { swagger.model(key, models.get(key)); } } } operation.operationId(operationId); if (requestMapping.produces() != null) { for (String str : Arrays.asList(requestMapping.produces())) { if (!produces.contains(str)) { produces.add(str); } } } if (requestMapping.consumes() != null) { for (String str : Arrays.asList(requestMapping.consumes())) { if (!consumes.contains(str)) { consumes.add(str); } } } ApiResponses responseAnnotation = method.getAnnotation(ApiResponses.class); if (responseAnnotation != null) { updateApiResponse(operation, responseAnnotation); } else { ResponseStatus responseStatus = method.getAnnotation(ResponseStatus.class); if (responseStatus != null) { operation.response( responseStatus.value().value(), new Response().description(responseStatus.reason())); } } boolean isDeprecated = false; Deprecated annotation = method.getAnnotation(Deprecated.class); if (annotation != null) isDeprecated = true; boolean hidden = false; if (apiOperation != null) hidden = apiOperation.hidden(); // process parameters Class[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); // paramTypes = method.getParameterTypes // genericParamTypes = method.getGenericParameterTypes for (int i = 0; i < parameterTypes.length; i++) { Type type = genericParameterTypes[i]; List<Annotation> annotations = Arrays.asList(paramAnnotations[i]); List<Parameter> parameters = getParameters(type, annotations); for (Parameter parameter : parameters) { operation.parameter(parameter); } } if (operation.getResponses() == null) { operation.defaultResponse(new Response().description("successful operation")); } // Process @ApiImplicitParams this.readImplicitParameters(method, operation); return operation; }