Provide a public API for resolving the type of a field
See gh-549
This commit is contained in:
committed by
Andy Wilkinson
parent
ada273ba9e
commit
4453fde23c
@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Andreas Evers
|
||||
* @author Andy Wilkinson
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
|
||||
|
||||
@@ -160,7 +161,7 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
|
||||
content = verifyContent(
|
||||
this.subsectionExtractor.extractSubsection(content, contentType));
|
||||
}
|
||||
ContentHandler contentHandler = getContentHandler(content, contentType);
|
||||
ContentHandler contentHandler = ContentHandler.forContent(content, contentType);
|
||||
|
||||
validateFieldDocumentation(contentHandler);
|
||||
|
||||
@@ -168,7 +169,7 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
|
||||
for (FieldDescriptor descriptor : this.fieldDescriptors) {
|
||||
if (!descriptor.isIgnored()) {
|
||||
try {
|
||||
Object type = contentHandler.determineFieldType(descriptor);
|
||||
Object type = contentHandler.resolveFieldType(descriptor);
|
||||
descriptorsToDocument.add(copyWithType(descriptor, type));
|
||||
}
|
||||
catch (FieldDoesNotExistException ex) {
|
||||
@@ -200,36 +201,6 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
|
||||
return content;
|
||||
}
|
||||
|
||||
private ContentHandler getContentHandler(byte[] content, MediaType contentType) {
|
||||
ContentHandler contentHandler = createJsonContentHandler(content);
|
||||
if (contentHandler == null) {
|
||||
contentHandler = createXmlContentHandler(content);
|
||||
if (contentHandler == null) {
|
||||
throw new PayloadHandlingException("Cannot handle " + contentType
|
||||
+ " content as it could not be parsed as JSON or XML");
|
||||
}
|
||||
}
|
||||
return contentHandler;
|
||||
}
|
||||
|
||||
private ContentHandler createJsonContentHandler(byte[] content) {
|
||||
try {
|
||||
return new JsonContentHandler(content);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ContentHandler createXmlContentHandler(byte[] content) {
|
||||
try {
|
||||
return new XmlContentHandler(content);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void validateFieldDocumentation(ContentHandler payloadHandler) {
|
||||
List<FieldDescriptor> missingFields = payloadHandler
|
||||
.findMissingFields(this.fieldDescriptors);
|
||||
|
||||
@@ -18,12 +18,15 @@ package org.springframework.restdocs.payload;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* A handler for the content of a request or response.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
interface ContentHandler {
|
||||
interface ContentHandler extends FieldTypeResolver {
|
||||
|
||||
/**
|
||||
* Finds the fields that are missing from the handler's payload. A field is missing if
|
||||
@@ -48,11 +51,26 @@ interface ContentHandler {
|
||||
String getUndocumentedContent(List<FieldDescriptor> fieldDescriptors);
|
||||
|
||||
/**
|
||||
* Returns the type of the field that is described by the given
|
||||
* {@code fieldDescriptor} based on the content of the payload.
|
||||
* @param fieldDescriptor the field descriptor
|
||||
* @return the type of the field
|
||||
* Create a {@link ContentHandler} for the given content type and payload.
|
||||
* @param content the payload
|
||||
* @param contentType the content type
|
||||
* @return the ContentHandler
|
||||
* @throws PayloadHandlingException if no known ContentHandler can handle the content
|
||||
*/
|
||||
Object determineFieldType(FieldDescriptor fieldDescriptor);
|
||||
static ContentHandler forContent(byte[] content, MediaType contentType) {
|
||||
|
||||
try {
|
||||
return new JsonContentHandler(content);
|
||||
}
|
||||
catch (Exception je) {
|
||||
try {
|
||||
return new XmlContentHandler(content);
|
||||
}
|
||||
catch (Exception xe) {
|
||||
throw new PayloadHandlingException("Cannot handle " + contentType
|
||||
+ " content as it could not be parsed as JSON or XML");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.restdocs.payload;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Public abstraction for external access to field type determination for xml and json
|
||||
* payloads.
|
||||
*
|
||||
* @author Mathias Düsterhöft
|
||||
* @since 2.0.3
|
||||
*/
|
||||
public interface FieldTypeResolver {
|
||||
|
||||
/**
|
||||
* Create a FieldTypeResolver for the given content and contentType.
|
||||
* @param content the payload that the {@link FieldTypeResolver} should handle
|
||||
* @param contentType the content type of the payload
|
||||
* @return the {@link FieldTypeResolver}
|
||||
*/
|
||||
static FieldTypeResolver forContent(byte[] content, MediaType contentType) {
|
||||
return ContentHandler.forContent(content, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the field that is described by the given
|
||||
* {@code fieldDescriptor} based on the content of the payload.
|
||||
* @param fieldDescriptor the field descriptor
|
||||
* @return the type of the field
|
||||
*/
|
||||
Object resolveFieldType(FieldDescriptor fieldDescriptor);
|
||||
|
||||
}
|
||||
@@ -32,8 +32,9 @@ import org.springframework.restdocs.payload.JsonFieldProcessor.ExtractedField;
|
||||
* A {@link ContentHandler} for JSON content.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
class JsonContentHandler implements ContentHandler {
|
||||
class JsonContentHandler implements ContentHandler, FieldTypeResolver {
|
||||
|
||||
private final JsonFieldProcessor fieldProcessor = new JsonFieldProcessor();
|
||||
|
||||
@@ -145,7 +146,7 @@ class JsonContentHandler implements ContentHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object determineFieldType(FieldDescriptor fieldDescriptor) {
|
||||
public Object resolveFieldType(FieldDescriptor fieldDescriptor) {
|
||||
if (fieldDescriptor.getType() == null) {
|
||||
return this.fieldTypesDiscoverer
|
||||
.discoverFieldTypes(fieldDescriptor.getPath(), readContent())
|
||||
|
||||
@@ -190,7 +190,7 @@ class XmlContentHandler implements ContentHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object determineFieldType(FieldDescriptor fieldDescriptor) {
|
||||
public Object resolveFieldType(FieldDescriptor fieldDescriptor) {
|
||||
if (fieldDescriptor.getType() != null) {
|
||||
return fieldDescriptor.getType();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user