org.springframework.restdocs.request.ParameterDescriptor Java Examples

The following examples show how to use org.springframework.restdocs.request.ParameterDescriptor. 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: DescriptorExtractorTest.java    From restdocs-raml with MIT License 6 votes vote down vote up
@Test
public void should_extract_request_parameter_descriptors() {
    // given
    RequestParametersSnippet snippet = requestParameters(
        parameterWithName("page").description("Is documented!"),
        parameterWithName("elementsPerPage").description("Is documented!")
    );

    // when
    List<ParameterDescriptor> descriptors = extract(snippet);

    then(descriptors).hasSize(2);
    then(descriptors.stream().map(ParameterDescriptor::getName).collect(toList()))
            .containsExactly("page", "elementsPerPage");
    then(descriptors.stream().map(AbstractDescriptor::getDescription).collect(toList()))
            .containsExactly("Is documented!", "Is documented!");
}
 
Example #2
Source File: Snippets.java    From genie with Apache License 2.0 6 votes vote down vote up
private static ParameterDescriptor[] getCommonSearchParameters() {
    return new ParameterDescriptor[]{
        RequestDocumentation
            .parameterWithName("page")
            .description("The page number to get. Default to 0.")
            .optional(),
        RequestDocumentation
            .parameterWithName("size")
            .description("The size of the page to get. Default to 64.")
            .optional(),
        RequestDocumentation
            .parameterWithName("sort")
            .description("The fields to sort the results by. Defaults to 'updated,desc'.")
            .optional(),
    };
}
 
Example #3
Source File: DescriptorExtractor.java    From restdocs-raml with MIT License 5 votes vote down vote up
public static List<ParameterDescriptor> extract(AbstractParametersSnippet snippet) {
    try {
        Method getParameterDescriptors = AbstractParametersSnippet.class.getDeclaredMethod("getParameterDescriptors");
        getParameterDescriptors.setAccessible(true);
        return new ArrayList<>(((Map<String, ParameterDescriptor>) getParameterDescriptors.invoke(snippet)).values());
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return emptyList();
}
 
Example #4
Source File: ParameterDescriptorWithRamlType.java    From restdocs-raml with MIT License 5 votes vote down vote up
protected static ParameterDescriptorWithRamlType from(ParameterDescriptor parameterDescriptor) {
    ParameterDescriptorWithRamlType newDescriptor = new ParameterDescriptorWithRamlType(parameterDescriptor.getName());
    newDescriptor.description(parameterDescriptor.getDescription());
    if (parameterDescriptor.isOptional()) {
        newDescriptor.optional();
    }
    if (parameterDescriptor.isIgnored()) {
        newDescriptor.ignored();
    }
    newDescriptor.type(STRING);
    return newDescriptor;
}
 
Example #5
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
protected static Snippet[] enhanceSnippetsWithRaml(String description,
                                                   boolean privateResource,
                                                   Function<List<Snippet>, List<Snippet>> snippetFilter,
                                                   Snippet... snippets) {

    List<RequestFieldsSnippet> requestFieldsSnippets = new ArrayList<>();
    List<ResponseFieldsSnippet> responseFieldsSnippets = new ArrayList<>();
    List<LinksSnippet> linkSnippets = new ArrayList<>();
    List<RequestParametersSnippet> requestParametersSnippets = new ArrayList<>();
    List<PathParametersSnippet> pathParametersSnippets = new ArrayList<>();
    List<RequestHeadersSnippet> requestHeadersSnippets = new ArrayList<>();
    List<ResponseHeadersSnippet> responseHeadersSnippets = new ArrayList<>();

    List<Snippet> ramlSnippets = new ArrayList<>();

    List<Snippet> otherSnippets = new ArrayList<>();

    for (Snippet snippet : snippets) {
        if (snippet instanceof RequestFieldsSnippet) {
            requestFieldsSnippets.add((RequestFieldsSnippet) snippet);
        } else if (snippet instanceof ResponseFieldsSnippet) {
            responseFieldsSnippets.add((ResponseFieldsSnippet) snippet);
        } else if (snippet instanceof LinksSnippet) {
            linkSnippets.add((LinksSnippet) snippet);
        } else if (snippet instanceof RequestParametersSnippet) {
            requestParametersSnippets.add((RequestParametersSnippet) snippet);
        } else if (snippet instanceof PathParametersSnippet) {
            pathParametersSnippets.add((PathParametersSnippet) snippet);
        } else if (snippet instanceof RequestHeadersSnippet) {
            requestHeadersSnippets.add((RequestHeadersSnippet) snippet);
        } else if (snippet instanceof ResponseHeadersSnippet) {
            responseHeadersSnippets.add((ResponseHeadersSnippet) snippet);
        } else if (snippet instanceof RamlResourceSnippet) {
            ramlSnippets.add(snippet);
        } else {
            otherSnippets.add(snippet);
        }
    }

    List<Snippet> enhancedSnippets = new ArrayList<>();
    enhancedSnippets.addAll(requestFieldsSnippets);
    enhancedSnippets.addAll(responseFieldsSnippets);
    enhancedSnippets.addAll(linkSnippets);
    enhancedSnippets.addAll(requestParametersSnippets);
    enhancedSnippets.addAll(pathParametersSnippets);
    enhancedSnippets.addAll(requestHeadersSnippets);
    enhancedSnippets.addAll(responseHeadersSnippets);
    enhancedSnippets.addAll(otherSnippets);

    if (ramlSnippets.isEmpty()) { // No RamlResourceSnippet, so we configure our own based on the info of the other snippets
        RamlResourceSnippetParameters ramlParameters = RamlResourceSnippetParameters.builder()
                .description(description)
                .privateResource(privateResource)
                .requestFields(requestFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
                .responseFields(responseFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
                .links(linkSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(LinkDescriptor[]::new))
                .requestParameters(requestParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
                .pathParameters(pathParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
                .requestHeaders(requestHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
                .responseHeaders(responseHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
                .build();
        enhancedSnippets.add(ramlResource(ramlParameters));
    } else {
        enhancedSnippets.addAll(ramlSnippets);
    }

    enhancedSnippets = snippetFilter.apply(enhancedSnippets);

    return enhancedSnippets.toArray(new Snippet[0]);
}
 
Example #6
Source File: AbstractSpringMVCTest.java    From SMSC with Apache License 2.0 4 votes vote down vote up
/**
 * Entity's id in path variables
 *
 * @return ParameterDescriptor
 */
protected ParameterDescriptor[] getPathParam(String name) {
    return new ParameterDescriptor[]{
            parameterWithName("id").description(String.format("%s's id", name))
    };
}
 
Example #7
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 4 votes vote down vote up
/**
 * userId in path variables
 * @return ParameterDescriptor
 */
private static ParameterDescriptor[] userPathParams() {
    return new ParameterDescriptor[] {
        parameterWithName("userId").description(USERS_ID_DESCRIPTION)
    };
}