Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Classic snippets #275

Merged
merged 6 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@
import org.springframework.restdocs.operation.Operation;

public class SnippetRegistry {
public static final String AUTHORIZATION = "auto-authorization";
public static final String DESCRIPTION = "auto-description";
public static final String METHOD_PATH = "auto-method-path";
public static final String PATH_PARAMETERS = "auto-path-parameters";
public static final String REQUEST_HEADERS = "auto-request-headers";
public static final String REQUEST_PARAMETERS = "auto-request-parameters";
public static final String REQUEST_FIELDS = "auto-request-fields";
public static final String RESPONSE_FIELDS = "auto-response-fields";
public static final String LINKS = "auto-links";
public static final String EMBEDDED = "auto-embedded";
// Spring Auto REST Docs snippets
public static final String AUTO_AUTHORIZATION = "auto-authorization";
public static final String AUTO_DESCRIPTION = "auto-description";
public static final String AUTO_METHOD_PATH = "auto-method-path";
public static final String AUTO_PATH_PARAMETERS = "auto-path-parameters";
public static final String AUTO_REQUEST_HEADERS = "auto-request-headers";
public static final String AUTO_REQUEST_PARAMETERS = "auto-request-parameters";
public static final String AUTO_REQUEST_FIELDS = "auto-request-fields";
public static final String AUTO_RESPONSE_FIELDS = "auto-response-fields";
public static final String AUTO_LINKS = "auto-links";
public static final String AUTO_EMBEDDED = "auto-embedded";

// Spring REST Docs classic snippets
public static final String CURL_REQUEST = "curl-request";
public static final String HTTP_REQUEST = "http-request";
public static final String HTTP_RESPONSE = "http-response";
public static final String HTTPIE_REQUEST = "httpie-request";
public static final String PATH_PARAMETERS = "path-parameters";
public static final String REQUEST_PARAMETERS = "request-parameters";
public static final String REQUEST_FIELDS = "request-fields";
public static final String RESPONSE_FIELDS = "response-fields";
public static final String REQUEST_BODY_SNIPPET = "request-body";
public static final String RESPONSE_BODY = "response-body";
public static final String REQUEST_HEADERS = "request-headers";
public static final String RESPONSE_HEADERS = "response-headers";
public static final String REQUEST_PARTS = "request-parts";
public static final String RESPONSE_PARTS = "response-parts";
public static final String REQUEST_PART_FIELDS = "request-part-fields";
public static final String LINKS = "links";

private static final Map<String, SectionSupport> CLASSIC_SNIPPETS;

Expand All @@ -49,17 +64,28 @@ public class SnippetRegistry {
CLASSIC_SNIPPETS.put(HTTPIE_REQUEST, section(HTTPIE_REQUEST, "example-request", true));
CLASSIC_SNIPPETS.put(HTTP_REQUEST, section(HTTP_REQUEST, "example-request", true));
CLASSIC_SNIPPETS.put(HTTP_RESPONSE, section(HTTP_RESPONSE, "example-response", true));
CLASSIC_SNIPPETS.put(REQUEST_PARAMETERS, section(REQUEST_PARAMETERS, "request-parameters", true));
CLASSIC_SNIPPETS.put(PATH_PARAMETERS, section(PATH_PARAMETERS, "path-parameters", true));
CLASSIC_SNIPPETS.put(LINKS, section(LINKS, "hypermedia-links", true));
CLASSIC_SNIPPETS.put(REQUEST_FIELDS, section(REQUEST_FIELDS, "request-fields", true));
CLASSIC_SNIPPETS.put(RESPONSE_FIELDS, section(RESPONSE_FIELDS, "response-fields", true));
CLASSIC_SNIPPETS.put(REQUEST_BODY_SNIPPET, section(REQUEST_BODY_SNIPPET, "request-body", true));
CLASSIC_SNIPPETS.put(RESPONSE_BODY, section(RESPONSE_BODY, "response-body", true));
CLASSIC_SNIPPETS.put(REQUEST_HEADERS, section(REQUEST_HEADERS, "request-headers", true));
CLASSIC_SNIPPETS.put(RESPONSE_HEADERS, section(RESPONSE_HEADERS, "response-headers", true));
CLASSIC_SNIPPETS.put(REQUEST_PARTS, section(REQUEST_PARTS, "request-parts", true));
CLASSIC_SNIPPETS.put(REQUEST_PART_FIELDS, section(REQUEST_PART_FIELDS, "request-part-fields", true));
}

public static SectionSupport getClassicSnippet(String snippetName) {
return CLASSIC_SNIPPETS.get(snippetName);
}

private static SectionSupport section(String snippetName, String header, boolean hasContent) {
public static SectionSupport section(String snippetName, String header, boolean hasContent) {
return new ClassicSnippetSection(snippetName, header, hasContent);
}

private static class ClassicSnippetSection implements SectionSupport {
public static class ClassicSnippetSection implements SectionSupport {
private String fileName;
private String headerKey;
private boolean hasContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
*/
package capital.scalable.restdocs.hypermedia;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_EMBEDDED;

import java.lang.reflect.Type;

import capital.scalable.restdocs.payload.AbstractJacksonFieldSnippet;
import org.springframework.web.method.HandlerMethod;

public class EmbeddedSnippet extends AbstractJacksonFieldSnippet {
public static final String EMBEDDED = "auto-embedded";

private final Type documentationType;
private final boolean failOnUndocumentedFields;
Expand All @@ -35,7 +36,7 @@ public EmbeddedSnippet() {
}

public EmbeddedSnippet(Type documentationType, boolean failOnUndocumentedFields) {
super(EMBEDDED, null);
super(AUTO_EMBEDDED, null);
this.documentationType = documentationType;
this.failOnUndocumentedFields = failOnUndocumentedFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
package capital.scalable.restdocs.hypermedia;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_LINKS;

import java.lang.reflect.Type;

import capital.scalable.restdocs.payload.AbstractJacksonFieldSnippet;
import org.springframework.web.method.HandlerMethod;

public class LinksSnippet extends AbstractJacksonFieldSnippet {
public static final String LINKS = "auto-links";

private final Type documentationType;
private final boolean failOnUndocumentedFields;

Expand All @@ -35,7 +35,7 @@ public LinksSnippet() {
}

public LinksSnippet(Type documentationType, boolean failOnUndocumentedFields) {
super(LINKS, null);
super(AUTO_LINKS, null);
this.documentationType = documentationType;
this.failOnUndocumentedFields = failOnUndocumentedFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static capital.scalable.restdocs.OperationAttributeHelper.getAuthorization;
import static capital.scalable.restdocs.OperationAttributeHelper.setAuthorization;
import static capital.scalable.restdocs.SnippetRegistry.AUTO_AUTHORIZATION;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -32,12 +33,10 @@

public class AuthorizationSnippet extends TemplatedSnippet implements SectionSupport {

public static final String AUTHORIZATION = "auto-authorization";

private final String defaultAuthorization;

public AuthorizationSnippet(String defaultAuthorization) {
super(AUTHORIZATION, null);
super(AUTO_AUTHORIZATION, null);
this.defaultAuthorization = defaultAuthorization;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static capital.scalable.restdocs.OperationAttributeHelper.determineTemplateFormatting;
import static capital.scalable.restdocs.OperationAttributeHelper.getHandlerMethod;
import static capital.scalable.restdocs.OperationAttributeHelper.getJavadocReader;
import static capital.scalable.restdocs.SnippetRegistry.AUTO_DESCRIPTION;
import static capital.scalable.restdocs.i18n.SnippetTranslationResolver.translate;
import static capital.scalable.restdocs.javadoc.JavadocUtil.convertFromJavadoc;
import static capital.scalable.restdocs.util.FormatUtil.addDot;
Expand All @@ -39,10 +40,8 @@

public class DescriptionSnippet extends TemplatedSnippet {

public static final String DESCRIPTION = "auto-description";

public DescriptionSnippet() {
super(DESCRIPTION, null);
super(AUTO_DESCRIPTION, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static capital.scalable.restdocs.OperationAttributeHelper.getRequestMethod;
import static capital.scalable.restdocs.OperationAttributeHelper.getRequestPattern;
import static capital.scalable.restdocs.SnippetRegistry.AUTO_METHOD_PATH;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,10 +31,8 @@

public class MethodAndPathSnippet extends TemplatedSnippet {

public static final String METHOD_PATH = "auto-method-path";

public MethodAndPathSnippet() {
super(METHOD_PATH, null);
super(AUTO_METHOD_PATH, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package capital.scalable.restdocs.payload;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_REQUEST_FIELDS;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;

Expand All @@ -29,8 +31,6 @@

public class JacksonRequestFieldSnippet extends AbstractJacksonFieldSnippet {

public static final String REQUEST_FIELDS = "auto-request-fields";

private final Type requestBodyType;
private final boolean failOnUndocumentedFields;

Expand All @@ -39,7 +39,7 @@ public JacksonRequestFieldSnippet() {
}

public JacksonRequestFieldSnippet(Type requestBodyType, boolean failOnUndocumentedFields) {
super(REQUEST_FIELDS, null);
super(AUTO_REQUEST_FIELDS, null);
this.requestBodyType = requestBodyType;
this.failOnUndocumentedFields = failOnUndocumentedFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package capital.scalable.restdocs.payload;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_RESPONSE_FIELDS;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.Map;
Expand All @@ -29,7 +31,6 @@

public class JacksonResponseFieldSnippet extends AbstractJacksonFieldSnippet {

public static final String RESPONSE_FIELDS = "auto-response-fields";
public static final String SPRING_DATA_PAGE_CLASS = "org.springframework.data.domain.Page";
public static final String REACTOR_MONO_CLASS = "reactor.core.publisher.Mono";
public static final String REACTOR_FLUX_CLASS = "reactor.core.publisher.Flux";
Expand All @@ -42,7 +43,7 @@ public JacksonResponseFieldSnippet() {
}

public JacksonResponseFieldSnippet(Type responseBodyType, boolean failOnUndocumentedFields) {
super(RESPONSE_FIELDS, null);
super(AUTO_RESPONSE_FIELDS, null);
this.responseBodyType = responseBodyType;
this.failOnUndocumentedFields = failOnUndocumentedFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
*/
package capital.scalable.restdocs.request;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_PATH_PARAMETERS;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.PathVariable;

public class PathParametersSnippet extends AbstractParameterSnippet<PathVariable> {

public static final String PATH_PARAMETERS = "auto-path-parameters";
private final boolean failOnUndocumentedParams;

public PathParametersSnippet() {
this(false);
}

public PathParametersSnippet(boolean failOnUndocumentedParams) {
super(PATH_PARAMETERS, null);
super(AUTO_PATH_PARAMETERS, null);
this.failOnUndocumentedParams = failOnUndocumentedParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
*/
package capital.scalable.restdocs.request;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_REQUEST_HEADERS;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestHeader;

public class RequestHeaderSnippet extends AbstractParameterSnippet<RequestHeader> {

public static final String REQUEST_HEADERS = "auto-request-headers";
private final boolean failOnUndocumentedParams;

public RequestHeaderSnippet() {
this(false);
}

public RequestHeaderSnippet(boolean failOnUndocumentedParams) {
super(REQUEST_HEADERS, null);
super(AUTO_REQUEST_HEADERS, null);
this.failOnUndocumentedParams = failOnUndocumentedParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package capital.scalable.restdocs.request;

import static capital.scalable.restdocs.SnippetRegistry.AUTO_REQUEST_PARAMETERS;

import java.util.Map;

import org.springframework.core.MethodParameter;
Expand All @@ -28,7 +30,6 @@

public class RequestParametersSnippet extends AbstractParameterSnippet<RequestParam> {

public static final String REQUEST_PARAMETERS = "auto-request-parameters";
public static final String SPRING_DATA_PAGEABLE_CLASS =
"org.springframework.data.domain.Pageable";

Expand All @@ -39,7 +40,7 @@ public RequestParametersSnippet() {
}

public RequestParametersSnippet(boolean failOnUndocumentedParams) {
super(REQUEST_PARAMETERS, null);
super(AUTO_REQUEST_PARAMETERS, null);
this.failOnUndocumentedParams = failOnUndocumentedParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
public class SectionBuilder {

public static final Collection<String> DEFAULT_SNIPPETS = Arrays.asList(
SnippetRegistry.AUTHORIZATION,
SnippetRegistry.PATH_PARAMETERS,
SnippetRegistry.REQUEST_PARAMETERS,
SnippetRegistry.REQUEST_FIELDS,
SnippetRegistry.RESPONSE_FIELDS,
SnippetRegistry.LINKS,
SnippetRegistry.EMBEDDED,
SnippetRegistry.AUTO_AUTHORIZATION,
SnippetRegistry.AUTO_PATH_PARAMETERS,
SnippetRegistry.AUTO_REQUEST_PARAMETERS,
SnippetRegistry.AUTO_REQUEST_FIELDS,
SnippetRegistry.AUTO_RESPONSE_FIELDS,
SnippetRegistry.AUTO_LINKS,
SnippetRegistry.AUTO_EMBEDDED,
SnippetRegistry.CURL_REQUEST,
SnippetRegistry.HTTP_RESPONSE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ public static String join(String delimiter, Object... texts) {
return result.toString();
}
}

public static String fixLineSeparator(String str) {
return str.replace("\n", System.lineSeparator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ authorization=Authorization
path-parameters=Path parameters
request-parameters=Query parameters
request-headers=Request headers
response-headers=Response headers
request-fields=Request fields
response-fields=Response fields
example-request=Example request
example-response=Example response
request-body=Request Body
response-body=Response Body
request-parts=Request Parts
response-parts=Response Parts
request-part-fields=Request Part Fields
th-parameter=Parameter
th-path=Path
th-header=Header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package capital.scalable.restdocs.hypermedia;

import static capital.scalable.restdocs.hypermedia.EmbeddedSnippet.EMBEDDED;
import static capital.scalable.restdocs.SnippetRegistry.AUTO_EMBEDDED;
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void embedded() throws Exception {
mockFieldComment(EmbeddedDocs.class, "embedded1", "Resource 1");
mockFieldComment(EmbeddedDocs.class, "embedded2", "Resource 2");

this.snippets.expect(EMBEDDED).withContents(
this.snippets.expect(AUTO_EMBEDDED).withContents(
tableWithHeader("Path", "Type", "Optional", "Description")
.row("embedded1", "Array[Object]", "true", "Resource 1.")
.row("embedded2", "Object", "true", "Resource 2."));
Expand All @@ -84,7 +84,7 @@ public void embedded() throws Exception {

@Test
public void noHandlerMethod() throws Exception {
this.snippets.expect(EMBEDDED).withContents(equalTo("No embedded resources."));
this.snippets.expect(AUTO_EMBEDDED).withContents(equalTo("No embedded resources."));

new EmbeddedSnippet().document(operationBuilder
.attribute(ObjectMapper.class.getName(), mapper)
Expand Down
Loading