Java Code Examples for io.swagger.util.ReflectionUtils

The following examples show how to use io.swagger.util.ReflectionUtils. These examples are extracted from open source projects. 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 Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applyConsumes(ReaderContext context, Operation operation, Method method) {
    final List<String> consumes = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        consumes.addAll(parseStringValues(apiOperation.consumes()));
    }

    if (consumes.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            consumes.addAll(parseStringValues(apiAnnotation.consumes()));
        }
        consumes.addAll(context.getParentConsumes());
    }

    for (String consume : consumes) {
        operation.consumes(consume);
    }

}
 
Example 2
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applyProduces(ReaderContext context, Operation operation, Method method) {
    final List<String> produces = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        produces.addAll(parseStringValues(apiOperation.produces()));
    }

    if (produces.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            produces.addAll(parseStringValues(apiAnnotation.produces()));
        }
        produces.addAll(context.getParentProduces());
    }

    for (String produce : produces) {
        operation.produces(produce);
    }

}
 
Example 3
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applySchemes(ReaderContext context, Operation operation, Method method) {
    final List<Scheme> schemes = new ArrayList<Scheme>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        schemes.addAll(parseSchemes(apiOperation.protocols()));
    }

    if (schemes.isEmpty() && apiAnnotation != null) {
        schemes.addAll(parseSchemes(apiAnnotation.protocols()));
    }

    for (Scheme scheme : schemes) {
        operation.scheme(scheme);
    }

}
 
Example 4
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
private static Map<String, Property> parseResponseHeaders(ReaderContext context,
                                                          ResponseHeader[] headers) {
    Map<String, Property> responseHeaders = null;
    for (ResponseHeader header : headers) {
        final String name = header.name();
        if (StringUtils.isNotEmpty(name)) {
            if (responseHeaders == null) {
                responseHeaders = new HashMap<String, Property>();
            }
            final Class<?> cls = header.response();
            if (!ReflectionUtils.isVoid(cls)) {
                final Property property = ModelConverters.getInstance().readAsProperty(cls);
                if (property != null) {
                    final Property responseProperty = ContainerWrapper.wrapContainer(
                        header.responseContainer(), property, ContainerWrapper.ARRAY,
                        ContainerWrapper.LIST, ContainerWrapper.SET);
                    responseProperty.setDescription(header.description());
                    responseHeaders.put(name, responseProperty);
                    appendModels(context.getSwagger(), cls);
                }
            }
        }
    }
    return responseHeaders;
}
 
Example 5
Source Project: proteus   Source File: Reader.java    License: Apache License 2.0 6 votes vote down vote up
protected Parameter readImplicitParam(ApiImplicitParam param) {
    final Parameter p;
    if (param.paramType().equalsIgnoreCase("path")) {
        p = new PathParameter();
    } else if (param.paramType().equalsIgnoreCase("query")) {
        p = new QueryParameter();
    } else if (param.paramType().equalsIgnoreCase("form") || param.paramType().equalsIgnoreCase("formData")) {
        p = new FormParameter();
    } else if (param.paramType().equalsIgnoreCase("body")) {
        p = null;
    } else if (param.paramType().equalsIgnoreCase("header")) {
        p = new HeaderParameter();
    } else {
        LOGGER.warn("Unknown implicit parameter type: [{}]", param.paramType());
        return null;
    }
    final Type type = ReflectionUtils.typeFromString(param.dataType());
     return ParameterProcessor.applyAnnotations(swagger, p, (type == null) ? String.class : type,
            Arrays.<Annotation>asList(param));
}
 
Example 6
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applyConsumes(ReaderContext context, Operation operation, Method method) {
	final List<String> consumes = new ArrayList<String>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

	if (apiOperation != null) {
		consumes.addAll(parseStringValues(apiOperation.consumes()));
	}

	if (consumes.isEmpty()) {
		final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
		if (apiAnnotation != null) {
			consumes.addAll(parseStringValues(apiAnnotation.consumes()));
		}
		consumes.addAll(context.getParentConsumes());
	}

	for (String consume : consumes) {
		operation.consumes(consume);
	}

}
 
Example 7
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applyProduces(ReaderContext context, Operation operation, Method method) {
	final List<String> produces = new ArrayList<String>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

	if (apiOperation != null) {
		produces.addAll(parseStringValues(apiOperation.produces()));
	}

	if (produces.isEmpty()) {
		final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
		if (apiAnnotation != null) {
			produces.addAll(parseStringValues(apiAnnotation.produces()));
		}
		produces.addAll(context.getParentProduces());
	}

	for (String produce : produces) {
		operation.produces(produce);
	}

}
 
Example 8
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void applySchemes(ReaderContext context, Operation operation, Method method) {
	final List<Scheme> schemes = new ArrayList<Scheme>();
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

	if (apiOperation != null) {
		schemes.addAll(parseSchemes(apiOperation.protocols()));
	}

	if (schemes.isEmpty() && apiAnnotation != null) {
		schemes.addAll(parseSchemes(apiAnnotation.protocols()));
	}

	for (Scheme scheme : schemes) {
		operation.scheme(scheme);
	}

}
 
Example 9
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
private static Map<String, Property> parseResponseHeaders(ReaderContext context,
		ResponseHeader[] headers) {
	Map<String, Property> responseHeaders = null;
	for (ResponseHeader header : headers) {
		final String name = header.name();
		if (StringUtils.isNotEmpty(name)) {
			if (responseHeaders == null) {
				responseHeaders = new HashMap<String, Property>();
			}
			final Class<?> cls = header.response();
			if (!ReflectionUtils.isVoid(cls)) {
				final Property property = ModelConverters.getInstance().readAsProperty(cls);
				if (property != null) {
					final Property responseProperty = ContainerWrapper.wrapContainer(
							header.responseContainer(), property, ContainerWrapper.ARRAY,
							ContainerWrapper.LIST, ContainerWrapper.SET);
					responseProperty.setDescription(header.description());
					responseHeaders.put(name, responseProperty);
					appendModels(context.getSwagger(), cls);
				}
			}
		}
	}
	return responseHeaders;
}
 
Example 10
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
private static Map<String, Property> parseResponseHeaders(Swagger swagger, ReaderContext context, ResponseHeader[] headers) {
    Map<String, Property> responseHeaders = null;
    for (ResponseHeader header : headers) {
        final String name = header.name();
        if (StringUtils.isNotEmpty(name)) {
            if (responseHeaders == null) {
                responseHeaders = new HashMap<String, Property>();
            }
            final Class<?> cls = header.response();
            if (!ReflectionUtils.isVoid(cls)) {
                final Property property = ModelConverters.getInstance().readAsProperty(cls);
                if (property != null) {
                    final Property responseProperty = ContainerWrapper.wrapContainer(header.responseContainer(),
                            property, ContainerWrapper.ARRAY, ContainerWrapper.LIST, ContainerWrapper.SET);
                    responseProperty.setDescription(header.description());
                    responseHeaders.put(name, responseProperty);
                    appendModels(swagger, cls);
                }
            }
        }
    }
    return responseHeaders;
}
 
Example 11
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
public void applyConsumes(ReaderContext context, Operation operation, Method method) {
    final List<String> consumes = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        consumes.addAll(parseStringValues(apiOperation.consumes()));
    }

    if (consumes.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            consumes.addAll(parseStringValues(apiAnnotation.consumes()));
        }
        consumes.addAll(context.getParentConsumes());
    }

    for (String consume : consumes) {
        operation.consumes(consume);
    }
}
 
Example 12
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
public void applyProduces(ReaderContext context, Operation operation, Method method) {
    final List<String> produces = new ArrayList<String>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);

    if (apiOperation != null) {
        produces.addAll(parseStringValues(apiOperation.produces()));
    }

    if (produces.isEmpty()) {
        final Api apiAnnotation = context.getCls().getAnnotation(Api.class);
        if (apiAnnotation != null) {
            produces.addAll(parseStringValues(apiAnnotation.produces()));
        }
        produces.addAll(context.getParentProduces());
    }

    for (String produce : produces) {
        operation.produces(produce);
    }
}
 
Example 13
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
public void applySchemes(ReaderContext context, Operation operation, Method method) {
    final List<Scheme> schemes = new ArrayList<Scheme>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        schemes.addAll(parseSchemes(apiOperation.protocols()));
    }

    if (schemes.isEmpty() && apiAnnotation != null) {
        schemes.addAll(parseSchemes(apiAnnotation.protocols()));
    }

    for (Scheme scheme : schemes) {
        operation.scheme(scheme);
    }
}
 
Example 14
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 6 votes vote down vote up
public void applySecurityRequirements(ReaderContext context, Operation operation, Method method) {
    final List<SecurityRequirement> securityRequirements = new ArrayList<SecurityRequirement>();
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    final Api apiAnnotation = context.getCls().getAnnotation(Api.class);

    if (apiOperation != null) {
        securityRequirements.addAll(parseAuthorizations(apiOperation.authorizations()));
    }

    if (securityRequirements.isEmpty() && apiAnnotation != null) {
        securityRequirements.addAll(parseAuthorizations(apiAnnotation.authorizations()));
    }

    for (SecurityRequirement securityRequirement : securityRequirements) {
        operation.security(securityRequirement);
    }
}
 
Example 15
@Override
public Type getGenericType(ApiImplicitParam apiImplicitParam) {
  Type dataTypeClass = apiImplicitParam.dataTypeClass();
  if (ReflectionUtils.isVoid(dataTypeClass)) {
    if (StringUtils.isEmpty(apiImplicitParam.dataType())) {
      return null;
    }

    dataTypeClass = ReflectionUtils.typeFromString(apiImplicitParam.dataType());
  }

  if ("array".equals(apiImplicitParam.type())) {
    return Types.arrayOf(dataTypeClass);
  }

  return dataTypeClass;
}
 
Example 16
Source Project: dorado   Source File: ReaderUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Collects field-level parameters from class.
 *
 * @param cls     is a class for collecting
 * @param swagger is the instance of the Swagger
 * @return the collection of supported parameters
 */
public static List<Parameter> collectFieldParameters(Class<?> cls, Swagger swagger) {
    final List<Parameter> parameters = new ArrayList<Parameter>();
    for (Field field : ReflectionUtils.getDeclaredFields(cls)) {
        final List<Annotation> annotations = Arrays.asList(field.getAnnotations());
        final Type genericType = field.getGenericType();
        for (Parameter parameter : collectParameters(genericType, annotations)) {
            if (ParameterProcessor.applyAnnotations(swagger, parameter, genericType, annotations) != null) {
                parameters.add(parameter);
            }
        }
    }
    return parameters;
}
 
Example 17
Source Project: dorado   Source File: Reader.java    License: Apache License 2.0 5 votes vote down vote up
private void readExternalDocs(Method method, Operation operation) {
	io.swagger.annotations.ExternalDocs externalDocs = ReflectionUtils.getAnnotation(method,
			io.swagger.annotations.ExternalDocs.class);
	if (externalDocs != null) {
		operation.setExternalDocs(new ExternalDocs(externalDocs.value(), externalDocs.url()));
	}
}
 
Example 18
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: 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 19
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void applySummary(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.value())) {
        operation.summary(apiOperation.value());
    }

}
 
Example 20
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void applyDescription(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.notes())) {
        operation.description(apiOperation.notes());
    }
    operation.description(operation.getDescription() == null ? outputMethod(method)
        : (outputMethod(method) + operation.getDescription()));
}
 
Example 21
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
private static Type getResponseType(Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && !ReflectionUtils.isVoid(apiOperation.response())) {
        return apiOperation.response();
    } else {
        return method.getGenericReturnType();
    }
}
 
Example 22
Source Project: sofa-rpc   Source File: RpcReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Parameter readImplicitParam(Swagger swagger, ApiImplicitParam param) {
    PrimitiveType fromType = PrimitiveType.fromName(param.paramType());
    final Parameter p = null == fromType ? new FormParameter() : new QueryParameter();
    final Type type = ReflectionUtils.typeFromString(param.dataType());
    return ParameterProcessor.applyAnnotations(swagger, p, type == null ? String.class : type,
        Collections.<Annotation> singletonList(param));
}
 
Example 23
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: 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 24
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void applySummary(Operation operation, Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	if (apiOperation != null && StringUtils.isNotBlank(apiOperation.value())) {
		operation.summary(apiOperation.value());
	}

}
 
Example 25
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void applyDescription(Operation operation, Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	if (apiOperation != null && StringUtils.isNotBlank(apiOperation.notes())) {
		operation.description(apiOperation.notes());
	}
	operation.description(operation.getDescription() == null ? outputMethod(method)
			: (outputMethod(method) + operation.getDescription()));
}
 
Example 26
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
private static Type getResponseType(Method method) {
	final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
	if (apiOperation != null && !ReflectionUtils.isVoid(apiOperation.response())) {
		return apiOperation.response();
	} else {
		return method.getGenericReturnType();
	}
}
 
Example 27
Source Project: swagger-dubbo   Source File: DubboReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Parameter readImplicitParam(Swagger swagger, ApiImplicitParam param) {
	PrimitiveType fromType = PrimitiveType.fromName(param.paramType());
	final Parameter p = null == fromType ? new FormParameter() : new QueryParameter();
	final Type type = ReflectionUtils.typeFromString(param.dataType());
	return ParameterProcessor.applyAnnotations(swagger, p, type == null ? String.class : type,
			Collections.<Annotation> singletonList(param));
}
 
Example 28
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
private static Type getResponseType(Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && !ReflectionUtils.isVoid(apiOperation.response())) {
        return apiOperation.response();
    } else {
        return method.getGenericReturnType();
    }
}
 
Example 29
Source Project: jboot   Source File: ControllerReaderExtension.java    License: 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 30
Source Project: jboot   Source File: ControllerReaderExtension.java    License: Apache License 2.0 5 votes vote down vote up
public void applyOperationId(Operation operation, Method method) {
    final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    if (apiOperation != null && StringUtils.isNotBlank(apiOperation.nickname())) {
        operation.operationId(apiOperation.nickname());
    } else {
        operation.operationId(method.getName());
    }
}