Search in sources :

Example 1 with SwaggerDefinition

use of io.swagger.annotations.SwaggerDefinition in project swagger-core by swagger-api.

the class Reader method read.

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();
        for (ReaderExtension extension : ReaderExtensions.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);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(context, operation, genericParameterTypes[i], paramAnnotations[i]);
                }
            }
        }
        if (httpMethod != null) {
            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("successful operation"));
            }
            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);
            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new Path();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Operation(io.swagger.models.Operation) ReaderExtension(io.swagger.servlet.extensions.ReaderExtension) Response(io.swagger.models.Response) Type(java.lang.reflect.Type) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 2 with SwaggerDefinition

use of io.swagger.annotations.SwaggerDefinition in project vertx-swagger by bobxwang.

the class Reader method readSwaggerConfig.

private void readSwaggerConfig(SwaggerDefinition config) {
    readInfoConfig(config);
    if (StringUtils.isNotBlank(config.basePath())) {
        swagger.setBasePath(config.basePath());
    }
    if (StringUtils.isNotBlank(config.host())) {
        swagger.setHost(config.host());
    }
    for (String consume : config.consumes()) {
        if (StringUtils.isNotBlank(consume)) {
            swagger.addConsumes(consume);
        }
    }
    for (String produce : config.produces()) {
        if (StringUtils.isNotBlank(produce)) {
            swagger.addProduces(produce);
        }
    }
    if (StringUtils.isNotBlank(config.externalDocs().value())) {
        ExternalDocs externalDocs = swagger.getExternalDocs();
        if (externalDocs == null) {
            externalDocs = new ExternalDocs();
            swagger.setExternalDocs(externalDocs);
        }
        externalDocs.setDescription(config.externalDocs().value());
        if (StringUtils.isNotBlank(config.externalDocs().url())) {
            externalDocs.setUrl(config.externalDocs().url());
        }
    }
    for (OAuth2Definition oAuth2Config : config.securityDefinition().oAuth2Definitions()) {
        io.swagger.models.auth.OAuth2Definition oAuth2Definition = new io.swagger.models.auth.OAuth2Definition();
        OAuth2Definition.Flow flow = oAuth2Config.flow();
        if (flow.equals(OAuth2Definition.Flow.ACCESS_CODE)) {
            oAuth2Definition = oAuth2Definition.accessCode(oAuth2Config.authorizationUrl(), oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.APPLICATION)) {
            oAuth2Definition = oAuth2Definition.application(oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.IMPLICIT)) {
            oAuth2Definition = oAuth2Definition.implicit(oAuth2Config.authorizationUrl());
        } else {
            oAuth2Definition = oAuth2Definition.password(oAuth2Config.tokenUrl());
        }
        for (Scope scope : oAuth2Config.scopes()) {
            oAuth2Definition.addScope(scope.name(), scope.description());
        }
        oAuth2Definition.setDescription(oAuth2Config.description());
        swagger.addSecurityDefinition(oAuth2Config.key(), oAuth2Definition);
    }
    for (ApiKeyAuthDefinition[] apiKeyAuthConfigs : new ApiKeyAuthDefinition[][] { config.securityDefinition().apiKeyAuthDefintions(), config.securityDefinition().apiKeyAuthDefinitions() }) {
        for (ApiKeyAuthDefinition apiKeyAuthConfig : apiKeyAuthConfigs) {
            io.swagger.models.auth.ApiKeyAuthDefinition apiKeyAuthDefinition = new io.swagger.models.auth.ApiKeyAuthDefinition();
            apiKeyAuthDefinition.setName(apiKeyAuthConfig.name());
            apiKeyAuthDefinition.setIn(In.forValue(apiKeyAuthConfig.in().toValue()));
            apiKeyAuthDefinition.setDescription(apiKeyAuthConfig.description());
            swagger.addSecurityDefinition(apiKeyAuthConfig.key(), apiKeyAuthDefinition);
        }
    }
    for (BasicAuthDefinition[] basicAuthConfigs : new BasicAuthDefinition[][] { config.securityDefinition().basicAuthDefinions(), config.securityDefinition().basicAuthDefinitions() }) {
        for (BasicAuthDefinition basicAuthConfig : basicAuthConfigs) {
            io.swagger.models.auth.BasicAuthDefinition basicAuthDefinition = new io.swagger.models.auth.BasicAuthDefinition();
            basicAuthDefinition.setDescription(basicAuthConfig.description());
            swagger.addSecurityDefinition(basicAuthConfig.key(), basicAuthDefinition);
        }
    }
    for (io.swagger.annotations.Tag tagConfig : config.tags()) {
        if (StringUtils.isNotBlank(tagConfig.name())) {
            final Tag tag = new Tag();
            tag.setName(tagConfig.name());
            tag.setDescription(tagConfig.description());
            if (StringUtils.isNotBlank(tagConfig.externalDocs().value())) {
                tag.setExternalDocs(new ExternalDocs(tagConfig.externalDocs().value(), tagConfig.externalDocs().url()));
            }
            tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
            swagger.addTag(tag);
        }
    }
    for (SwaggerDefinition.Scheme scheme : config.schemes()) {
        if (scheme != SwaggerDefinition.Scheme.DEFAULT) {
            swagger.addScheme(Scheme.forValue(scheme.name()));
        }
    }
}
Also used : OAuth2Definition(io.swagger.annotations.OAuth2Definition) BasicAuthDefinition(io.swagger.annotations.BasicAuthDefinition) ExternalDocs(io.swagger.models.ExternalDocs) ApiKeyAuthDefinition(io.swagger.annotations.ApiKeyAuthDefinition) Scope(io.swagger.annotations.Scope) Tag(io.swagger.models.Tag) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 3 with SwaggerDefinition

use of io.swagger.annotations.SwaggerDefinition in project swagger-core by swagger-api.

the class Reader method readSwaggerConfig.

protected void readSwaggerConfig(Class<?> cls, SwaggerDefinition config) {
    if (!config.basePath().isEmpty()) {
        swagger.setBasePath(config.basePath());
    }
    if (!config.host().isEmpty()) {
        swagger.setHost(config.host());
    }
    readInfoConfig(config);
    for (String consume : config.consumes()) {
        if (StringUtils.isNotEmpty(consume)) {
            swagger.addConsumes(consume);
        }
    }
    for (String produce : config.produces()) {
        if (StringUtils.isNotEmpty(produce)) {
            swagger.addProduces(produce);
        }
    }
    for (OAuth2Definition oAuth2Config : config.securityDefinition().oAuth2Definitions()) {
        io.swagger.models.auth.OAuth2Definition oAuth2Definition = new io.swagger.models.auth.OAuth2Definition();
        OAuth2Definition.Flow flow = oAuth2Config.flow();
        if (flow.equals(OAuth2Definition.Flow.ACCESS_CODE)) {
            oAuth2Definition = oAuth2Definition.accessCode(oAuth2Config.authorizationUrl(), oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.APPLICATION)) {
            oAuth2Definition = oAuth2Definition.application(oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.IMPLICIT)) {
            oAuth2Definition = oAuth2Definition.implicit(oAuth2Config.authorizationUrl());
        } else {
            oAuth2Definition = oAuth2Definition.password(oAuth2Config.tokenUrl());
        }
        for (Scope scope : oAuth2Config.scopes()) {
            oAuth2Definition.addScope(scope.name(), scope.description());
        }
        oAuth2Definition.setDescription(oAuth2Config.description());
        swagger.addSecurityDefinition(oAuth2Config.key(), oAuth2Definition);
    }
    for (ApiKeyAuthDefinition[] apiKeyAuthConfigs : new ApiKeyAuthDefinition[][] { config.securityDefinition().apiKeyAuthDefintions(), config.securityDefinition().apiKeyAuthDefinitions() }) {
        for (ApiKeyAuthDefinition apiKeyAuthConfig : apiKeyAuthConfigs) {
            io.swagger.models.auth.ApiKeyAuthDefinition apiKeyAuthDefinition = new io.swagger.models.auth.ApiKeyAuthDefinition();
            apiKeyAuthDefinition.setName(apiKeyAuthConfig.name());
            apiKeyAuthDefinition.setIn(In.forValue(apiKeyAuthConfig.in().toValue()));
            apiKeyAuthDefinition.setDescription(apiKeyAuthConfig.description());
            swagger.addSecurityDefinition(apiKeyAuthConfig.key(), apiKeyAuthDefinition);
        }
    }
    for (BasicAuthDefinition[] basicAuthConfigs : new BasicAuthDefinition[][] { config.securityDefinition().basicAuthDefinions(), config.securityDefinition().basicAuthDefinitions() }) {
        for (BasicAuthDefinition basicAuthConfig : basicAuthConfigs) {
            io.swagger.models.auth.BasicAuthDefinition basicAuthDefinition = new io.swagger.models.auth.BasicAuthDefinition();
            basicAuthDefinition.setDescription(basicAuthConfig.description());
            swagger.addSecurityDefinition(basicAuthConfig.key(), basicAuthDefinition);
        }
    }
    if (!config.externalDocs().value().isEmpty()) {
        ExternalDocs externalDocs = swagger.getExternalDocs();
        if (externalDocs == null) {
            externalDocs = new ExternalDocs();
            swagger.setExternalDocs(externalDocs);
        }
        externalDocs.setDescription(config.externalDocs().value());
        if (!config.externalDocs().url().isEmpty()) {
            externalDocs.setUrl(config.externalDocs().url());
        }
    }
    for (io.swagger.annotations.Tag tagConfig : config.tags()) {
        if (!tagConfig.name().isEmpty()) {
            Tag tag = new Tag();
            tag.setName(tagConfig.name());
            tag.setDescription(tagConfig.description());
            if (!tagConfig.externalDocs().value().isEmpty()) {
                tag.setExternalDocs(new ExternalDocs(tagConfig.externalDocs().value(), tagConfig.externalDocs().url()));
            }
            tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
            swagger.addTag(tag);
        }
    }
    for (SwaggerDefinition.Scheme scheme : config.schemes()) {
        if (scheme != SwaggerDefinition.Scheme.DEFAULT) {
            swagger.addScheme(Scheme.forValue(scheme.name()));
        }
    }
}
Also used : OAuth2Definition(io.swagger.annotations.OAuth2Definition) BasicAuthDefinition(io.swagger.annotations.BasicAuthDefinition) ExternalDocs(io.swagger.models.ExternalDocs) ApiKeyAuthDefinition(io.swagger.annotations.ApiKeyAuthDefinition) AuthorizationScope(io.swagger.annotations.AuthorizationScope) Scope(io.swagger.annotations.Scope) Tag(io.swagger.models.Tag) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 4 with SwaggerDefinition

use of io.swagger.annotations.SwaggerDefinition in project swagger-core by swagger-api.

the class Reader method read.

/**
     * Scans a set of classes for both ReaderListeners and Swagger annotations. All found listeners will
     * be instantiated before any of the classes are scanned for Swagger annotations - so they can be invoked
     * accordingly.
     *
     * @param classes a set of classes to scan
     * @return the generated Swagger definition
     */
public Swagger read(Set<Class<?>> classes) {
    Set<Class<?>> sortedClasses = new TreeSet<Class<?>>(new Comparator<Class<?>>() {

        @Override
        public int compare(Class<?> class1, Class<?> class2) {
            if (class1.equals(class2)) {
                return 0;
            } else if (class1.isAssignableFrom(class2)) {
                return -1;
            } else if (class2.isAssignableFrom(class1)) {
                return 1;
            }
            return class1.getName().compareTo(class2.getName());
        }
    });
    sortedClasses.addAll(classes);
    Map<Class<?>, ReaderListener> listeners = new HashMap<Class<?>, ReaderListener>();
    for (Class<?> cls : sortedClasses) {
        if (ReaderListener.class.isAssignableFrom(cls) && !listeners.containsKey(cls)) {
            try {
                listeners.put(cls, (ReaderListener) cls.newInstance());
            } catch (Exception e) {
                LOGGER.error("Failed to create ReaderListener", e);
            }
        }
    }
    for (ReaderListener listener : listeners.values()) {
        try {
            listener.beforeScan(this, swagger);
        } catch (Exception e) {
            LOGGER.error("Unexpected error invoking beforeScan listener [" + listener.getClass().getName() + "]", e);
        }
    }
    // process SwaggerDefinitions first - so we get tags in desired order
    for (Class<?> cls : sortedClasses) {
        SwaggerDefinition swaggerDefinition = cls.getAnnotation(SwaggerDefinition.class);
        if (swaggerDefinition != null) {
            readSwaggerConfig(cls, swaggerDefinition);
        }
    }
    for (Class<?> cls : sortedClasses) {
        read(cls, "", null, false, new String[0], new String[0], new LinkedHashMap<String, Tag>(), new ArrayList<Parameter>(), new HashSet<Class<?>>());
    }
    for (ReaderListener listener : listeners.values()) {
        try {
            listener.afterScan(this, swagger);
        } catch (Exception e) {
            LOGGER.error("Unexpected error invoking afterScan listener [" + listener.getClass().getName() + "]", e);
        }
    }
    return swagger;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter) Tag(io.swagger.models.Tag) ReaderListener(io.swagger.jaxrs.config.ReaderListener) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 5 with SwaggerDefinition

use of io.swagger.annotations.SwaggerDefinition in project swagger-core by swagger-api.

the class Reader method readSwaggerConfig.

private void readSwaggerConfig(SwaggerDefinition config) {
    readInfoConfig(config);
    if (StringUtils.isNotBlank(config.basePath())) {
        swagger.setBasePath(config.basePath());
    }
    if (StringUtils.isNotBlank(config.host())) {
        swagger.setHost(config.host());
    }
    for (String consume : config.consumes()) {
        if (StringUtils.isNotBlank(consume)) {
            swagger.addConsumes(consume);
        }
    }
    for (String produce : config.produces()) {
        if (StringUtils.isNotBlank(produce)) {
            swagger.addProduces(produce);
        }
    }
    if (StringUtils.isNotBlank(config.externalDocs().value())) {
        ExternalDocs externalDocs = swagger.getExternalDocs();
        if (externalDocs == null) {
            externalDocs = new ExternalDocs();
            swagger.setExternalDocs(externalDocs);
        }
        externalDocs.setDescription(config.externalDocs().value());
        if (StringUtils.isNotBlank(config.externalDocs().url())) {
            externalDocs.setUrl(config.externalDocs().url());
        }
    }
    for (OAuth2Definition oAuth2Config : config.securityDefinition().oAuth2Definitions()) {
        io.swagger.models.auth.OAuth2Definition oAuth2Definition = new io.swagger.models.auth.OAuth2Definition();
        OAuth2Definition.Flow flow = oAuth2Config.flow();
        if (flow.equals(OAuth2Definition.Flow.ACCESS_CODE)) {
            oAuth2Definition = oAuth2Definition.accessCode(oAuth2Config.authorizationUrl(), oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.APPLICATION)) {
            oAuth2Definition = oAuth2Definition.application(oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.IMPLICIT)) {
            oAuth2Definition = oAuth2Definition.implicit(oAuth2Config.authorizationUrl());
        } else {
            oAuth2Definition = oAuth2Definition.password(oAuth2Config.tokenUrl());
        }
        for (Scope scope : oAuth2Config.scopes()) {
            oAuth2Definition.addScope(scope.name(), scope.description());
        }
        oAuth2Definition.setDescription(oAuth2Config.description());
        swagger.addSecurityDefinition(oAuth2Config.key(), oAuth2Definition);
    }
    for (ApiKeyAuthDefinition[] apiKeyAuthConfigs : new ApiKeyAuthDefinition[][] { config.securityDefinition().apiKeyAuthDefintions(), config.securityDefinition().apiKeyAuthDefinitions() }) {
        for (ApiKeyAuthDefinition apiKeyAuthConfig : apiKeyAuthConfigs) {
            io.swagger.models.auth.ApiKeyAuthDefinition apiKeyAuthDefinition = new io.swagger.models.auth.ApiKeyAuthDefinition();
            apiKeyAuthDefinition.setName(apiKeyAuthConfig.name());
            apiKeyAuthDefinition.setIn(In.forValue(apiKeyAuthConfig.in().toValue()));
            apiKeyAuthDefinition.setDescription(apiKeyAuthConfig.description());
            swagger.addSecurityDefinition(apiKeyAuthConfig.key(), apiKeyAuthDefinition);
        }
    }
    for (BasicAuthDefinition[] basicAuthConfigs : new BasicAuthDefinition[][] { config.securityDefinition().basicAuthDefinions(), config.securityDefinition().basicAuthDefinitions() }) {
        for (BasicAuthDefinition basicAuthConfig : basicAuthConfigs) {
            io.swagger.models.auth.BasicAuthDefinition basicAuthDefinition = new io.swagger.models.auth.BasicAuthDefinition();
            basicAuthDefinition.setDescription(basicAuthConfig.description());
            swagger.addSecurityDefinition(basicAuthConfig.key(), basicAuthDefinition);
        }
    }
    for (io.swagger.annotations.Tag tagConfig : config.tags()) {
        if (StringUtils.isNotBlank(tagConfig.name())) {
            final Tag tag = new Tag();
            tag.setName(tagConfig.name());
            tag.setDescription(tagConfig.description());
            if (StringUtils.isNotBlank(tagConfig.externalDocs().value())) {
                tag.setExternalDocs(new ExternalDocs(tagConfig.externalDocs().value(), tagConfig.externalDocs().url()));
            }
            tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
            swagger.addTag(tag);
        }
    }
    for (SwaggerDefinition.Scheme scheme : config.schemes()) {
        if (scheme != SwaggerDefinition.Scheme.DEFAULT) {
            swagger.addScheme(Scheme.forValue(scheme.name()));
        }
    }
}
Also used : OAuth2Definition(io.swagger.annotations.OAuth2Definition) BasicAuthDefinition(io.swagger.annotations.BasicAuthDefinition) ExternalDocs(io.swagger.models.ExternalDocs) ApiKeyAuthDefinition(io.swagger.annotations.ApiKeyAuthDefinition) Scope(io.swagger.annotations.Scope) Tag(io.swagger.models.Tag) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Aggregations

SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)8 Tag (io.swagger.models.Tag)4 ApiKeyAuthDefinition (io.swagger.annotations.ApiKeyAuthDefinition)3 BasicAuthDefinition (io.swagger.annotations.BasicAuthDefinition)3 OAuth2Definition (io.swagger.annotations.OAuth2Definition)3 Scope (io.swagger.annotations.Scope)3 ExternalDocs (io.swagger.models.ExternalDocs)3 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 Response (io.swagger.models.Response)2 Swagger (io.swagger.models.Swagger)2 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 AuthorizationScope (io.swagger.annotations.AuthorizationScope)1 ReaderListener (io.swagger.jaxrs.config.ReaderListener)1 FormParameter (io.swagger.models.parameters.FormParameter)1 HeaderParameter (io.swagger.models.parameters.HeaderParameter)1 Parameter (io.swagger.models.parameters.Parameter)1