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

Commit 5bba23c

Browse files
authored
Polish (#171)
* Remove unused code and fix copyright year * Cover all cases for the required flag for path parameters * Simplify control flow * Remove unnecessary method calls and type casts * Fix typos * Fix typo * Use new request mapping annotations
1 parent 15e887e commit 5bba23c

File tree

15 files changed

+62
-70
lines changed

15 files changed

+62
-70
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/SnippetRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SnippetRegistry {
1111
public static final String DESCRIPTION = "auto-description";
1212
public static final String METHOD_PATH = "auto-method-path";
1313
public static final String PATH_PARAMETERS = "auto-path-parameters";
14-
public static final String REQUEST_HEADRERS = "auto-request-headers";
14+
public static final String REQUEST_HEADERS = "auto-request-headers";
1515
public static final String REQUEST_PARAMETERS = "auto-request-parameters";
1616
public static final String REQUEST_FIELDS = "auto-request-fields";
1717
public static final String RESPONSE_FIELDS = "auto-response-fields";

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/SkippableConstraintResolver.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828

2929
class SkippableConstraintResolver implements MethodParameterConstraintResolver {
3030
public static final Collection<String> MANDATORY_VALUE_ANNOTATIONS = Arrays.asList(
31-
new String[]{
32-
"javax.validation.constraints.NotNull",
33-
"org.hibernate.validator.constraints.NotBlank",
34-
"org.hibernate.validator.constraints.NotEmpty"
35-
});
31+
"javax.validation.constraints.NotNull",
32+
"org.hibernate.validator.constraints.NotBlank",
33+
"org.hibernate.validator.constraints.NotEmpty");
3634

3735
private final MethodParameterConstraintResolver delegate;
3836
private final GroupDescriptionResolver descriptionResolver;

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/jackson/FieldDocumentationVisitorWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,6 @@ private boolean topLevelPath() {
188188
}
189189

190190
private String toString(JavaType type) {
191-
return ((Class) type.getRawClass()).getSimpleName();
191+
return type.getRawClass().getSimpleName();
192192
}
193193
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/javadoc/JavadocUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private JavadocUtil() {
1111
}
1212

1313
public static String convertFromJavadoc(String javadoc, TemplateFormatting templateFormatting) {
14-
String converted = javadoc.toString()
14+
String converted = javadoc
1515
// line breaks in javadoc are ignored
1616
.replace("\n", "")
1717
// will be replaced with forced line break
@@ -35,7 +35,7 @@ public static String convertFromJavadoc(String javadoc, TemplateFormatting templ
3535

3636
String res = trimAndFixLineBreak(converted, FORCED_LB, templateFormatting.getLineBreak());
3737
res = trimAndFixLineBreak(res, LB, "\n");
38-
return res.toString();
38+
return res;
3939
}
4040

4141
private static String trimAndFixLineBreak(String text, String separator, String newSeparator) {

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/payload/AbstractJacksonFieldSnippet.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import capital.scalable.restdocs.snippet.StandardTableSnippet;
3737
import com.fasterxml.jackson.databind.JsonMappingException;
3838
import com.fasterxml.jackson.databind.ObjectMapper;
39-
import com.fasterxml.jackson.databind.ObjectWriter;
40-
import com.fasterxml.jackson.databind.type.TypeFactory;
4139
import org.springframework.core.MethodParameter;
4240
import org.springframework.restdocs.operation.Operation;
4341
import org.springframework.restdocs.payload.FieldDescriptor;
@@ -62,8 +60,6 @@ protected AbstractJacksonFieldSnippet(String snippetName, Map<String, Object> at
6260
protected Collection<FieldDescriptor> createFieldDescriptors(Operation operation,
6361
HandlerMethod handlerMethod) {
6462
ObjectMapper objectMapper = getObjectMapper(operation);
65-
ObjectWriter writer = objectMapper.writer();
66-
TypeFactory typeFactory = objectMapper.getTypeFactory();
6763

6864
JavadocReader javadocReader = getJavadocReader(operation);
6965
ConstraintReader constraintReader = getConstraintReader(operation);

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/PathParametersSnippet.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public PathParametersSnippet failOnUndocumentedParams(boolean failOnUndocumented
3939

4040
@Override
4141
protected boolean isRequired(MethodParameter param, PathVariable annot) {
42-
return param.getParameterType().isPrimitive()
43-
? true // spring disallows null for primitive
44-
: annot.required();
42+
// Spring disallows null for primitive types
43+
return param.getParameterType().isPrimitive() || annot.required();
4544
}
4645

4746
@Override

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/RequestHeaderSnippet.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,9 +39,8 @@ public RequestHeaderSnippet failOnUndocumentedParams(boolean failOnUndocumentedP
3939

4040
@Override
4141
protected boolean isRequired(MethodParameter param, RequestHeader annot) {
42-
return param.getParameterType().isPrimitive()
43-
? true // spring disallows null for primitive
44-
: annot.required();
42+
// Spring disallows null for primitive types
43+
return param.getParameterType().isPrimitive() || annot.required();
4544
}
4645

4746
@Override

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/snippet/StandardTableSnippet.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static capital.scalable.restdocs.constraints.ConstraintReader.OPTIONAL_ATTRIBUTE;
2424
import static capital.scalable.restdocs.javadoc.JavadocUtil.convertFromJavadoc;
2525
import static capital.scalable.restdocs.util.FormatUtil.addDot;
26-
import static java.util.Collections.emptyList;
2726
import static org.apache.commons.lang3.StringUtils.capitalize;
2827
import static org.apache.commons.lang3.StringUtils.join;
2928
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
@@ -54,11 +53,9 @@ protected Map<String, Object> createModel(Operation operation) {
5453
return model;
5554
}
5655

57-
Collection<FieldDescriptor> fieldDescriptors = emptyList();
58-
fieldDescriptors = createFieldDescriptors(operation, handlerMethod);
59-
56+
Collection<FieldDescriptor> fieldDescriptors =
57+
createFieldDescriptors(operation, handlerMethod);
6058
TemplateFormatting templateFormatting = determineTemplateFormatting(operation);
61-
6259
return createModel(handlerMethod, model, fieldDescriptors, templateFormatting);
6360
}
6461

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/util/TypeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static boolean isPrimitive(Class<?> javaBaseClass, String javaFieldName)
7171
if (field == null && isGetter(javaFieldName)) {
7272
field = ReflectionUtils.findField(javaBaseClass, fromGetter(javaFieldName));
7373
}
74-
return field != null ? field.getType().isPrimitive() : false;
74+
return field != null && field.getType().isPrimitive();
7575
}
7676

7777
public static List<JavaType> resolveAllTypes(JavaType javaType, TypeFactory typeFactory) {
@@ -97,7 +97,7 @@ private static List<JavaType> resolveNonArrayTypes(JavaType javaType, TypeFactor
9797
types.add(javaType);
9898

9999
Class<?> rawClass = javaType.getRawClass();
100-
JsonSubTypes jsonSubTypes = (JsonSubTypes) rawClass.getAnnotation(JsonSubTypes.class);
100+
JsonSubTypes jsonSubTypes = rawClass.getAnnotation(JsonSubTypes.class);
101101
if (jsonSubTypes != null) {
102102
for (JsonSubTypes.Type subType : jsonSubTypes.value()) {
103103
JavaType javaSubType = typeFactory.constructType(subType.value());

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/jackson/FieldDocumentationGeneratorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.lang.reflect.Type;
3535
import java.math.BigDecimal;
3636
import java.util.ArrayList;
37-
import java.util.Arrays;
3837
import java.util.List;
3938
import java.util.Map;
4039

@@ -459,10 +458,10 @@ private ExtendedFieldDescriptor descriptor(String path, Object fieldType,
459458
.type(fieldType)
460459
.description(comment);
461460
fieldDescriptor.attributes(
462-
new Attribute(OPTIONAL_ATTRIBUTE, Arrays.asList(optional)));
461+
new Attribute(OPTIONAL_ATTRIBUTE, singletonList(optional)));
463462
if (constraints != null) {
464463
fieldDescriptor.attributes(
465-
new Attribute(CONSTRAINTS_ATTRIBUTE, Arrays.asList(constraints)));
464+
new Attribute(CONSTRAINTS_ATTRIBUTE, asList(constraints)));
466465
}
467466
return new ExtendedFieldDescriptor(fieldDescriptor);
468467
}

0 commit comments

Comments
 (0)