private function getDocumentReference(
        ConnectionInterface $connection,
        ValueMapper $mapper,
        $projectId,
        $database,
        $name
    ) {
        if ($this->isRelative($name)) {
            try {
                $name = $this->fullName($projectId, $database, $name);
            } catch (ValidationException $e) {
                // The GAPIC parser does not support special characters in paths,
                // but Firestore does. If an exception is raised by the parser,
                // we'll check for special characters. If they exist, we'll
                // manually construct a document path.
                $hasSpecialChars = preg_match('/[!@#$%^&*(),.?":{}|<>]/', $name) === 1;

                //@codeCoverageIgnoreStart
                if (!$hasSpecialChars) {
                    throw $e;
                }
                //@codeCoverageIgnoreEnd

                $base = $this->databaseName($projectId, $database);
                $name = $base .'/documents/'. $name;
            }
        }

        if (!$this->isDocument($name)) {
            throw new \InvalidArgumentException('Given path is not a valid document path.');
        }

        return new DocumentReference(
            $connection,
            $mapper,
            $this->getCollectionReference(
                $connection,
                $mapper,
                $projectId,
                $database,
                $this->parentPath($name)
            ),
            $name
        );
    }