Search in sources :

Example 11 with AnnotatedField

use of com.fasterxml.jackson.databind.introspect.AnnotatedField in project cxf by apache.

the class JaxRs2Extension method extractParameters.

@SuppressWarnings("deprecation")
@Override
public List<Parameter> extractParameters(final List<Annotation> annotations, final Type type, final Set<Type> typesToSkip, final Iterator<SwaggerExtension> chain) {
    if (shouldIgnoreType(type, typesToSkip)) {
        return new ArrayList<>();
    }
    List<Parameter> parameters = new ArrayList<>();
    for (Annotation annotation : annotations) {
        if (annotation instanceof MatrixParam) {
            MatrixParam param = (MatrixParam) annotation;
            MatrixParameter mp = new MatrixParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                mp.setProperty(schema);
            }
            applyBeanValidatorAnnotations(mp, annotations);
            parameters.add(mp);
        } else if (annotation instanceof BeanParam) {
            // Use Jackson's logic for processing Beans
            final BeanDescription beanDesc = mapper.getSerializationConfig().introspect(constructType(type));
            final List<BeanPropertyDefinition> properties = beanDesc.findProperties();
            for (final BeanPropertyDefinition propDef : properties) {
                final AnnotatedField field = propDef.getField();
                final AnnotatedMethod setter = propDef.getSetter();
                final List<Annotation> paramAnnotations = new ArrayList<>();
                final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
                Type paramType = null;
                // Gather the field's details
                if (field != null) {
                    paramType = field.getAnnotated().getGenericType();
                    for (final Annotation fieldAnnotation : field.annotations()) {
                        if (!paramAnnotations.contains(fieldAnnotation)) {
                            paramAnnotations.add(fieldAnnotation);
                        }
                    }
                }
                // Gather the setter's details but only the ones we need
                if (setter != null) {
                    // Do not set the param class/type from the setter if the values are already identified
                    if (paramType == null && setter.getMember().getGenericParameterTypes() != null) {
                        paramType = setter.getMember().getGenericParameterTypes()[0];
                    }
                    for (final Annotation fieldAnnotation : setter.annotations()) {
                        if (!paramAnnotations.contains(fieldAnnotation)) {
                            paramAnnotations.add(fieldAnnotation);
                        }
                    }
                }
                // Re-process all Bean fields and let the default swagger-jaxrs processor do its thing
                List<Parameter> extracted = extensions.next().extractParameters(paramAnnotations, paramType, typesToSkip, extensions);
                // since downstream processors won't know how to introspect @BeanParam, process here
                for (Parameter param : extracted) {
                    if (ParameterProcessor.applyAnnotations(null, param, paramType, paramAnnotations) != null) {
                        applyBeanValidatorAnnotations(param, paramAnnotations);
                        parameters.add(param);
                    }
                }
            }
        }
    }
    // Only call down to the other items in the chain if no parameters were produced
    if (parameters.isEmpty()) {
        parameters = super.extractParameters(annotations, type, typesToSkip, chain);
    }
    return parameters;
}
Also used : MatrixParam(javax.ws.rs.MatrixParam) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) Iterator(java.util.Iterator) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) Parameter(io.swagger.models.parameters.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) BeanParam(javax.ws.rs.BeanParam)

Aggregations

AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)11 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)4 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)2 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)2 Parameter (io.swagger.models.parameters.Parameter)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1 AbstractSwaggerExtension (io.swagger.jaxrs.ext.AbstractSwaggerExtension)1 SwaggerExtension (io.swagger.jaxrs.ext.SwaggerExtension)1 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)1 CookieParameter (io.swagger.models.parameters.CookieParameter)1 FormParameter (io.swagger.models.parameters.FormParameter)1 HeaderParameter (io.swagger.models.parameters.HeaderParameter)1 PathParameter (io.swagger.models.parameters.PathParameter)1