Class AbstractFieldsSnippet

java.lang.Object
org.springframework.restdocs.snippet.TemplatedSnippet
org.springframework.restdocs.payload.AbstractFieldsSnippet
All Implemented Interfaces:
Snippet
Direct Known Subclasses:
RequestFieldsSnippet, RequestPartFieldsSnippet, ResponseFieldsSnippet

public abstract class AbstractFieldsSnippet extends TemplatedSnippet
Abstract TemplatedSnippet subclass that provides a base for snippets that document a RESTful resource's request or response fields.
Author:
Andreas Evers, Andy Wilkinson, Mathias Düsterhöft
  • Constructor Details

    • AbstractFieldsSnippet

      protected AbstractFieldsSnippet(String type, List<FieldDescriptor> descriptors, Map<String,Object> attributes, boolean ignoreUndocumentedFields)
      Creates a new AbstractFieldsSnippet that will produce a snippet named <type>-fields using a template named <type>-fields. The fields will be documented using the given descriptors and the given attributes will be included in the model during template rendering. If ignoreUndocumentedFields is true, undocumented fields will be ignored and will not trigger a failure.
      Parameters:
      type - the type of the fields
      descriptors - the field descriptors
      attributes - the additional attributes
      ignoreUndocumentedFields - whether undocumented fields should be ignored
    • AbstractFieldsSnippet

      protected AbstractFieldsSnippet(String type, List<FieldDescriptor> descriptors, Map<String,Object> attributes, boolean ignoreUndocumentedFields, PayloadSubsectionExtractor<?> subsectionExtractor)
      Creates a new AbstractFieldsSnippet that will produce a snippet named <type>-fields using a template named <type>-fields. The fields in the subsection of the payload extracted by the given subsectionExtractor will be documented using the given descriptors and the given attributes will be included in the model during template rendering. If ignoreUndocumentedFields is true, undocumented fields will be ignored and will not trigger a failure.
      Parameters:
      type - the type of the fields
      descriptors - the field descriptors
      attributes - the additional attributes
      ignoreUndocumentedFields - whether undocumented fields should be ignored
      subsectionExtractor - the subsection extractor
      Since:
      1.2.0
    • AbstractFieldsSnippet

      protected AbstractFieldsSnippet(String name, String type, List<FieldDescriptor> descriptors, Map<String,Object> attributes, boolean ignoreUndocumentedFields)
      Creates a new AbstractFieldsSnippet that will produce a snippet named <name>-fields using a template named <type>-fields. The fields will be documented using the given descriptors and the given attributes will be included in the model during template rendering. If ignoreUndocumentedFields is true, undocumented fields will be ignored and will not trigger a failure.
      Parameters:
      name - the name of the snippet
      type - the type of the fields
      descriptors - the field descriptors
      attributes - the additional attributes
      ignoreUndocumentedFields - whether undocumented fields should be ignored
    • AbstractFieldsSnippet

      protected AbstractFieldsSnippet(String name, String type, List<FieldDescriptor> descriptors, Map<String,Object> attributes, boolean ignoreUndocumentedFields, PayloadSubsectionExtractor<?> subsectionExtractor)
      Creates a new AbstractFieldsSnippet that will produce a snippet named <name>-fields using a template named <type>-fields. The fields in the subsection of the payload identified by subsectionPath will be documented using the given descriptors and the given attributes will be included in the model during template rendering. If ignoreUndocumentedFields is true, undocumented fields will be ignored and will not trigger a failure.
      Parameters:
      name - the name of the snippet
      type - the type of the fields
      descriptors - the field descriptors
      attributes - the additional attributes
      ignoreUndocumentedFields - whether undocumented fields should be ignored
      subsectionExtractor - the subsection extractor documented. null or an empty string can be used to indicate that the entire payload should be documented.
      Since:
      1.2.0
  • Method Details

    • createModel

      protected Map<String,Object> createModel(Operation operation)
      Description copied from class: TemplatedSnippet
      Create the model that should be used during template rendering to document the given operation. Any additional attributes that were supplied when this TemplatedSnippet were created will be automatically added to the model prior to rendering.
      Specified by:
      createModel in class TemplatedSnippet
      Parameters:
      operation - the operation
      Returns:
      the model
    • getContentType

      protected abstract org.springframework.http.MediaType getContentType(Operation operation)
      Returns the content type of the request or response extracted from the given operation.
      Parameters:
      operation - the operation
      Returns:
      the content type
    • getContent

      protected abstract byte[] getContent(Operation operation) throws IOException
      Returns the content of the request or response extracted form the given operation.
      Parameters:
      operation - the operation
      Returns:
      the content
      Throws:
      IOException - if the content cannot be extracted
    • getFieldDescriptors

      protected final List<FieldDescriptor> getFieldDescriptors()
      Returns the list of FieldDescriptors that will be used to generate the documentation.
      Returns:
      the field descriptors
    • isIgnoredUndocumentedFields

      protected final boolean isIgnoredUndocumentedFields()
      Returns whether or not this snippet ignores undocumented fields.
      Returns:
      true if undocumented fields are ignored, otherwise false
    • getSubsectionExtractor

      protected final PayloadSubsectionExtractor<?> getSubsectionExtractor()
      Returns the PayloadSubsectionExtractor, if any, used by this snippet.
      Returns:
      the subsection extractor or null
      Since:
      1.2.4
    • createModelForDescriptor

      protected Map<String,Object> createModelForDescriptor(FieldDescriptor descriptor)
      Returns a model for the given descriptor.
      Parameters:
      descriptor - the descriptor
      Returns:
      the model