Search in sources :

Example 21 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition 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().getType();
                    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)

Example 22 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project requery by requery.

the class DeserializerModifier method updateProperties.

@Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) {
    List<BeanPropertyDefinition> definitions = super.updateProperties(config, beanDesc, propDefs);
    List<BeanPropertyDefinition> remove = new ArrayList<>();
    List<BeanPropertyDefinition> add = new ArrayList<>();
    for (BeanPropertyDefinition definition : definitions) {
        if (definition.hasGetter() && Collection.class.isAssignableFrom(definition.getGetter().getRawType())) {
            if (definition instanceof POJOPropertyBuilder) {
                POJOPropertyBuilder builder = (POJOPropertyBuilder) definition;
                builder = new POJOPropertyBuilder(builder, builder.getFullName()) {

                    @Override
                    public boolean hasField() {
                        // forces the getter to be used on the collection
                        return false;
                    }
                };
                remove.add(definition);
                add.add(builder);
            }
        }
    }
    definitions.removeAll(remove);
    definitions.addAll(add);
    return definitions;
}
Also used : BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ArrayList(java.util.ArrayList) POJOPropertyBuilder(com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder) Collection(java.util.Collection)

Example 23 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.

the class JsonSerialisationUtil method getSerialisedFieldClasses.

/**
 * Gets all the fields and their classes for a given class.
 *
 * @param className the class name to find the fields for.
 * @return a map of field name to class name
 */
public static Map<String, String> getSerialisedFieldClasses(final String className) {
    final Map<String, String> cachedResult = cache.get(className);
    if (null != cachedResult) {
        return cachedResult;
    }
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
    } catch (final Exception e) {
        throw new IllegalArgumentException("Class name was not recognised: " + className, e);
    }
    final ObjectMapper mapper = new ObjectMapper();
    final JavaType type = mapper.getTypeFactory().constructType(clazz);
    final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
    final Class<?> builder = introspection.findPOJOBuilder();
    String buildMethodPrefix = "with";
    if (null != builder) {
        JsonPOJOBuilder anno = findAnnotation(builder, JsonPOJOBuilder.class);
        if (null != anno) {
            buildMethodPrefix = anno.withPrefix();
        }
    }
    Constructor<?> creator = null;
    for (final Constructor<?> constructor : type.getRawClass().getDeclaredConstructors()) {
        final JsonCreator anno = constructor.getAnnotation(JsonCreator.class);
        if (null != anno) {
            creator = constructor;
            break;
        }
    }
    final List<BeanPropertyDefinition> properties = introspection.findProperties();
    final Map<String, String> fieldMap = new HashMap<>();
    for (final BeanPropertyDefinition property : properties) {
        final String propName = property.getName();
        final String propClass;
        if ("class".equals(propName)) {
            propClass = Class.class.getName();
        } else {
            Type genericType = null;
            if (null != builder) {
                final String methodName = buildMethodPrefix + propName;
                Method matchedMethod = null;
                for (final Method method : builder.getMethods()) {
                    if (methodName.equalsIgnoreCase(method.getName())) {
                        final Type[] params = method.getGenericParameterTypes();
                        if (null != params && 1 == params.length) {
                            final JsonSetter jsonSetter = method.getAnnotation(JsonSetter.class);
                            if (null != jsonSetter && propName.equals(jsonSetter.value())) {
                                matchedMethod = method;
                                break;
                            }
                            final JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class);
                            if (null != jsonProperty && propName.equals(jsonProperty.value())) {
                                matchedMethod = method;
                                break;
                            }
                            if (null == matchedMethod) {
                                matchedMethod = method;
                            } else if (builder.equals(method.getReturnType())) {
                                // Checks for overridden methods
                                matchedMethod = method;
                            }
                        }
                    }
                }
                if (null != matchedMethod) {
                    genericType = matchedMethod.getGenericParameterTypes()[0];
                }
            }
            if (null == genericType && null != creator) {
                for (final Parameter parameter : creator.getParameters()) {
                    final JsonProperty anno = parameter.getAnnotation(JsonProperty.class);
                    if (null != anno && propName.equals(anno.value())) {
                        if (null != parameter.getParameterizedType()) {
                            genericType = parameter.getParameterizedType();
                        } else {
                            genericType = parameter.getType();
                        }
                        break;
                    }
                }
            }
            if (null == genericType && null != property.getSetter() && null != property.getSetter().getGenericParameterTypes() && 1 == property.getSetter().getGenericParameterTypes().length) {
                genericType = property.getSetter().getGenericParameterTypes()[0];
            }
            if (null != genericType && genericType instanceof Class && ((Class) genericType).isEnum()) {
                genericType = String.class;
            }
            if (null == genericType) {
                propClass = Object.class.getName();
            } else {
                propClass = getFieldTypeString(clazz, genericType);
            }
        }
        fieldMap.put(propName, propClass);
    }
    final Map<String, Map<String, String>> newCache = new HashMap<>(cache);
    newCache.put(className, Collections.unmodifiableMap(fieldMap));
    cache = Collections.unmodifiableMap(newCache);
    return fieldMap;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) HashMap(java.util.HashMap) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Method(java.lang.reflect.Method) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) JsonPOJOBuilder(com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder) Parameter(java.lang.reflect.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 24 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.

the class GraphConfigurationServiceV2 method getSerialisedFields.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Response getSerialisedFields(final String className) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
    } catch (final Exception e) {
        throw new IllegalArgumentException("Class name was not recognised: " + className, e);
    }
    final ObjectMapper mapper = new ObjectMapper();
    final JavaType type = mapper.getTypeFactory().constructType(clazz);
    final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
    final List<BeanPropertyDefinition> properties = introspection.findProperties();
    final Set<String> fields = new HashSet<>();
    for (final BeanPropertyDefinition property : properties) {
        fields.add(property.getName());
    }
    return Response.ok(fields).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 25 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.

the class GraphConfigurationService method getSerialisedFields.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<String> getSerialisedFields(final String className) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
    } catch (final Exception e) {
        throw new IllegalArgumentException("Class name was not recognised: " + className, e);
    }
    final ObjectMapper mapper = new ObjectMapper();
    final JavaType type = mapper.getTypeFactory().constructType(clazz);
    final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
    final List<BeanPropertyDefinition> properties = introspection.findProperties();
    final Set<String> fields = new HashSet<>();
    for (final BeanPropertyDefinition property : properties) {
        fields.add(property.getName());
    }
    return fields;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)32 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)24 JavaType (com.fasterxml.jackson.databind.JavaType)21 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ArrayList (java.util.ArrayList)10 Annotation (java.lang.annotation.Annotation)8 Type (java.lang.reflect.Type)6 HashSet (java.util.HashSet)6 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)5 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)3 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)3 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)3 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)3 POJOPropertyBuilder (com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)2