springfox.documentation.spi.service.contexts.ParameterContext Java Examples

The following examples show how to use springfox.documentation.spi.service.contexts.ParameterContext. 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: ApiRefererParameterReader.java    From jframework with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    ApiReferer apiReferer = context.getOperationContext().findAnnotation(ApiReferer.class).orNull();
    if (ObjectUtils.isEmpty(apiReferer)) {
        return;
    }
    String filedName = context.resolvedMethodParameter().defaultName().orNull();
    if (ObjectUtils.isEmpty(filedName)) {
        return;
    }
    try {
        String apimodelPropertyValue = apiReferer.value().getDeclaredField(filedName).getAnnotation(ApiModelProperty.class).value();
        context.parameterBuilder().description(apimodelPropertyValue);
    } catch (Exception e) {
        log.debug("apiReferer get filed error ", e);
    }
}
 
Example #2
Source File: ApiParamParameterBuilder.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void apply(ParameterContext context) {
  MethodParameter methodParameter = context.methodParameter();
  Optional<ApiParam> apiParam = findApiParam(methodParameter);
  context.parameterBuilder()
      .allowableValues(allowableValues(
          methodParameter,
          apiParam.transform(toAllowableValue()).or("")));
  if (apiParam.isPresent()) {
      Optional<ResolvedType> typeOptional = context.parameterBuilder().build().getType();
      ResolvedType resolvedType = typeOptional.get();
      String enumDescs = EnumUtil.getDescIfIsEnum(resolvedType);
    context.parameterBuilder().name(emptyToNull(apiParam.get().name()));
    if (StringUtil.isNotEmpty(enumDescs)){
        context.parameterBuilder().description(emptyToNull(apiParam.get().value()+":"+enumDescs));
    } else {
        context.parameterBuilder().description(emptyToNull(apiParam.get().value()));
    }
    context.parameterBuilder().parameterAccess(emptyToNull(apiParam.get().access()));
    context.parameterBuilder().defaultValue(emptyToNull(apiParam.get().defaultValue()));
    context.parameterBuilder().allowMultiple(apiParam.get().allowMultiple());
    context.parameterBuilder().required(apiParam.get().required());
  }
}
 
Example #3
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    String description = null;
    Optional<String> parmName = context.resolvedMethodParameter().defaultName();
    Annotation apiParam = annotationFromField(context, API_PARAM);
    if (apiParam != null) {
        Optional<Boolean> isRequired = isParamRequired(apiParam, context);
        if (isRequired.isPresent()) {
            context.parameterBuilder().required(isRequired.get());
        }
    }
    if (parmName.isPresent() && (apiParam == null || !hasValue(apiParam, context))) {
        String key = context.getOperationContext().requestMappingPattern() + PERIOD
          + context.getOperationContext().httpMethod().name() + ".param." + parmName.get();
        description = environment.getProperty(key);
    }
    if (description != null) {
        context.parameterBuilder().description(description);
    }
}
 
Example #4
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 #5
Source File: ApiParamReader.java    From swagger-more with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    ResolvedType resolvedType = context.resolvedMethodParameter().getParameterType();
    Class erasedType = resolvedType.getErasedType();
    if (isGeneratedType(erasedType)) {
        context.parameterBuilder()
                .parameterType("body").name(erasedType.getSimpleName())
                .description("Not a real parameter, it is a parameter generated after assembly.");
        return;
    }
    Optional<ApiParam> optional = readApiParam(context);
    if (optional.isPresent()) {
        ApiParam apiParam = optional.get();
        List<VendorExtension> extensions = buildExtensions(resolvedType);
        context.parameterBuilder().name(emptyToNull(apiParam.name()))
                .description(emptyToNull(resolver.resolve(apiParam.value())))
                .parameterType(TypeUtils.isComplexObjectType(erasedType) ? "body" : "query")
                .order(SWAGGER_PLUGIN_ORDER)
                .hidden(false)
                .parameterAccess(emptyToNull(apiParam.access()))
                .defaultValue(emptyToNull(apiParam.defaultValue()))
                .allowMultiple(apiParam.allowMultiple())
                .allowEmptyValue(apiParam.allowEmptyValue())
                .required(apiParam.required())
                .scalarExample(new Example(apiParam.example()))
                .complexExamples(examples(apiParam.examples()))
                .collectionFormat(apiParam.collectionFormat())
                .vendorExtensions(extensions);
    }
}
 
Example #6
Source File: DefaultParamPlugin.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
private void resolveApiParam(ParameterContext context) {
	ResolvedType resolvedType = context.resolvedMethodParameter().getParameterType();
	Class<?> parameterClass = resolveParamType(resolvedType);
	if (parameterClass == null) {
		log.warn(StrUtil.concat(resolvedType.getBriefDescription(), "的类型无法被DefaultParamPlugin解析"));
		@SuppressWarnings("unused") int a = 1;
		return;
	}
	ApiModel apiModel = parameterClass.getAnnotation(ApiModel.class);
	if (apiModel == null) {
		if (!BeanUtils.isSimpleProperty(parameterClass)) {
			warn(context, parameterClass);
		}
		return;
	}
	ParameterBuilder builder = context.parameterBuilder();
	builder.name(apiModel.description());
	builder.description(descriptions.resolve(apiModel.description()));
	builder.allowMultiple(false);
	builder.allowEmptyValue(false);
	builder.hidden(false);
	builder.collectionFormat("");
	builder.order(SWAGGER_PLUGIN_ORDER);
}
 
Example #7
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 #8
Source File: PageableParameterBuilderPlugin.java    From jhipster with Apache License 2.0 5 votes vote down vote up
/**
 * <p>createModelRefFactory.</p>
 *
 * @param context a {@link springfox.documentation.spi.service.contexts.ParameterContext} object.
 * @return a {@link java.util.function.Function} object.
 */
protected Function<ResolvedType, ? extends ModelReference> createModelRefFactory(ParameterContext context) {
    ModelContext modelContext = inputParam(
        context.getGroupName(),
        context.resolvedMethodParameter().getParameterType(),
        context.getDocumentationType(),
        context.getAlternateTypeProvider(),
        context.getGenericNamingStrategy(),
        context.getIgnorableParameterTypes());
    return modelRefFactory(modelContext, nameExtractor);
}
 
Example #9
Source File: PageableParameterBuilderPlugin.java    From jhipster with Apache License 2.0 5 votes vote down vote up
/**
 * Create a sort parameter.
 * Override it if needed. Set a default value or further description for example.
 *
 * @param context {@link org.springframework.data.domain.Pageable} parameter context
 * @return The sort parameter
 */
protected Parameter createSortParameter(ParameterContext context) {
    ModelReference stringModel = createModelRefFactory(context).apply(resolver.resolve(List.class, String.class));
    return new ParameterBuilder()
        .name(getSortName())
        .parameterType(SORT_TYPE)
        .modelRef(stringModel)
        .allowMultiple(true)
        .description(SORT_DESCRIPTION)
        .build();
}
 
Example #10
Source File: PageableParameterBuilderPlugin.java    From jhipster with Apache License 2.0 5 votes vote down vote up
/**
 * Create a size parameter.
 * Override it if needed. Set a default value for example.
 *
 * @param context {@link org.springframework.data.domain.Pageable} parameter context
 * @return The size parameter
 */
protected Parameter createSizeParameter(ParameterContext context) {
    ModelReference intModel = createModelRefFactory(context).apply(resolver.resolve(Integer.TYPE));
    return new ParameterBuilder()
        .name(getSizeName())
        .parameterType(SIZE_TYPE)
        .modelRef(intModel)
        .description(SIZE_DESCRIPTION)
        .build();
}
 
Example #11
Source File: PageableParameterBuilderPlugin.java    From jhipster with Apache License 2.0 5 votes vote down vote up
/**
 * Create a page parameter.
 * Override it if needed. Set a default value for example.
 *
 * @param context {@link org.springframework.data.domain.Pageable} parameter context
 * @return The page parameter
 */
protected Parameter createPageParameter(ParameterContext context) {
    ModelReference intModel = createModelRefFactory(context).apply(resolver.resolve(Integer.TYPE));
    return new ParameterBuilder()
        .name(getPageName())
        .parameterType(PAGE_TYPE)
        .modelRef(intModel)
        .description(PAGE_DESCRIPTION)
        .build();
}
 
Example #12
Source File: ParametersReader.java    From Resource with GNU General Public License v3.0 5 votes vote down vote up
private Function<ResolvedType, ? extends ModelReference> createModelRefFactory(ParameterContext context)
{
    ModelContext modelContext = inputParam(
        context.getGroupName(),
        context.resolvedMethodParameter().getParameterType(),
        context.getDocumentationType(),
        context.getAlternateTypeProvider(),
        context.getGenericNamingStrategy(),
        context.getIgnorableParameterTypes());
    return ResolvedTypes.modelRefFactory(modelContext, nameExtractor);
}
 
Example #13
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 #14
Source File: PageableParameterBuilderPlugin.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected Function<ResolvedType, ? extends ModelReference> createModelRefFactory(ParameterContext context) {
	ModelContext modelContext = inputParam(
		context.getGroupName(),
		context.resolvedMethodParameter().getParameterType(),
		context.getDocumentationType(),
		context.getAlternateTypeProvider(),
		context.getGenericNamingStrategy(),
		context.getIgnorableParameterTypes());
	return (Function<ResolvedType, ? extends ModelReference>) modelRefFactory(modelContext, nameExtractor);
}
 
Example #15
Source File: DefaultParamPlugin.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
private void resolveMapEnum(ParameterContext context) {
	MapEnum enumParam = context.resolvedMethodParameter().findAnnotation(MapEnum.class).orNull();
	if (enumParam != null) {
		String descs = MapEnumHelper.getDescs(enumParam.value(), enumParam.descMethod());
		Object defaultValue = MapEnumHelper.getDefaultValue(enumParam.value());
		ParameterBuilder builder = context.parameterBuilder();
		builder.description(descs);
		builder.scalarExample(new Example(defaultValue == null ? DefStr.EMPTY : defaultValue.toString()));
		builder.allowableValues(MapEnumHelper.getAllowableValues(enumParam.value()));
		builder.defaultValue(defaultValue == null ? DefStr.EMPTY : defaultValue.toString());
	}
}
 
Example #16
Source File: DefaultParamPlugin.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(ParameterContext context) {
	resolveParameterType(context);
	if (context.resolvedMethodParameter().hasParameterAnnotation(ApiParam.class)) {
		return;
	}
	resolveMapEnum(context);
	resolveApiParam(context);
}
 
Example #17
Source File: PageableParameterBuilderPlugin.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private Function<ResolvedType, ? extends ModelReference> createModelRefFactory(ParameterContext context) {
    ModelContext modelContext = inputParam(
            context.getGroupName(),
            context.resolvedMethodParameter().getParameterType(),
            context.getDocumentationType(),
            context.getAlternateTypeProvider(),
            context.getGenericNamingStrategy(),
            context.getIgnorableParameterTypes());
    return ResolvedTypes.modelRefFactory(modelContext, nameExtractor);
}
 
Example #18
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
boolean hasValue(Annotation annotation, ParameterContext context) {
    for (Method method : annotation.annotationType().getDeclaredMethods()) {
        if (method.getName().equals("value")) {
            try {
                Optional<String> value = Optional.of((String) method.invoke(annotation, (Object) null));
                return value.isPresent();
            } catch (Exception ex) {
                return false;
            }
        }
    }
    return false;
}
 
Example #19
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
Optional<Boolean> isRequired(Annotation annotation, ParameterContext context) {
    for (Method method : annotation.annotationType().getDeclaredMethods()) {
        if (method.getName().equals("required")) {
            try {
                return Optional.of((Boolean) method.invoke(annotation, (Object) null));
            } catch (Exception ex) {
                return Optional.absent();
            }
        }
    }
    return Optional.absent();
}
 
Example #20
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
Optional<Boolean> isParamRequired(Annotation apiParam, ParameterContext context) {
    if (apiParam != null) {
        Optional<Boolean> required = isRequired(apiParam, context);
        if (required.isPresent()) {
            return required;
        }
    }
    Annotation annotation = annotationFromField(context, REQUEST_PARAM);
    if (annotation == null) {
        annotation = annotationFromField(context, PATH_VARIABLE);
    }
    return annotation != null ? isRequired(annotation, context) : Optional.<Boolean>absent();
}
 
Example #21
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 #22
Source File: ParameterAllowableValuesPlugin.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(ParameterContext parameterContext) {
    String name = parameterContext.getOperationContext().getName();
    switch (name) {
        case "getClientOptions":
        case "generateClient":
            parameterContext.parameterBuilder().allowableValues(new AllowableListValues(clients, "string"));
            break;
        case "getServerOptions":
        case "generateServerForLanguage":
            parameterContext.parameterBuilder().allowableValues(new AllowableListValues(servers, "string"));
    }
}
 
Example #23
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
private boolean isCookieValue(ParameterContext context) {
    List<Annotation> annotations = context.resolvedMethodParameter().getAnnotations();
    return annotations.stream().anyMatch(annotation -> annotation.annotationType() == CookieValue.class);
}
 
Example #24
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    if (isCookieValue(context)) {
        context.parameterBuilder().parameterType("cookie");
    }
}
 
Example #25
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    if (isCookieValue(context)) {
        context.parameterBuilder().parameterType("cookie");
    }
}
 
Example #26
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
private boolean isCookieValue(ParameterContext context) {
    List<Annotation> annotations = context.resolvedMethodParameter().getAnnotations();
    return annotations.stream().anyMatch(annotation -> annotation.annotationType() == CookieValue.class);
}
 
Example #27
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
private boolean isCookieValue(ParameterContext context) {
    List<Annotation> annotations = context.resolvedMethodParameter().getAnnotations();
    return annotations.stream().anyMatch(annotation -> annotation.annotationType() == CookieValue.class);
}
 
Example #28
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    if (isCookieValue(context)) {
        context.parameterBuilder().parameterType("cookie");
    }
}
 
Example #29
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
private boolean isCookieValue(ParameterContext context) {
    List<Annotation> annotations = context.resolvedMethodParameter().getAnnotations();
    return annotations.stream().anyMatch(annotation -> annotation.annotationType() == CookieValue.class);
}
 
Example #30
Source File: SwaggerCustomParameterBuilderPlugin.java    From restful-booker-platform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(ParameterContext context) {
    if (isCookieValue(context)) {
        context.parameterBuilder().parameterType("cookie");
    }
}