Allow subclasses to access a snippet’s state
This commit updates each of the built-in snippet implementations to provide protected getter methods for their fields. This allows subclasses to access and work with the snippet’s state. See gh-73
This commit is contained in:
@@ -47,8 +47,6 @@ public class LinksSnippet extends TemplatedSnippet {
|
||||
|
||||
private final Map<String, LinkDescriptor> descriptorsByRel = new LinkedHashMap<>();
|
||||
|
||||
private final Set<String> requiredRels = new HashSet<>();
|
||||
|
||||
private final LinkExtractor linkExtractor;
|
||||
|
||||
/**
|
||||
@@ -79,9 +77,6 @@ public class LinksSnippet extends TemplatedSnippet {
|
||||
Assert.hasText(descriptor.getRel());
|
||||
Assert.hasText(descriptor.getDescription());
|
||||
this.descriptorsByRel.put(descriptor.getRel(), descriptor);
|
||||
if (!descriptor.isOptional()) {
|
||||
this.requiredRels.add(descriptor.getRel());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +100,15 @@ public class LinksSnippet extends TemplatedSnippet {
|
||||
Set<String> undocumentedRels = new HashSet<String>(actualRels);
|
||||
undocumentedRels.removeAll(this.descriptorsByRel.keySet());
|
||||
|
||||
Set<String> missingRels = new HashSet<String>(this.requiredRels);
|
||||
Set<String> requiredRels = new HashSet<>();
|
||||
for (Entry<String, LinkDescriptor> relAndDescriptor : this.descriptorsByRel
|
||||
.entrySet()) {
|
||||
if (!relAndDescriptor.getValue().isOptional()) {
|
||||
requiredRels.add(relAndDescriptor.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> missingRels = new HashSet<String>(requiredRels);
|
||||
missingRels.removeAll(actualRels);
|
||||
|
||||
if (!undocumentedRels.isEmpty() || !missingRels.isEmpty()) {
|
||||
@@ -133,4 +136,14 @@ public class LinksSnippet extends TemplatedSnippet {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code Map} of {@link LinkDescriptor LinkDescriptors} keyed by their
|
||||
* {@link LinkDescriptor#getRel() rels}.
|
||||
*
|
||||
* @return the link descriptors
|
||||
*/
|
||||
protected final Map<String, LinkDescriptor> getDescriptorsByRel() {
|
||||
return this.descriptorsByRel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -146,4 +146,14 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
|
||||
*/
|
||||
protected abstract byte[] getContent(Operation operation) throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the list of {@link FieldDescriptor FieldDescriptors} that will be used to
|
||||
* generate the documentation.
|
||||
*
|
||||
* @return the field descriptors
|
||||
*/
|
||||
protected final List<FieldDescriptor> getFieldDescriptors() {
|
||||
return this.fieldDescriptors;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -94,4 +94,15 @@ public abstract class AbstractParametersSnippet extends TemplatedSnippet {
|
||||
protected abstract void verificationFailed(Set<String> undocumentedParameters,
|
||||
Set<String> missingParameters);
|
||||
|
||||
/**
|
||||
* Returns a {@code Map} of {@link ParameterDescriptor ParameterDescriptors} that will
|
||||
* be used to generate the documentation key by their
|
||||
* {@link ParameterDescriptor#getName()}.
|
||||
*
|
||||
* @return the map of path descriptors
|
||||
*/
|
||||
protected final Map<String, ParameterDescriptor> getFieldDescriptors() {
|
||||
return this.descriptorsByName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -81,4 +81,23 @@ public abstract class TemplatedSnippet implements Snippet {
|
||||
*/
|
||||
protected abstract Map<String, Object> createModel(Operation operation);
|
||||
|
||||
/**
|
||||
* Returns the additional attributes that will be included in the model during
|
||||
* template rendering.
|
||||
*
|
||||
* @return the additional attributes
|
||||
*/
|
||||
protected final Map<String, Object> getAttributes() {
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the snippet that will be created.
|
||||
*
|
||||
* @return the snippet name
|
||||
*/
|
||||
protected final String getSnippetName() {
|
||||
return this.snippetName;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user