io.swagger.annotations.SwaggerDefinition Java Examples

The following examples show how to use io.swagger.annotations.SwaggerDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwaggerDefinitionProcessor.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@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()));
}
 
Example #2
Source File: AnnotationProcessor.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: JaxrsReader.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: ApiSourceTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: Reader.java    From dorado with Apache License 2.0 5 votes vote down vote up
/**
 * 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<?>>());
}
 
Example #6
Source File: TestClassUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #7
Source File: AnnotationProcessor.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #8
Source File: ExtendedSwaggerReader.java    From msf4j with Apache License 2.0 5 votes vote down vote up
/**
 * 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<?>>());
}
 
Example #9
Source File: ApiSourceTest.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: SwaggerDefinitionProcessor.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Type getProcessType() {
  return SwaggerDefinition.class;
}
 
Example #11
Source File: SwaggerDefinitionProcessor.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
private Scheme convertScheme(io.swagger.annotations.SwaggerDefinition.Scheme annotationScheme) {
  if (SwaggerDefinition.Scheme.DEFAULT.equals(annotationScheme)) {
    return Scheme.HTTP;
  }
  return Scheme.forValue(annotationScheme.name());
}
 
Example #12
Source File: SwaggerAnnotationsReader.java    From mdw with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}
 
Example #13
Source File: ApiSource.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void setBasePathFromAnnotation() {
    for (Class<?> aClass : getValidClasses(SwaggerDefinition.class)) {
        SwaggerDefinition swaggerDefinition = AnnotationUtils.findAnnotation(aClass, SwaggerDefinition.class);
        basePath = emptyToNull(swaggerDefinition.basePath());
    }
}
 
Example #14
Source File: ApiSource.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void setHostFromAnnotation() {
    for (Class<?> aClass : getValidClasses(SwaggerDefinition.class)) {
        SwaggerDefinition swaggerDefinition = AnnotationUtils.findAnnotation(aClass, SwaggerDefinition.class);
        host = emptyToNull(swaggerDefinition.host());
    }
}