public static function resolveObjectType(Endpoint $endpoint, $object): ?string
    {
        if ($object instanceof PolymorphicObjectInterface && $object->getConcreteType()) {
            return $object->getConcreteType();
        }

        $class = DoctrineClassUtils::getClass($object);
        if (!$endpoint->hasTypeForClass($class)) {
            return null;
        }

        $types = $endpoint->getTypesForClass($class);

        //if only one type for given object class return the type
        if (count($types) === 1) {
            return $types[0];
        }

        //in case of multiple types using polymorphic definitions
        foreach ($types as $type) {
            $definition = $endpoint->getType($type);
            if ($definition instanceof PolymorphicDefinitionInterface) {
                return self::resolveConcreteType($endpoint, $definition, $object);
            }
        }

        //as fallback use the first type in the list
        return $types[0];
    }