Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: URI encode user-inputted string #76

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(string $name, ApiCall $apiCall)
*/
public function endPointPath(): string
{
return sprintf('%s/%s', Aliases::RESOURCE_PATH, $this->name);
return sprintf('%s/%s', Aliases::RESOURCE_PATH, encodeURIComponent($this->name));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(ApiCall $apiCall)
*/
public function endPointPath(string $aliasName): string
{
return sprintf('%s/%s', static::RESOURCE_PATH, $aliasName);
return sprintf('%s/%s', static::RESOURCE_PATH, encodeURIComponent($aliasName));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyticsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function delete()

private function endpointPath()
{
return AnalyticsRules::RESOURCE_PATH . '/' . $this->ruleName;
return AnalyticsRules::RESOURCE_PATH . '/' . encodeURIComponent($this->ruleName);
}
}
2 changes: 1 addition & 1 deletion src/AnalyticsRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function retrieve()

private function endpoint_path($operation = null)
{
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
}
}
1 change: 1 addition & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Typesense\Exceptions\ConfigError;
use Typesense\Lib\Configuration;

include('utils/utils.php');
/**
* Class Client
*
Expand Down
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(string $name, ApiCall $apiCall)
*/
public function endPointPath(): string
{
return sprintf('%s/%s', Collections::RESOURCE_PATH, $this->name);
return sprintf('%s/%s', Collections::RESOURCE_PATH, encodeURIComponent($this->name));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function delete(): array
*/
public function endPointPath(): string
{
return sprintf('%s/%s', Conversations::RESOURCE_PATH, $this->id);
return sprintf('%s/%s', Conversations::RESOURCE_PATH, encodeURIComponent($this->id));
}
}
2 changes: 1 addition & 1 deletion src/ConversationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function delete(): array
*/
public function endPointPath(): string
{
return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, $this->id);
return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, encodeURIComponent($this->id));
}
}
4 changes: 2 additions & 2 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ private function endpointPath(): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
Documents::RESOURCE_PATH,
$this->documentId
encodeURIComponent($this->documentId)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function endPointPath(string $action = ''): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
static::RESOURCE_PATH,
$action
);
Expand Down Expand Up @@ -125,7 +125,7 @@ public function createMany(array $documents, array $options = []): array
{
$this->apiCall->getLogger()->warning(
"createMany is deprecated and will be removed in a future version. " .
"Use import instead, which now takes both an array of documents or a JSONL string of documents"
"Use import instead, which now takes both an array of documents or a JSONL string of documents"
);
return $this->import($documents, $options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(string $keyId, ApiCall $apiCall)
*/
private function endpointPath(): string
{
return sprintf('%s/%s', Keys::RESOURCE_PATH, $this->keyId);
return sprintf('%s/%s', Keys::RESOURCE_PATH, encodeURIComponent($this->keyId));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Override.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ private function endPointPath(): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
Overrides::RESOURCE_PATH,
$this->overrideId
encodeURIComponent($this->overrideId)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function endPointPath(string $overrideId = ''): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
static::RESOURCE_PATH,
$overrideId
encodeURIComponent($overrideId)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function endpointPath($presetsName)
return sprintf(
'%s/%s',
static::PRESETS_PATH,
$presetsName
encodeURIComponent($presetsName)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stopwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function endpointPath($stopwordsName)
return sprintf(
'%s/%s',
static::STOPWORDS_PATH,
$stopwordsName
encodeURIComponent($stopwordsName)
);
}
}
4 changes: 2 additions & 2 deletions src/Synonym.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ private function endPointPath(): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
synonyms::RESOURCE_PATH,
$this->synonymId
encodeURIComponent($this->synonymId)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public function endPointPath(string $synonymId = ''): string
return sprintf(
'%s/%s/%s/%s',
Collections::RESOURCE_PATH,
$this->collectionName,
encodeURIComponent($this->collectionName),
static::RESOURCE_PATH,
$synonymId
encodeURIComponent($synonymId)
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// https://stackoverflow.com/a/1734255
function encodeURIComponent($str)
{
$revert = array('%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')');
return strtr(rawurlencode($str), $revert);
}
9 changes: 9 additions & 0 deletions tests/Feature/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public function testCanUpsertAnAlias(): void
$this->assertEquals($this->sampleAliasResponse, $this->upsertResponse);
}

public function testCanUpsertAnAliasUrlEncodedName(): void
{
$aliasedCollection = [
'collection_name' => 'companies_june11'
];
$res = $this->client()->aliases->upsert("abc123?=+/~-_- &| test'", $aliasedCollection);
$this->assertEquals(["name"=>"abc123?=+/~-_- &| test'", ...$aliasedCollection], $res);
}

public function testCanRetrieveAlias(): void
{
$response = $this->client()->aliases['companies']->retrieve();
Expand Down