Polish "Copy attributes when applying path prefix to field descriptors"

- Apply code formatting
 - Test copying of attributes (and other parts of a field descriptor)
   in new PayloadDocumentationTests

Closes gh-314
This commit is contained in:
Andy Wilkinson
2016-10-25 11:25:14 +01:00
parent 27b7553812
commit 7487f41363
2 changed files with 102 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.restdocs.snippet.Attributes;
import org.springframework.restdocs.snippet.Attributes.Attribute;
/**
* Static factory methods for documenting a RESTful API's request and response payloads.
@@ -453,8 +454,8 @@ public abstract class PayloadDocumentation {
FieldDescriptor prefixedDescriptor = new FieldDescriptor(
pathPrefix + descriptor.getPath())
.description(descriptor.getDescription())
.type(descriptor.getType());
prefixedDescriptor.attributes(convertAttributesToArray(descriptor.getAttributes()));
.type(descriptor.getType())
.attributes(asArray(descriptor.getAttributes()));
if (descriptor.isIgnored()) {
prefixedDescriptor.ignored();
}
@@ -466,12 +467,13 @@ public abstract class PayloadDocumentation {
return prefixedDescriptors;
}
private static Attributes.Attribute[] convertAttributesToArray(Map<String, Object> attributes) {
List<Attributes.Attribute> attributesAsList = new ArrayList<>();
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
attributesAsList.add(Attributes.key(entry.getKey()).value(entry.getValue()));
private static Attribute[] asArray(Map<String, Object> attributeMap) {
List<Attributes.Attribute> attributes = new ArrayList<>();
for (Map.Entry<String, Object> attribute : attributeMap.entrySet()) {
attributes
.add(Attributes.key(attribute.getKey()).value(attribute.getValue()));
}
return attributesAsList.toArray(new Attributes.Attribute[] {});
return attributes.toArray(new Attribute[attributes.size()]);
}
}