springfox.documentation.service.ResolvedMethodParameter Java Examples

The following examples show how to use springfox.documentation.service.ResolvedMethodParameter. 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: HandlerMethodResolverWrapper.java    From summerframework with Apache License 2.0 6 votes vote down vote up
private Function<ResolvedMethod, List<ResolvedMethodParameter>> toParameters(final HandlerMethod methodToResolve) {
    return new Function<ResolvedMethod, List<ResolvedMethodParameter>>() {
        @Override
        public List<ResolvedMethodParameter> apply(ResolvedMethod input) {
            List<ResolvedMethodParameter> parameters = newArrayList();
            MethodParameter[] methodParameters = methodToResolve.getMethodParameters();
            for (int i = 0; i < methodParameters.length; i++) {
                methodParameters[i] = interfaceMethodParameter(methodParameters[i]);
            }
            for (int i = 0; i < input.getArgumentCount(); i++) {
                parameters.add(
                    new ResolvedMethodParameter(discoveredName(methodParameters[i]).or(String.format("param%s", i)),
                        methodParameters[i], input.getArgumentType(i)));
            }
            return parameters;
        }
    };
}
 
Example #2
Source File: PageableParameterBuilderPlugin.java    From jhipster with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void apply(OperationContext context) {
    List<Parameter> parameters = newArrayList();
    for (ResolvedMethodParameter methodParameter : context.getParameters()) {
        ResolvedType resolvedType = methodParameter.getParameterType();

        if (pageableType.equals(resolvedType)) {
            ParameterContext parameterContext = new ParameterContext(methodParameter,
                new ParameterBuilder(),
                context.getDocumentationContext(),
                context.getGenericsNamingStrategy(),
                context);

            parameters.add(createPageParameter(parameterContext));
            parameters.add(createSizeParameter(parameterContext));
            parameters.add(createSortParameter(parameterContext));

            context.operationBuilder().parameters(parameters);
        }
    }
}
 
Example #3
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 5 votes vote down vote up
private static Annotation annotationFromField(ParameterContext context, String annotationType) {

        ResolvedMethodParameter methodParam = context.resolvedMethodParameter();

        for (Annotation annotation : methodParam.getAnnotations()) {
            if (annotation.annotationType().getName().equals(annotationType)) {
                return annotation;
            }
        }
        return null;

    }
 
Example #4
Source File: ApiMethodModelsProvider.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
private void collectApiMethodParams(RequestMappingContext context) {
    List<ResolvedMethodParameter> parameterTypes = context.getParameters();
    Optional<ApiMethod> optional = context.findAnnotation(ApiMethod.class);
    OperationModelContextsBuilder builder = context.operationModelsBuilder();
    if (optional.isPresent()) {
        parameterTypes.forEach(parameter -> collectAllTypes(context, parameter).forEach(builder::addInputParam));
    }
}
 
Example #5
Source File: PageableParameterBuilderPlugin.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void apply(OperationContext context) {
	for (ResolvedMethodParameter methodParameter : context.getParameters()) {
		ResolvedType resolvedType = methodParameter.getParameterType();
		if (pageableType.equals(resolvedType)) {
			ParameterContext parameterContext = new ParameterContext(methodParameter,
				new ParameterBuilder(),
				context.getDocumentationContext(),
				context.getGenericsNamingStrategy(),
				context);

			ModelReference intModel = createModelRefFactory(parameterContext).apply(resolver.resolve(Integer.TYPE));
			ModelReference stringModel = createModelRefFactory(parameterContext).apply(resolver.resolve(List.class, String.class));

			List<Parameter> parameters = Lists.newArrayList(
				new ParameterBuilder()
					.parameterType("query").name("current").modelRef(intModel)
					.description("Page number of the requested page")
					.build(),
				new ParameterBuilder()
					.parameterType("query").name("size").modelRef(intModel)
					.description("Size of a page")
					.build(),
				new ParameterBuilder()
					.parameterType("query").name("descs").modelRef(stringModel).allowMultiple(true)
					.description("Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.")
					.build(),
				new ParameterBuilder()
					.parameterType("query").name("ascs").modelRef(stringModel).allowMultiple(true)
					.description("Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.")
					.build(),
				new ParameterBuilder()
					.parameterType("query").name("queryConditionJson").modelRef(stringModel).allowMultiple(true)
					.description("search json [{\"fieldName\":\"name\",\"attrType\":\"String\",\"fieldNode\":\"\",\"operate\":\"like\",\"weight\":0,\"value\":\"g\"},{\"fieldName\":\"status\",\"attrType\":\"Integer\",\"fieldNode\":\"\",\"operate\":\"in\",\"weight\":0,\"value\":\"-1\"}]}")
					.build());

			context.operationBuilder().parameters(parameters);
		}
	}
}
 
Example #6
Source File: DefaultRequiredPlugin.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 获取spring系列注解中的必填设置
 * @author Frodez
 * @date 2019-06-11
 */
private boolean requestXXX(ResolvedMethodParameter methodParameter) {
	Optional<RequestAttribute> requestAttribute = methodParameter.findAnnotation(RequestAttribute.class);
	if (requestAttribute.isPresent()) {
		return requestAttribute.get().required();
	}
	Optional<RequestBody> requestBody = methodParameter.findAnnotation(RequestBody.class);
	if (requestBody.isPresent()) {
		return requestBody.get().required();
	}
	Optional<RequestHeader> requestHeader = methodParameter.findAnnotation(RequestHeader.class);
	if (requestHeader.isPresent()) {
		return requestHeader.get().required();
	}
	Optional<RequestParam> requestParam = methodParameter.findAnnotation(RequestParam.class);
	if (requestParam.isPresent()) {
		return requestParam.get().required();
	}
	Optional<RequestPart> requestPart = methodParameter.findAnnotation(RequestPart.class);
	if (requestPart.isPresent()) {
		return requestPart.get().required();
	}
	Optional<PathVariable> pathVariable = methodParameter.findAnnotation(PathVariable.class);
	if (pathVariable.isPresent()) {
		return pathVariable.get().required();
	}
	return false;
}
 
Example #7
Source File: ApiMethodModelsProvider.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
private List<ResolvedType> collectAllTypes(RequestMappingContext context, ResolvedMethodParameter parameter) {
    List<ResolvedType> allTypes = newArrayList();
    for (ResolvedType type : collectBindingTypes(context.alternateFor(parameter.getParameterType()), newArrayList())) {
        ApiModel apiModel = AnnotationUtils.getAnnotation(type.getErasedType(), ApiModel.class);
        allTypes.add(type);
        if (apiModel != null) {
            allTypes.addAll(Arrays.stream(apiModel.subTypes())
                    .filter(subType -> subType.getAnnotation(ApiModel.class) != type.getErasedType().getAnnotation(ApiModel.class))
                    .map(typeResolver::resolve).collect(Collectors.toList()));
        }
    }
    return allTypes;
}
 
Example #8
Source File: ApiParamReader.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
private Optional<ApiParam> readApiParam(ParameterContext context) {
    Optional<ApiMethod> optional = context.getOperationContext().findAnnotation(ApiMethod.class);
    if (optional.isPresent()) {
        ApiMethod apiMethod = optional.get();
        ResolvedMethodParameter parameter = context.resolvedMethodParameter();
        if (parameter.getParameterIndex() > apiMethod.params().length - 1) {
            throw new SwaggerMoreException("The number of parameters in method " + context.getOperationContext().getName() + " does not match the number of @ApiParam.");
        }
        return Optional.of(apiMethod.params()[parameter.getParameterIndex()]);
    }
    return Optional.absent();
}
 
Example #9
Source File: RequestToPoOperationBuilder.java    From jframework with Apache License 2.0 5 votes vote down vote up
private boolean shouldIgnore(
        final ResolvedMethodParameter parameter,
        ResolvedType resolvedParameterType,
        final Set<Class> ignorableParamTypes) {

    if (ignorableParamTypes.contains(resolvedParameterType.getErasedType())) {
        return true;
    }
    return ignorableParamTypes.stream()
            .filter(Annotation.class::isAssignableFrom)
            .anyMatch(parameter::hasParameterAnnotation);
}
 
Example #10
Source File: RequestToPoOperationBuilder.java    From jframework with Apache License 2.0 5 votes vote down vote up
private void readRequestFormToPojo(OperationContext context, ResolvedMethodParameter methodParameter) {

        List<RawField> fields = methodParameter.getParameterType().getMemberFields();
        List<RawField> parentRawFields = methodParameter.getParameterType().getParentClass().getMemberFields();

        List<RawField> fieldList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(fields)) {
            fieldList.addAll(fields);
        }
        if (!CollectionUtils.isEmpty(parentRawFields)) {
            fieldList.addAll(parentRawFields);
        }
        List<Parameter> requestMapParameters = Lists.newArrayList();
        for (RawField rawField : fieldList) {
            String name = CaseUtil.snakeCase(rawField.getName());
            Class itemClass = rawField.getRawMember().getType();
            if (itemClass != null) {
                String itemTypeName = typeNameFor(itemClass);
                if (ObjectUtils.isEmpty(itemTypeName)) {
                    // 解析出这个类型的所有成员加到文档里
                    requestMapParameters.addAll(addFieldsToDoc(rawField.getRawMember(), name, null));
                } else {
                    requestMapParameters.add(genPrimaryTypeDoc(rawField.getRawMember(), "", itemTypeName));
                }
            }
        }

        try {
            OperationBuilder operationBuilder = context.operationBuilder();
            Field parametersField = context.operationBuilder().getClass().getDeclaredField("parameters");
            parametersField.setAccessible(true);
            parametersField.set(operationBuilder, requestMapParameters);
        } catch (Exception e) {
            log.debug("swagger RequestToPo error", e);
        }
    }
 
Example #11
Source File: RequestToPoOperationBuilder.java    From jframework with Apache License 2.0 5 votes vote down vote up
private List<Parameter> readParameters(final OperationContext context) {

        List<ResolvedMethodParameter> methodParameters = context.getParameters();
        final List<Parameter> parameterList = new ArrayList<>();

        for (ResolvedMethodParameter methodParameter : methodParameters) {
            ResolvedType alternate = context.alternateFor(methodParameter.getParameterType());

            if (!shouldIgnore(methodParameter, alternate, context.getIgnorableParameterTypes()) && isRequestFormToPojo(methodParameter)) {
                readRequestFormToPojo(context, methodParameter);
            }
        }
        return parameterList.stream().filter(((Predicate<Parameter>) Parameter::isHidden).negate()).collect(Collectors.toList());
    }
 
Example #12
Source File: ApiRequestHandler.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
ApiRequestHandler(HandlerMethodResolver methodResolver,
                  HandlerMethod handlerMethod,
                  List<ResolvedMethodParameter> resolvedMethodParameters) {
    this.methodResolver = methodResolver;
    this.handlerMethod = handlerMethod;
    this.resolvedMethodParameters = resolvedMethodParameters;
}
 
Example #13
Source File: RequestToPoOperationBuilder.java    From jframework with Apache License 2.0 4 votes vote down vote up
private boolean isRequestFormToPojo(final ResolvedMethodParameter parameter) {
    return parameter.hasParameterAnnotation(RequestToPo.class);
}
 
Example #14
Source File: HandlerMethodResolverWrapper.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public List<ResolvedMethodParameter> methodParameters(final HandlerMethod methodToResolve) {
    return resolvedMethod(methodToResolve).transform(toParameters(methodToResolve))
        .or(Lists.<ResolvedMethodParameter>newArrayList());
}
 
Example #15
Source File: PageableParameterBuilderPlugin.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(OperationContext context) {
    List<ResolvedMethodParameter> methodParameters = context.getParameters();
    List<Parameter> parameters = newArrayList();

    for (ResolvedMethodParameter methodParameter : methodParameters) {
        ResolvedType resolvedType = methodParameter.getParameterType();

        if (pageableType.equals(resolvedType)) {
            ParameterContext parameterContext = new ParameterContext(methodParameter,
                    new ParameterBuilder(),
                    context.getDocumentationContext(),
                    context.getGenericsNamingStrategy(),
                    context);
            Function<ResolvedType, ? extends ModelReference> factory = createModelRefFactory(parameterContext);

            ModelReference intModel = factory.apply(resolver.resolve(Integer.TYPE));
            ModelReference stringModel = factory.apply(resolver.resolve(List.class, String.class));

            parameters.add(new ParameterBuilder()
                    .parameterType("queryRecords")
                    .name("page")
                    .modelRef(intModel)
                    .description("Results page you want to retrieve (0..N)").build());
            parameters.add(new ParameterBuilder()
                    .parameterType("queryRecords")
                    .name("size")
                    .modelRef(intModel)
                    .description("Number of records per page").build());
            parameters.add(new ParameterBuilder()
                    .parameterType("queryRecords")
                    .name("sort")
                    .modelRef(stringModel)
                    .allowMultiple(true)
                    .description("Sorting criteria in the format: property(,asc|desc). "
                            + "Default sort order is ascending. "
                            + "Multiple sort criteria are supported.")
                    .build());
            context.operationBuilder().parameters(parameters);
        }
    }
}
 
Example #16
Source File: DefaultParamPlugin.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
private void resolveParameterType(ParameterContext context) {
	ResolvedMethodParameter parameter = context.resolvedMethodParameter();
	ResolvedType type = context.alternateFor(parameter.getParameterType());
	//Multi-part file trumps any other annotations
	if (isFileType(type) || isListOfFiles(type)) {
		context.parameterBuilder().parameterType("form");
		return;
	}
	if (parameter.hasParameterAnnotation(PathVariable.class)) {
		context.parameterBuilder().parameterType("path");
		return;
	}
	if (parameter.hasParameterAnnotation(RequestBody.class)) {
		context.parameterBuilder().parameterType("body");
		return;
	}
	if (parameter.hasParameterAnnotation(RequestPart.class)) {
		context.parameterBuilder().parameterType("formData");
		return;
	}
	if (parameter.hasParameterAnnotation(RequestParam.class)) {
		Set<? extends MediaType> consumes = context.getOperationContext().consumes();
		HttpMethod method = context.getOperationContext().httpMethod();
		if (consumes.contains(MediaType.APPLICATION_FORM_URLENCODED) && method == HttpMethod.POST) {
			context.parameterBuilder().parameterType("form");
		} else if (consumes.contains(MediaType.MULTIPART_FORM_DATA) && method == HttpMethod.POST) {
			context.parameterBuilder().parameterType("formData");
		} else {
			context.parameterBuilder().parameterType("query");
		}
		return;
	}
	if (parameter.hasParameterAnnotation(RequestHeader.class)) {
		context.parameterBuilder().parameterType("header");
		return;
	}
	if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
		log.warn(StrUtil.concat("@ModelAttribute annotated parameters should have already been expanded via ",
			"the ExpandedParameterBuilderPlugin"));
	}
	if (parameter.hasParameterAnnotation(ApiParam.class)) {
		context.parameterBuilder().parameterType("query");
		return;
	}
	if (parameter.hasParameterAnnotation(MapEnum.class)) {
		context.parameterBuilder().parameterType("query");
		return;
	}
	context.parameterBuilder().parameterType("body");
}
 
Example #17
Source File: ApiRequestHandler.java    From swagger-more with Apache License 2.0 4 votes vote down vote up
@Override
public List<ResolvedMethodParameter> getParameters() {
    return resolvedMethodParameters;
}
 
Example #18
Source File: ParametersReader.java    From Resource with GNU General Public License v3.0 4 votes vote down vote up
private List<Parameter> readParameters(OperationContext context)
{
    List<Parameter> parameters = Lists.newArrayList();
    List<ResolvedMethodParameter> methodParameters = context.getParameters();

    Map<String, ApiSingleParam> paramMap = new HashMap<>();
    Field[] fields = ModelCache.getInstance().getParamClass().getDeclaredFields();
    String type = new String();
    for (Field field : fields)
    {
        if (field.isAnnotationPresent(ApiSingleParam.class))
        {
            ApiSingleParam param = field.getAnnotation(ApiSingleParam.class);
            try
            {
                String name = (String)field.get(type);
                paramMap.put(name, param);
            }
            catch (Exception e)
            {
            }
        }
    }

    for (ResolvedMethodParameter methodParameter : methodParameters)
    {
        ParameterContext parameterContext = new ParameterContext(methodParameter,
            new ParameterBuilder(),
            context.getDocumentationContext(),
            context.getGenericsNamingStrategy(),
            context);
        Function<ResolvedType, ? extends ModelReference> factory = createModelRefFactory(parameterContext);
        Optional<ApiJsonObject> annotation = context.findAnnotation(ApiJsonObject.class);

        if (annotation.isPresent())
        {
            ModelCache.getInstance().setFactory(factory)
                .setParamMap(paramMap)
                .addModel(annotation.get());

        }
    }
    return parameters;
}
 
Example #19
Source File: DefaultRequiredPlugin.java    From BlogManagePlatform with Apache License 2.0 2 votes vote down vote up
/**
 * 判断是否必填
 * @author Frodez
 * @date 2019-06-11
 */
private boolean isRequired(ResolvedMethodParameter methodParameter) {
	//优先判断spring的必填设置,如果spring未设置必填,则判断是否拥有可以为空的注解
	return requestXXX(methodParameter) ? true : !hasNullableAnnotation(methodParameter);
}
 
Example #20
Source File: DefaultRequiredPlugin.java    From BlogManagePlatform with Apache License 2.0 2 votes vote down vote up
/**
 * 判断是否拥有可以为空的注解
 * @author Frodez
 * @date 2019-06-11
 */
private boolean hasNullableAnnotation(ResolvedMethodParameter methodParameter) {
	return methodParameter.hasParameterAnnotation(Null.class) || methodParameter.hasParameterAnnotation(Nullable.class) || methodParameter
		.hasParameterAnnotation(reactor.util.annotation.Nullable.class);
}