capital.scalable.restdocs.payload.AbstractJacksonFieldSnippet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-auto-restdocs-core Show documentation
Show all versions of spring-auto-restdocs-core Show documentation
Spring Auto REST Docs is an extension to Spring REST Docs
/*-
* #%L
* Spring Auto REST Docs Core
* %%
* Copyright (C) 2015 - 2018 Scalable Capital GmbH
* %%
* 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.
* #L%
*/
package capital.scalable.restdocs.payload;
import static capital.scalable.restdocs.OperationAttributeHelper.getConstraintReader;
import static capital.scalable.restdocs.OperationAttributeHelper.getHandlerMethod;
import static capital.scalable.restdocs.OperationAttributeHelper.getJavadocReader;
import static capital.scalable.restdocs.OperationAttributeHelper.getObjectMapper;
import static capital.scalable.restdocs.i18n.SnippetTranslationResolver.translate;
import static capital.scalable.restdocs.util.FieldDescriptorUtil.assertAllDocumented;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import capital.scalable.restdocs.constraints.ConstraintReader;
import capital.scalable.restdocs.jackson.FieldDocumentationGenerator;
import capital.scalable.restdocs.javadoc.JavadocReader;
import capital.scalable.restdocs.section.SectionSupport;
import capital.scalable.restdocs.snippet.StandardTableSnippet;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.MethodParameter;
import org.springframework.restdocs.operation.Operation;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.web.method.HandlerMethod;
abstract class AbstractJacksonFieldSnippet extends StandardTableSnippet implements SectionSupport {
private static Class> SCALA_TRAVERSABLE;
static {
try {
SCALA_TRAVERSABLE = Class.forName("scala.collection.Traversable");
} catch (ClassNotFoundException ignored) {
// It's fine to not be available outside of Scala projects.
}
}
protected AbstractJacksonFieldSnippet(String snippetName, Map attributes) {
super(snippetName, attributes);
}
protected Collection createFieldDescriptors(Operation operation,
HandlerMethod handlerMethod) {
ObjectMapper objectMapper = getObjectMapper(operation);
JavadocReader javadocReader = getJavadocReader(operation);
ConstraintReader constraintReader = getConstraintReader(operation);
Map fieldDescriptors = new LinkedHashMap<>();
Type type = getType(handlerMethod);
if (type != null) {
try {
resolveFieldDescriptors(fieldDescriptors, type, objectMapper,
javadocReader, constraintReader);
} catch (JsonMappingException e) {
throw new JacksonFieldProcessingException("Error while parsing fields", e);
}
}
if (shouldFailOnUndocumentedFields()) {
assertAllDocumented(fieldDescriptors.values(), translate(getHeaderKey()).toLowerCase());
}
return fieldDescriptors.values();
}
protected Type firstGenericType(MethodParameter param) {
Type type = param.getGenericParameterType();
if (type != null && type instanceof ParameterizedType) {
return ((ParameterizedType) type).getActualTypeArguments()[0];
} else {
return Object.class;
}
}
protected abstract Type getType(HandlerMethod method);
protected abstract boolean shouldFailOnUndocumentedFields();
protected boolean isCollection(Class> type) {
return Collection.class.isAssignableFrom(type) ||
(SCALA_TRAVERSABLE != null && SCALA_TRAVERSABLE.isAssignableFrom(type));
}
private void resolveFieldDescriptors(Map fieldDescriptors,
Type type, ObjectMapper objectMapper, JavadocReader javadocReader,
ConstraintReader constraintReader) throws JsonMappingException {
FieldDocumentationGenerator generator = new FieldDocumentationGenerator(
objectMapper.writer(), objectMapper.getDeserializationConfig(), javadocReader,
constraintReader);
List descriptors = generator.generateDocumentation(type,
objectMapper.getTypeFactory());
for (FieldDescriptor descriptor : descriptors) {
if (fieldDescriptors.get(descriptor.getPath()) == null) {
fieldDescriptors.put(descriptor.getPath(), descriptor);
}
}
}
@Override
public String getFileName() {
return getSnippetName();
}
@Override
public boolean hasContent(Operation operation) {
return getType(getHandlerMethod(operation)) != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy