springfox.documentation.schema.ModelReference Java Examples

The following examples show how to use springfox.documentation.schema.ModelReference. 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: ApiMethodReader.java    From swagger-more with Apache License 2.0 6 votes vote down vote up
private void readReturnDescription(OperationContext context, ApiMethod apiMethod) {
    ResolvedType returnType = context.alternateFor(context.getReturnType());
    String message = StringUtils.isEmpty(apiMethod.returnDescription()) ? "成功" : apiMethod.returnDescription();
    ModelReference modelRef = null;
    if (!isVoid(returnType)) {
        ModelContext modelContext = ModelContext.returnValue(
                context.getGroupName(), returnType,
                context.getDocumentationType(),
                context.getAlternateTypeProvider(),
                context.getGenericsNamingStrategy(),
                context.getIgnorableParameterTypes());
        modelRef = modelRefFactory(modelContext, typeNameExtractor).apply(returnType);
    }
    ResponseMessage built = new ResponseMessageBuilder()
            .code(HttpStatus.OK.value())
            .message(message)
            .responseModel(modelRef)
            .build();
    context.operationBuilder().responseMessages(newHashSet(built));
}
 
Example #2
Source File: SuccessPlugin.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(OperationContext context) {
	//TODO:这样做屏蔽了@ResponseHeader的功能,因为没有了该注解。
	//由于本项目header为统一配置,故未实现替代功能。有必要的话需要提供一个替代实现。
	if (context.findAnnotation(ApiResponses.class).isPresent()) {
		return;
	}
	Optional<Success> annotation = context.findAnnotation(Success.class);
	if (!annotation.isPresent()) {
		context.operationBuilder().responseMessages(okResponses);
		return;
	}
	ResponseMessageBuilder messageBuilder = new ResponseMessageBuilder();
	messageBuilder.code(HttpStatus.OK.value());
	messageBuilder.message(okMessage);
	ModelReference model = resolveModel(context, annotation.get());
	messageBuilder.responseModel(model);
	context.operationBuilder().responseMessages(Set.of(messageBuilder.build()));
}
 
Example #3
Source File: JavadocBuilderPlugin.java    From springfox-javadoc with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(OperationContext context) {

    String notes = context.requestMappingPattern() + PERIOD + context.httpMethod().toString() + ".notes";
    if (StringUtils.hasText(notes) && StringUtils.hasText(environment.getProperty(notes))) {
        context.operationBuilder().notes("<b>" + context.getName() + "</b><br/>" + environment.getProperty(notes));
    }
    String returnDescription = context.requestMappingPattern() + PERIOD + context.httpMethod().toString()
      + ".return";
    if (StringUtils.hasText(returnDescription) && StringUtils.hasText(environment.getProperty(returnDescription))) {
        context.operationBuilder().summary("returns " + environment.getProperty(returnDescription));
    }
    String throwsDescription = context.requestMappingPattern() + PERIOD + context.httpMethod().toString()
      + ".throws.";
    int i = 0;
    Set<ResponseMessage> responseMessages = new HashSet<ResponseMessage>();
    while (StringUtils.hasText(throwsDescription + i)
      && StringUtils.hasText(environment.getProperty(throwsDescription + i))) {
        String[] throwsValues = StringUtils.split(environment.getProperty(throwsDescription + i), "-");
        if (throwsValues.length == 2) {
            // TODO[MN]: proper mapping once
            // https://github.com/springfox/springfox/issues/521 is solved
            String thrownExceptionName = throwsValues[0];
            String throwComment = throwsValues[1];
            ModelReference model = new ModelRef(thrownExceptionName);
            ResponseMessage message = new ResponseMessageBuilder().code(500).message(throwComment)
              .responseModel(model).build();
            responseMessages.add(message);
        }
        i++;
    }
    context.operationBuilder().responseMessages(responseMessages);

}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: SuccessPlugin.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
private ModelReference resolveModel(OperationContext context, Success success) {
	ModelContext modelContext = SwaggerUtil.resolveModelContext(context, success.value());
	ResolvedType type = context.alternateFor(SwaggerUtil.resolvedType(typeResolver, success));
	return modelRefFactory(modelContext, typeNameExtractor).apply(type);
}
 
Example #14
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;
}