Search in sources :

Example 1 with SwaggerExtension

use of io.swagger.jaxrs.ext.SwaggerExtension in project swagger-core by swagger-api.

the class DefaultParameterExtension method handleAdditionalAnnotation.

/**
     * Adds additional annotation processing support 
     * 
     * @param parameters
     * @param annotation
     * @param type
     * @param typesToSkip
     */
private void handleAdditionalAnnotation(List<Parameter> parameters, Annotation annotation, final Type type, Set<Type> typesToSkip) {
    if (CLASS_BEAN_PARAM != null && CLASS_BEAN_PARAM.isAssignableFrom(annotation.getClass())) {
        // 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 AnnotatedMethod getter = propDef.getGetter();
            final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
            final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
            Type paramType = null;
            // Gather the field's details
            if (field != null) {
                paramType = field.getRawType();
                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) {
                    paramType = setter.getRawParameterTypes() != null ? setter.getRawParameterTypes()[0] : null;
                }
                for (final Annotation fieldAnnotation : setter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the getter's details but only the ones we need
            if (getter != null) {
                // Do not set the param class/type from the getter if the values are already identified
                if (paramType == null) {
                    paramType = getter.getRawReturnType();
                }
                for (final Annotation fieldAnnotation : getter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            if (paramType == null) {
                continue;
            }
            // Re-process all Bean fields and let the default swagger-jaxrs/swagger-jersey-jaxrs processors do their 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) {
                    parameters.add(param);
                }
            }
        }
    }
}
Also used : 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) AbstractSwaggerExtension(io.swagger.jaxrs.ext.AbstractSwaggerExtension) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HeaderParameter(io.swagger.models.parameters.HeaderParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Example 2 with SwaggerExtension

use of io.swagger.jaxrs.ext.SwaggerExtension in project swagger-core by swagger-api.

the class Reader method getParameters.

private List<Parameter> getParameters(Type type, List<Annotation> annotations) {
    final Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
    if (!chain.hasNext()) {
        return Collections.emptyList();
    }
    LOGGER.debug("getParameters for {}", type);
    Set<Type> typesToSkip = new HashSet<Type>();
    final SwaggerExtension extension = chain.next();
    LOGGER.debug("trying extension {}", extension);
    final List<Parameter> parameters = extension.extractParameters(annotations, type, typesToSkip, chain);
    if (!parameters.isEmpty()) {
        final List<Parameter> processed = new ArrayList<Parameter>(parameters.size());
        for (Parameter parameter : parameters) {
            if (ParameterProcessor.applyAnnotations(swagger, parameter, type, annotations) != null) {
                processed.add(parameter);
            }
        }
        return processed;
    } else {
        LOGGER.debug("no parameter found, looking at body params");
        final List<Parameter> body = new ArrayList<Parameter>();
        if (!typesToSkip.contains(type)) {
            Parameter param = ParameterProcessor.applyAnnotations(swagger, null, type, annotations);
            if (param != null) {
                body.add(param);
            }
        }
        return body;
    }
}
Also used : Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ParameterizedType(java.lang.reflect.ParameterizedType) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) ArrayList(java.util.ArrayList) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with SwaggerExtension

use of io.swagger.jaxrs.ext.SwaggerExtension in project swagger-core by swagger-api.

the class Reader method processOperationDecorator.

private void processOperationDecorator(Operation operation, Method method) {
    final Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
    if (chain.hasNext()) {
        SwaggerExtension extension = chain.next();
        LOGGER.debug("trying to decorate operation: {}", extension);
        extension.decorateOperation(operation, method, chain);
    }
}
Also used : SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension)

Aggregations

SwaggerExtension (io.swagger.jaxrs.ext.SwaggerExtension)3 FormParameter (io.swagger.models.parameters.FormParameter)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)2 Parameter (io.swagger.models.parameters.Parameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)1 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)1 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 AbstractSwaggerExtension (io.swagger.jaxrs.ext.AbstractSwaggerExtension)1 CookieParameter (io.swagger.models.parameters.CookieParameter)1 Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1