@Test public void testProcess() { SwaggerGenerator swaggerGenerator = new SwaggerGenerator(Mockito.mock(SwaggerGeneratorContext.class), null); swaggerDefinitionProcessor.process(SwaggerTestTarget.class.getAnnotation(SwaggerDefinition.class), swaggerGenerator); Swagger swagger = swaggerGenerator.getSwagger(); assertEquals(1, swagger.getTags().size()); io.swagger.models.Tag tag = swagger.getTags().get(0); assertEquals("testTag", tag.getName()); assertEquals("desc", tag.getDescription()); assertEquals("testValue", tag.getExternalDocs().getDescription()); assertEquals("testUrl", tag.getExternalDocs().getUrl()); assertEquals("127.0.0.1", swagger.getHost()); assertThat(swagger.getSchemes(), contains(io.swagger.models.Scheme.HTTP, io.swagger.models.Scheme.HTTPS)); io.swagger.models.Info info = swagger.getInfo(); assertEquals("title", info.getTitle()); assertEquals("version", info.getVersion()); assertEquals("desc", info.getDescription()); assertEquals("contactName", info.getContact().getName()); assertEquals("licenseName", info.getLicense().getName()); }
public AnnotationProcessor(final StandardContext context) { servletContext = context.getServletContext(); classLoader = servletContext.getClassLoader(); try { pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName()); consumesClass = (Class<Consumes>) classLoader.loadClass(Consumes.class.getName()); producesClass = (Class<Produces>) classLoader.loadClass(Produces.class.getName()); apiClazz= (Class<SwaggerDefinition>)classLoader.loadClass((SwaggerDefinition.class.getName())); infoClass = (Class<io.swagger.annotations.Info>)classLoader .loadClass((io.swagger.annotations.Info.class.getName())); tagClass = (Class<io.swagger.annotations.Tag>)classLoader .loadClass((io.swagger.annotations.Tag.class.getName())); extensionClass = (Class<io.swagger.annotations.Extension>)classLoader .loadClass((io.swagger.annotations.Extension.class.getName())); extensionPropertyClass = (Class<io.swagger.annotations.ExtensionProperty>)classLoader .loadClass(io.swagger.annotations.ExtensionProperty.class.getName()); scopeClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scope>) classLoader .loadClass(org.wso2.carbon.apimgt.annotations.api.Scope.class.getName()); scopesClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scopes>) classLoader .loadClass(org.wso2.carbon.apimgt.annotations.api.Scopes.class.getName()); apiOperation = (Class<io.swagger.annotations.ApiOperation>)classLoader .loadClass((io.swagger.annotations.ApiOperation.class.getName())); } catch (ClassNotFoundException e) { log.error("An error has occurred while loading classes ", e); } }
private Map<String, Tag> scanClasspathForTags() { Map<String, Tag> tags = new HashMap<String, Tag>(); for (Class<?> aClass: new Reflections("").getTypesAnnotatedWith(SwaggerDefinition.class)) { SwaggerDefinition swaggerDefinition = AnnotationUtils.findAnnotation(aClass, SwaggerDefinition.class); for (io.swagger.annotations.Tag tag : swaggerDefinition.tags()) { String tagName = tag.name(); if (!tagName.isEmpty()) { tags.put(tag.name(), new Tag().name(tag.name()).description(tag.description())); } } } return tags; }
@Override public void process(Object annotation, SwaggerGenerator swaggerGenerator) { SwaggerDefinition definitionAnnotation = (SwaggerDefinition) annotation; Swagger swagger = swaggerGenerator.getSwagger(); swaggerGenerator.setBasePath(definitionAnnotation.basePath()); swagger.setHost(definitionAnnotation.host()); convertConsumes(definitionAnnotation, swagger); convertProduces(definitionAnnotation, swagger); convertSchemes(definitionAnnotation, swagger); convertTags(definitionAnnotation, swagger); convertInfo(definitionAnnotation.info(), swagger); swagger.setExternalDocs(convertExternalDocs(definitionAnnotation.externalDocs())); }
private void convertProduces(SwaggerDefinition definitionAnnotation, Swagger swagger) { String[] produces = definitionAnnotation.produces(); if (produces == null) { return; } for (String produce : produces) { if (!StringUtils.isEmpty(produce)) { swagger.addProduces(produce); } } }
private void convertConsumes(SwaggerDefinition definitionAnnotation, Swagger swagger) { String[] consumes = definitionAnnotation.consumes(); if (consumes == null) { return; } for (String consume : consumes) { if (!StringUtils.isEmpty(consume)) { swagger.addConsumes(consume); } } }
protected void initClassAnnotationMgr() { classAnnotationMgr.register(Api.class, new ApiProcessor()); classAnnotationMgr.register(SwaggerDefinition.class, new SwaggerDefinitionProcessor()); classAnnotationMgr.register(ApiImplicitParams.class, new ApiImplicitParamsClassProcessor()); classAnnotationMgr.register(ApiImplicitParam.class, new ApiImplicitParamClassProcessor()); classAnnotationMgr.register(ApiResponses.class, new ApiResponsesClassProcessor()); classAnnotationMgr.register(ApiResponse.class, new ApiResponseClassProcessor()); }
@Test public void testHasAnnotation() { Assert.assertEquals(true, ClassUtils.hasAnnotation(TestClassUtils.class, SwaggerDefinition.class)); Assert.assertEquals(true, ClassUtils.hasAnnotation(TestClassUtils.class, Test.class)); Assert.assertEquals(false, ClassUtils.hasAnnotation(TestClassUtils.class, Path.class)); }
public SwaggerHandler( final Class<? extends Application> applicationClass) { final ApplicationPath annotation = applicationClass.getAnnotation(ApplicationPath.class); if (annotation != null) { baseUri = URI.create(annotation.value()).normalize(); } else { baseUri = URI.create("/"); } Application application; try { application = applicationClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new ExceptionInInitializerError(e); } swagger = new ClonableSwagger(); final Reader swaggerReader = new Reader(swagger); final Set<Class<?>> resourceClasses = application.getClasses(); if (resourceClasses.isEmpty()) { final String packageName = applicationClass.getPackage().getName(); final Reflections reflections = new Reflections(packageName); reflections.getTypesAnnotatedWith(Api.class).forEach(swaggerReader::read); reflections.getTypesAnnotatedWith(SwaggerDefinition.class).forEach(swaggerReader::read); } else { swaggerReader.read(applicationClass); resourceClasses.forEach(swaggerReader::read); } }
private boolean isAnnotated(Class<?> clz) { if (clz.getAnnotation(Api.class) != null) { return true; } if (clz.getAnnotation(SwaggerDefinition.class) != null) { return true; } if (clz.getAnnotation(javax.ws.rs.Path.class) != null) { return true; } return false; }
public AnnotationProcessor(final StandardContext context) { servletContext = context.getServletContext(); classLoader = servletContext.getClassLoader(); try { pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName()); consumesClass = (Class<Consumes>) classLoader.loadClass(Consumes.class.getName()); producesClass = (Class<Produces>) classLoader.loadClass(Produces.class.getName()); apiClazz= (Class<SwaggerDefinition>)classLoader.loadClass((SwaggerDefinition.class.getName())); apiOperation = (Class<io.swagger.annotations.ApiOperation>)classLoader .loadClass((io.swagger.annotations.ApiOperation.class.getName())); authorizationClass = (Class<io.swagger.annotations.Authorization>)classLoader .loadClass((io.swagger.annotations.Authorization.class.getName())); authorizationScopeClass = (Class<io.swagger.annotations.AuthorizationScope>)classLoader .loadClass((io.swagger.annotations.AuthorizationScope.class.getName())); extensionClass = (Class<io.swagger.annotations.Extension>)classLoader .loadClass((io.swagger.annotations.Extension.class.getName())); extensionPropertyClass = (Class<io.swagger.annotations.ExtensionProperty>)classLoader .loadClass(io.swagger.annotations.ExtensionProperty.class.getName()); scopeClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scope>) classLoader .loadClass(org.wso2.carbon.apimgt.annotations.api.Scope.class.getName()); scopesClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scopes>) classLoader .loadClass(org.wso2.carbon.apimgt.annotations.api.Scopes.class.getName()); } catch (ClassNotFoundException e) { log.error("An error has occurred while loading classes ", e); } }
/** * Scans a single class for Swagger annotations - does not invoke ReaderListeners */ public Swagger read(Class<?> cls) { SwaggerDefinition swaggerDefinition = cls.getAnnotation(SwaggerDefinition.class); if (swaggerDefinition != null) { readSwaggerConfig(cls, swaggerDefinition); } return read(cls, "", null, false, new String[0], new String[0], new HashMap<>(), new ArrayList<Parameter>(), new HashSet<Class<?>>()); }
private Scheme convertScheme(io.swagger.annotations.SwaggerDefinition.Scheme annotationScheme) { return Scheme.forValue(annotationScheme.name()); }
private void setBasePathFromAnnotation() { for (Class<?> aClass : getValidClasses(SwaggerDefinition.class)) { SwaggerDefinition swaggerDefinition = AnnotationUtils.findAnnotation(aClass, SwaggerDefinition.class); basePath = emptyToNull(swaggerDefinition.basePath()); } }
private void setHostFromAnnotation() { for (Class<?> aClass : getValidClasses(SwaggerDefinition.class)) { SwaggerDefinition swaggerDefinition = AnnotationUtils.findAnnotation(aClass, SwaggerDefinition.class); host = emptyToNull(swaggerDefinition.host()); } }