io.swagger.util.PathUtils Java Examples

The following examples show how to use io.swagger.util.PathUtils. 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: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public String getPath(ReaderContext context, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    String operationId = null == apiOperation ? ""
        : StringUtils.isBlank(apiOperation.nickname()) ? null : apiOperation.nickname();
    return PathUtils.collectPath(context.getParentPath(), context.getInterfaceCls().getName(),
        method.getName(), operationId);

}
 
Example #2
Source File: DubboReaderExtension.java    From swagger-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public String getPath(ReaderContext context, Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	String operationId = null == apiOperation ? ""
			: StringUtils.isBlank(apiOperation.nickname()) ? null : apiOperation.nickname();
	return PathUtils.collectPath(context.getParentPath(), context.getInterfaceCls().getName(),
			method.getName(), operationId);

}
 
Example #3
Source File: ControllerReaderExtension.java    From jboot with Apache License 2.0 5 votes vote down vote up
public String getPath(ReaderContext context, Method method) {
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final String operationPath = apiOperation == null ? null : apiOperation.nickname();
    return PathUtils.collectPath(context.getParentPath(),
            apiAnnotation == null ? null : apiAnnotation.value(),
            StringUtils.isBlank(operationPath) ? method.getName() : operationPath);
}
 
Example #4
Source File: Reader.java    From jboot with Apache License 2.0 4 votes vote down vote up
private void read(ReaderContext context) {

        for (Method method : context.getCls().getDeclaredMethods()) {
            if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
                continue;
            }
            final Operation operation = new Operation();


            final Type[] genericParameterTypes = method.getGenericParameterTypes();
            final Annotation[][] paramAnnotations = method.getParameterAnnotations();

            ControllerReaderExtension extension = new ControllerReaderExtension();

            String methodPath = "index".equals(method.getName()) ? "" : "/" + method.getName();
            String operationPath = JbootControllerManager.me().getPathByController((Class<? extends Controller>) context.getCls()) + methodPath;

            String 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(swagger, context, operation, method);
                extension.applyImplicitParameters(swagger, context, operation, method);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(httpMethod, context, operation, paramAnnotations[i]);
                }

                if ("post".equalsIgnoreCase(httpMethod) && operation.getConsumes() == null) {
                    operation.addConsumes("application/x-www-form-urlencoded");
                }
            }

            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 SwaggerPath();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
 
Example #5
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 #6
Source File: ResourceReaderExtension.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public String getPath(ReaderContext context, Method method) {
    String p = null;
    Api apiAnnotation = context.getCls().getAnnotation(Api.class);
    ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    String operationPath = apiOperation == null ? null : apiOperation.nickname();
    if (operationPath != null && !operationPath.isEmpty()) {
        // same logic as ServletReaderExtension
        p = PathUtils.collectPath(context.getParentPath(),
                apiAnnotation == null ? null : apiAnnotation.value(), operationPath);
    }
    else {
        // try JAX-RS annotations
        Path parentPath = ReflectionUtils.getAnnotation(method.getDeclaringClass(), Path.class);
        if (parentPath != null && parentPath.value() != null && !parentPath.value().isEmpty()) {
            p = parentPath.value();
        }
        Path path = ReflectionUtils.getAnnotation(method, Path.class);
        if (path != null && path.value() != null && !path.value().isEmpty()) {
            if (p == null)
                p = path.value();
            else {
                if (path.value().startsWith("/"))
                    p += path.value();
                else
                    p = p + "/" + path.value();
            }
        }
        // check dynamic java, which has package-based pathing
        java.lang.Package pkg = method.getDeclaringClass().getPackage();
        if (p != null && "MDW".equals(pkg.getImplementationTitle())) {
            if (p.startsWith("/"))
                p = "/" + pkg.getName().replace('.', '/') + p;
            else
                p = "/" + pkg.getName().replace('.', '/') + "/" + p;
        }

        if (apiOperation != null) {
            ApiImplicitParams implicitParams = ReflectionUtils.getAnnotation(method, ApiImplicitParams.class);
            if (implicitParams != null && implicitParams.value() != null && implicitParams.value().length == 1) {
                ApiImplicitParam implicitParam = implicitParams.value()[0];
                if (implicitParam.name() != null && !"body".equals(implicitParam.paramType()) && !"query".equals(implicitParam.paramType()) && !"header".equals(implicitParam.paramType()) && !"form".equals(implicitParam.paramType()))
                    p += "/{" + implicitParam.name() + "}";
            }
        }
    }

    return p;
}
 
Example #7
Source File: AbstractReader.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
protected String parseOperationPath(String operationPath, Map<String, String> regexMap) {
    return PathUtils.parsePath(operationPath, regexMap);
}