Skip to content

Commit 3eb5279

Browse files
committed
fix: corrects either id or name to be required
1 parent 6b48bef commit 3eb5279

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/Tools/DTO/FunctionResponse.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ public static function getJsonSchema(): array
113113
'description' => 'The response data from the function.',
114114
],
115115
],
116-
'required' => [self::KEY_ID, self::KEY_NAME, self::KEY_RESPONSE],
116+
'oneOf' => [
117+
[
118+
'required' => [self::KEY_RESPONSE, self::KEY_ID],
119+
],
120+
[
121+
'required' => [self::KEY_RESPONSE, self::KEY_NAME],
122+
],
123+
],
117124
];
118125
}
119126

@@ -140,11 +147,16 @@ public function toArray(): array
140147
*/
141148
public static function fromArray(array $array): self
142149
{
143-
static::validateFromArrayData($array, [self::KEY_ID, self::KEY_NAME, self::KEY_RESPONSE]);
150+
static::validateFromArrayData($array, [self::KEY_RESPONSE]);
151+
152+
// Validate that at least one of id or name is provided
153+
if (!array_key_exists(self::KEY_ID, $array) && !array_key_exists(self::KEY_NAME, $array)) {
154+
throw new \InvalidArgumentException('At least one of id or name must be provided.');
155+
}
144156

145157
return new self(
146-
$array[self::KEY_ID],
147-
$array[self::KEY_NAME],
158+
$array[self::KEY_ID] ?? null,
159+
$array[self::KEY_NAME] ?? null,
148160
$array[self::KEY_RESPONSE]
149161
);
150162
}

tests/unit/Tools/DTO/FunctionCallTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,13 @@ public function testJsonSchema(): void
123123

124124
// Check oneOf for required fields
125125
$this->assertArrayHasKey('oneOf', $schema);
126-
$this->assertCount(3, $schema['oneOf']);
126+
$this->assertCount(2, $schema['oneOf']);
127127

128128
// First option: only id required
129129
$this->assertEquals([FunctionCall::KEY_ID], $schema['oneOf'][0]['required']);
130130

131131
// Second option: only name required
132132
$this->assertEquals([FunctionCall::KEY_NAME], $schema['oneOf'][1]['required']);
133-
134-
// Third option: both id and name required
135-
$this->assertEquals([FunctionCall::KEY_ID, FunctionCall::KEY_NAME], $schema['oneOf'][2]['required']);
136133
}
137134

138135
/**

tests/unit/Tools/DTO/FunctionResponseTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,15 @@ public function testJsonSchema(): void
122122
$this->assertContains('array', $responseTypes);
123123
$this->assertContains('null', $responseTypes);
124124

125-
// Check required fields
126-
$this->assertArrayHasKey('required', $schema);
127-
$this->assertEquals([FunctionResponse::KEY_ID, FunctionResponse::KEY_NAME, FunctionResponse::KEY_RESPONSE], $schema['required']);
125+
// Check oneOf for required fields
126+
$this->assertArrayHasKey('oneOf', $schema);
127+
$this->assertCount(2, $schema['oneOf']);
128+
129+
// First option: response and id required
130+
$this->assertEquals([FunctionResponse::KEY_RESPONSE, FunctionResponse::KEY_ID], $schema['oneOf'][0]['required']);
131+
132+
// Second option: response and name required
133+
$this->assertEquals([FunctionResponse::KEY_RESPONSE, FunctionResponse::KEY_NAME], $schema['oneOf'][1]['required']);
128134
}
129135

130136
/**

0 commit comments

Comments
 (0)