Search in sources :

Example 1 with ReaderExtension

use of io.swagger.servlet.extensions.ReaderExtension 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)

Aggregations

SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 Response (io.swagger.models.Response)1 ReaderExtension (io.swagger.servlet.extensions.ReaderExtension)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1