下面列出了io.swagger.annotations.Info#io.swagger.annotations.SwaggerDefinition 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void process(SwaggerGenerator swaggerGenerator, SwaggerDefinition definitionAnnotation) {
Swagger swagger = swaggerGenerator.getSwagger();
if (StringUtils.isNotEmpty(definitionAnnotation.basePath())) {
swaggerGenerator.setBasePath(definitionAnnotation.basePath());
}
if (StringUtils.isNotEmpty(definitionAnnotation.host())) {
swagger.setHost(definitionAnnotation.host());
}
SwaggerUtils.setConsumes(swagger, definitionAnnotation.consumes());
SwaggerUtils.setProduces(swagger, definitionAnnotation.produces());
convertSchemes(definitionAnnotation, swagger);
convertTags(definitionAnnotation, swagger);
convertInfo(definitionAnnotation.info(), swagger);
swagger.setExternalDocs(convertExternalDocs(definitionAnnotation.externalDocs()));
}
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<>();
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;
}
@Test
public void testGetInfo0VendorExtensions() {
Map<String, Object> logo = new HashMap<String, Object>();
logo.put("logo", "logo url");
logo.put("description", "This is our logo.");
Map<String, Object> website = new HashMap<String, Object>();
website.put("website", "website url");
website.put("description", "This is our website.");
Map<String, Object> expectedExtensions = new HashMap<String, Object>();
expectedExtensions.put("x-logo", logo);
expectedExtensions.put("x-website", website);
Set<Class<?>> validClasses = Sets.newHashSet(ApiSourceTestClass.class);
ApiSource apiSource = spy(ApiSource.class);
when(apiSource.getValidClasses(SwaggerDefinition.class)).thenReturn(validClasses);
Info info = apiSource.getInfo();
Map<String, Object> vendorExtensions = info.getVendorExtensions();
Assert.assertEquals(vendorExtensions.size(), 2);
Assert.assertEquals(vendorExtensions, expectedExtensions);
}
/**
* 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, new LinkedHashMap<String, Tag>(), new ArrayList<Parameter>(), new HashSet<Class<?>>());
}
@Test
public void testHasAnnotation() {
Assert.assertTrue(SwaggerUtils.hasAnnotation(TestClassUtils.class, SwaggerDefinition.class));
Assert.assertTrue(SwaggerUtils.hasAnnotation(TestClassUtils.class, Test.class));
Assert.assertFalse(SwaggerUtils.hasAnnotation(TestClassUtils.class, Path.class));
}
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<?>>());
}
@Test
public void testGetExternalDocsNoneFound() {
// given
@SwaggerDefinition
class TestClassNoExternalDocs { }
ApiSource apiSource = spy(ApiSource.class);
when(apiSource.getValidClasses(SwaggerDefinition.class)).thenReturn(Sets.newHashSet(TestClassNoExternalDocs.class));
// when
ExternalDocs externalDocs = apiSource.getExternalDocs();
// then
Assert.assertNull(externalDocs);
}
@Override
public Type getProcessType() {
return SwaggerDefinition.class;
}
private Scheme convertScheme(io.swagger.annotations.SwaggerDefinition.Scheme annotationScheme) {
if (SwaggerDefinition.Scheme.DEFAULT.equals(annotationScheme)) {
return Scheme.HTTP;
}
return Scheme.forValue(annotationScheme.name());
}
private void read(ReaderContext context) {
final SwaggerDefinition swaggerDefinition = context.getCls().getAnnotation(SwaggerDefinition.class);
if (swaggerDefinition != null) {
readSwaggerConfig(swaggerDefinition);
}
for (Method method : context.getCls().getMethods()) {
if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
continue;
}
final Operation operation = new Operation();
String operationPath = null;
String httpMethod = null;
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
// Avoid java.util.ServiceLoader mechanism which finds ServletReaderExtension
// for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
for (ReaderExtension extension : getExtensions()) {
if (operationPath == null) {
operationPath = extension.getPath(context, method);
}
if (httpMethod == null) {
httpMethod = extension.getHttpMethod(context, method);
}
if (operationPath == null || httpMethod == null) {
continue;
}
if (extension.isReadable(context)) {
extension.setDeprecated(operation, method);
extension.applyConsumes(context, operation, method);
extension.applyProduces(context, operation, method);
extension.applyOperationId(operation, method);
extension.applySummary(operation, method);
extension.applyDescription(operation, method);
extension.applySchemes(context, operation, method);
extension.applySecurityRequirements(context, operation, method);
extension.applyTags(context, operation, method);
extension.applyResponses(context, operation, method);
extension.applyImplicitParameters(context, operation, method);
for (int i = 0; i < genericParameterTypes.length; i++) {
extension.applyParameters(context, operation, genericParameterTypes[i], paramAnnotations[i]);
}
}
}
if (httpMethod != null && operationPath != null) {
if (operation.getResponses() == null) {
operation.defaultResponse(new Response().description("OK"));
}
else {
for (String k : operation.getResponses().keySet()) {
if (k.equals("200")) {
Response response = operation.getResponses().get(k);
if ("successful operation".equals(response.getDescription()))
response.setDescription("OK");
}
}
}
final Map<String,String> regexMap = new HashMap<>();
final String parsedPath = PathUtils.parsePath(operationPath, regexMap);
if (parsedPath != null) {
// check for curly path params
for (String seg : parsedPath.split("/")) {
if (seg.startsWith("{") && seg.endsWith("}")) {
String segName = seg.substring(1, seg.length() - 1);
boolean declared = false;
for (Parameter opParam : operation.getParameters()) {
if ("path".equals(opParam.getIn()) && segName.equals(opParam.getName())) {
declared = true;
break;
}
}
if (!declared) {
// add it for free
PathParameter pathParam = new PathParameter();
pathParam.setName(segName);
pathParam.setRequired(false);
pathParam.setDefaultValue("");
operation.parameter(pathParam);
}
}
}
}
Path path = swagger.getPath(parsedPath);
if (path == null) {
path = new Path();
swagger.path(parsedPath, path);
}
path.set(httpMethod.toLowerCase(), operation);
}
}
}
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());
}
}