io.github.jhipster.config.apidoc.PageableParameterBuilderPlugin.java Source code

Java tutorial

Introduction

Here is the source code for io.github.jhipster.config.apidoc.PageableParameterBuilderPlugin.java

Source

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.jhipster.config.apidoc;

import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.schema.ResolvedTypes.modelRefFactory;
import static springfox.documentation.spi.schema.contexts.ModelContext.inputParam;

import java.util.List;

import org.springframework.data.domain.Pageable;

import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Function;

import springfox.documentation.schema.ModelReference;
import springfox.documentation.schema.TypeNameExtractor;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.contexts.ModelContext;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.ParameterContext;

public class PageableParameterBuilderPlugin implements ParameterBuilderPlugin {

    private final TypeNameExtractor nameExtractor;
    private final TypeResolver resolver;

    public PageableParameterBuilderPlugin(TypeNameExtractor nameExtractor, TypeResolver resolver) {
        this.nameExtractor = nameExtractor;
        this.resolver = resolver;
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return true;
    }

    private Function<ResolvedType, ? extends ModelReference> createModelRefFactory(ParameterContext context) {
        ModelContext modelContext = inputParam(context.resolvedMethodParameter().getParameterType(),
                context.getDocumentationType(), context.getAlternateTypeProvider(),
                context.getGenericNamingStrategy(), context.getIgnorableParameterTypes());
        return modelRefFactory(modelContext, nameExtractor);
    }

    @Override
    public void apply(ParameterContext context) {
        ResolvedMethodParameter parameter = context.resolvedMethodParameter();
        Class<?> type = parameter.getParameterType().getErasedType();
        if (type != null && Pageable.class.isAssignableFrom(type)) {
            Function<ResolvedType, ? extends ModelReference> factory = createModelRefFactory(context);

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

            List<Parameter> parameters = newArrayList(
                    context.parameterBuilder().parameterType("query").name("page").modelRef(intModel)
                            .description("Page number of the requested page").build(),
                    context.parameterBuilder().parameterType("query").name("size").modelRef(intModel)
                            .description("Size of a page").build(),
                    context.parameterBuilder().parameterType("query").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.getOperationContext().operationBuilder().parameters(parameters);
        }
    }
}