Refine search
@Nullable @SuppressWarnings("unchecked") @Override public Object convertToMongoType(@Nullable Object obj, TypeInformation<?> typeInformation) { if (target.isPresent()) { return conversionService.convert(obj, target.get()); if (typeInformation.getType().equals(NestedDocument.class)) { return removeTypeInfo(newDocument, false); return !obj.getClass().equals(typeInformation.getType()) ? newDocument : removeTypeInfo(newDocument, true);
@Nullable @SuppressWarnings("unchecked") private <S extends Object> S read(TypeInformation<S> type, @Nullable Bson bson, ObjectPath path) { Class<? extends S> rawType = typeToUse.getType(); if (typeToUse.isCollectionLike() && bson instanceof List) { return (S) readCollectionOrArray(typeToUse, (List<?>) bson, path); if (typeToUse.isMap()) { return (S) readMap(typeToUse, bson, path); throw new MappingException(String.format(INCOMPATIBLE_TYPES, bson, BasicDBList.class, typeToUse.getType(), path)); throw new MappingException(String.format(INVALID_TYPE_TO_READ, target, typeToUse.getType()));
private void writeInternal(String keyspace, String path, @Nullable Object value, TypeInformation<?> typeHint, RedisData sink) { if (!StringUtils.hasText(path) && customConversions.getCustomWriteTarget(value.getClass()).equals(byte[].class)) { sink.getBucket().put(StringUtils.hasText(path) ? path : "_raw", conversionService.convert(value, byte[].class)); } else { if (!ClassUtils.isAssignable(typeHint.getType(), value.getClass())) { throw new MappingException( String.format(INVALID_TYPE_ASSIGNMENT, value.getClass(), path, typeHint.getType())); writeToBucket(path, value, sink, typeHint.getType()); if (value.getClass() != typeHint.getType()) { typeMapper.writeType(value.getClass(), sink.getBucket().getPropertyPath(path)); persistentProperty.getTypeInformation().getRequiredComponentType(), sink); } else { persistentProperty.getTypeInformation().getRequiredComponentType(), sink); } else if (propertyValue.getClass().isArray()) { persistentProperty.getTypeInformation().getRequiredComponentType(), sink); } else { persistentProperty.getTypeInformation().getActualType(), sink);
private static boolean isListOfGeoResult(TypeInformation<?> returnType) { if (!returnType.getType().equals(List.class)) { return false; } TypeInformation<?> componentType = returnType.getComponentType(); return componentType != null && GeoResult.class.equals(componentType.getType()); } }
private int getTypeIndex(List<TypeInformation<?>> parameterTypes, Class<?> type, @Nullable Class<?> componentType) { for (int i = 0; i < parameterTypes.size(); i++) { TypeInformation<?> candidate = parameterTypes.get(i); if (candidate.getType().equals(type)) { if (componentType == null) { return i; } else if (componentType.equals(candidate.getRequiredComponentType().getType())) { return i; } } } return -1; }
private boolean isGeoNearQuery(Method method) { Class<?> returnType = method.getReturnType(); for (Class<?> type : GEO_NEAR_RESULTS) { if (type.isAssignableFrom(returnType)) { return true; } } if (Iterable.class.isAssignableFrom(returnType)) { TypeInformation<?> from = ClassTypeInformation.fromReturnTypeOf(method); return GeoResult.class.equals(from.getRequiredComponentType().getType()); } return false; }
private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata, final Definitions definitions) { return entities.getPersistentEntity(type).map(entity -> { .ofNullable(entity.getPersistentProperty(definition.getInternalName())); if (prop.isPresent()) { PersistentProperty<?> persistentProperty = prop.get(); TypeInformation<?> actualPropertyType = propertyType.getActualType(); Class<?> rawPropertyType = propertyType.getType();
@SuppressWarnings("unchecked") QueryPostProcessorMetadata(@NonNull QueryPostProcessor<?, ?> queryPostProcessor) { Assert.notNull(queryPostProcessor, "QueryPostProcessor must not be null"); this.queryPostProcessor = queryPostProcessor; List<TypeInformation<?>> typeArguments = ClassTypeInformation.from(queryPostProcessor.getClass()) .getRequiredSuperTypeInformation(QueryPostProcessor.class) .getTypeArguments(); this.declaredRepositoryType = Optional.of(typeArguments) .filter(list -> !list.isEmpty()) .map(list -> list.get(0)) .map(typeInfo -> typeInfo.getType()) .orElse((Class) Repository.class); }
@SuppressWarnings({ "unchecked" }) protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor accessor, MongoPersistentProperty prop) { TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass()); TypeInformation<?> type = prop.getTypeInformation(); if (valueType.isCollectionLike()) { List<Object> collectionInternal = createCollection(asCollection(obj), prop); accessor.put(prop, collectionInternal); if (valueType.isMap()) { Bson mapDbObj = createMap((Map<Object, Object>) obj, prop); accessor.put(prop, mapDbObj); if (basicTargetType.isPresent()) { accessor.put(prop, conversionService.convert(obj, basicTargetType.get())); return; MongoPersistentEntity<?> entity = valueType.isSubTypeOf(prop.getType()) ? mappingContext.getRequiredPersistentEntity(obj.getClass()) : mappingContext.getRequiredPersistentEntity(type); addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, document); accessor.put(prop, document);
/** * Creates a new {@link UriToEntityConverter} using the given {@link PersistentEntities}, * {@link RepositoryInvokerFactory} and {@link Repositories}. * * @param entities must not be {@literal null}. * @param invokerFactory must not be {@literal null}. * @param repositories must not be {@literal null}. */ public UriToEntityConverter(PersistentEntities entities, RepositoryInvokerFactory invokerFactory, Repositories repositories) { Assert.notNull(entities, "PersistentEntities must not be null!"); Assert.notNull(invokerFactory, "RepositoryInvokerFactory must not be null!"); Assert.notNull(repositories, "Repositories must not be null!"); Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>(); for (TypeInformation<?> domainType : entities.getManagedTypes()) { Class<?> rawType = domainType.getType(); Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(rawType); if (entity.map(it -> it.hasIdProperty()).orElse(false)) { convertiblePairs.add(new ConvertiblePair(URI.class, domainType.getType())); } } this.convertiblePairs = Collections.unmodifiableSet(convertiblePairs); this.entities = entities; this.invokerFactory = invokerFactory; this.repositories = repositories; }
private static Optional<Method> findWither(TypeInformation<?> owner, String propertyName, Class<?> rawType) { AtomicReference<Method> resultHolder = new AtomicReference<>(); String methodName = String.format("with%s", StringUtils.capitalize(propertyName)); ReflectionUtils.doWithMethods(owner.getType(), it -> { if (owner.isAssignableFrom(owner.getReturnType(it))) { resultHolder.set(it); } }, it -> isMethodWithSingleParameterOfType(it, methodName, rawType)); Method method = resultHolder.get(); return method != null ? Optional.of(method) : Optional.empty(); }
/** * Creates a new {@link PersistentEntityToJsonSchemaConverter} for the given {@link PersistentEntities} and * {@link ResourceMappings}. * * @param entities must not be {@literal null}. * @param mappings must not be {@literal null}. * @param accessor must not be {@literal null}. * @param objectMapper must not be {@literal null}. * @param configuration must not be {@literal null}. */ public PersistentEntityToJsonSchemaConverter(PersistentEntities entities, Associations associations, MessageSourceAccessor accessor, ObjectMapper objectMapper, RepositoryRestConfiguration configuration, ValueTypeSchemaPropertyCustomizerFactory customizerFactory) { Assert.notNull(entities, "PersistentEntities must not be null!"); Assert.notNull(associations, "AssociationLinks must not be null!"); Assert.notNull(accessor, "MessageSourceAccessor must not be null!"); Assert.notNull(objectMapper, "ObjectMapper must not be null!"); Assert.notNull(configuration, "RepositoryRestConfiguration must not be null!"); this.entities = entities; this.associations = associations; this.objectMapper = objectMapper; this.configuration = configuration; this.customizerFactory = customizerFactory; this.resolver = new DefaultMessageResolver(accessor, configuration); for (TypeInformation<?> domainType : entities.getManagedTypes()) { convertiblePairs.add(new ConvertiblePair(domainType.getType(), JsonSchema.class)); } }
protected Map<Object, Object> readMap(TypeInformation<?> type, Bson bson, ObjectPath path) { Assert.notNull(bson, "Document must not be null!"); Assert.notNull(path, "Object path must not be null!"); Class<?> mapType = typeMapper.readType(bson, type).getType(); TypeInformation<?> keyType = type.getComponentType(); TypeInformation<?> valueType = type.getMapValueType(); Class<?> rawKeyType = keyType != null ? keyType.getType() : null; Class<?> rawValueType = valueType != null ? valueType.getType() : null;
private Object readCollectionOrArray(TypeInformation<?> targetType, Collection<?> source, ObjectPath path) { Assert.notNull(targetType, "Target type must not be null!"); Assert.notNull(path, "Object path must not be null!"); Class<?> collectionType = targetType.isSubTypeOf(Collection.class) // ? targetType.getType() // : List.class; TypeInformation<?> componentType = targetType.getComponentType() != null // ? targetType.getComponentType() // : ClassTypeInformation.OBJECT; Class<?> rawComponentType = componentType.getType(); Collection<Object> items = targetType.getType().isArray() // ? new ArrayList<>(source.size()) // : CollectionFactory.createCollection(collectionType, rawComponentType, source.size()); return getPotentiallyConvertedSimpleRead(items, targetType.getType()); return getPotentiallyConvertedSimpleRead(objects, targetType.getType()); return getPotentiallyConvertedSimpleRead(items, targetType.getType());
/** * Configures the {@link JsonSchemaProperty} to reflect the given type. * * @param type must not be {@literal null}. * @return */ public JsonSchemaProperty with(TypeInformation<?> type) { Assert.notNull(type, "Type must not be null!"); this.type = toJsonSchemaType(type); if (isDate(type)) { return withFormat(JsonSchemaFormat.DATE_TIME); } if (type.isCollectionLike()) { if (Set.class.equals(type.getType())) { this.uniqueItems = true; } this.items = Collections.singletonMap("type", toJsonSchemaType(type.getActualType())); } return this; }
private Object readCollectionOrArray(TypeInformation<?> targetType, Collection<?> source, ObjectPath path) { Assert.notNull(targetType, "Target type must not be null!"); Assert.notNull(path, "Object path must not be null!"); Class<?> collectionType = targetType.getType(); collectionType = isSubTypeOf(collectionType, Collection.class) // TypeInformation<?> componentType = targetType.getComponentType() != null // ? targetType.getComponentType() // : ClassTypeInformation.OBJECT; Class<?> rawComponentType = componentType.getType(); Collection<Object> items = targetType.getType().isArray() // ? new ArrayList<>(source.size()) // : CollectionFactory.createCollection(collectionType, rawComponentType, source.size()); return getPotentiallyConvertedSimpleRead(items, targetType.getType()); return getPotentiallyConvertedSimpleRead(objects, targetType.getType()); return getPotentiallyConvertedSimpleRead(items, targetType.getType());
@SuppressWarnings("unchecked") public <T> TypeInformation<? extends T> readType(S source, TypeInformation<T> basicType) { Assert.notNull(source); Class<?> documentsTargetType = getDefaultedTypeToBeUsed(source); if (documentsTargetType == null) { return basicType; } Class<T> rawType = basicType == null ? null : basicType.getType(); boolean isMoreConcreteCustomType = rawType == null ? true : rawType.isAssignableFrom(documentsTargetType) && !rawType.equals(documentsTargetType); return isMoreConcreteCustomType ? (TypeInformation<? extends T>) ClassTypeInformation.from(documentsTargetType) : basicType; }
private List<Object> convertCollection(final Collection<?> source, final TypeInformation<?> type) { Assert.notNull(source, "Given collection must not be null!"); Assert.notNull(type, "Given type must not be null!"); TypeInformation<?> componentType = type.getComponentType(); return source.stream().map(element -> getValueToWrite(element, componentType)).collect(Collectors.toList()); }
private Map<String, Object> convertMap(final Map<Object, Object> source, final TypeInformation<?> type) { Assert.notNull(source, "Given map must not be null!"); Assert.notNull(type, "Given type must not be null!"); return source.entrySet().stream().collect(HashMap::new, (m, e) -> { Object key = e.getKey(); Object value = e.getValue(); if (!conversions.isSimpleType(key.getClass())) { throw new MappingException("Cannot use a complex object as a key value."); } String simpleKey = key.toString(); Object convertedValue = getValueToWrite(value, type.getMapValueType()); m.put(simpleKey, convertedValue); }, HashMap::putAll); }