Skip to content

Commit d3a0fe6

Browse files
refactor(Schema): Remove optimistic string format inference from SchemaGenerator
1 parent ca6d25a commit d3a0fe6

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

src/Support/SchemaGenerator.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,10 @@ public function fromMethodParameters(ReflectionMethod $method): array
131131
}
132132
}
133133

134-
// Add format for specific string types (basic inference)
135-
if (isset($paramSchema['type'])) {
136-
$schemaType = is_array($paramSchema['type']) ? (in_array('string', $paramSchema['type']) ? 'string' : null) : $paramSchema['type'];
137-
if ($schemaType === 'string') {
138-
if (stripos($name, 'email') !== false || stripos($typeString, 'email') !== false) {
139-
$paramSchema['format'] = 'email';
140-
} elseif (stripos($name, 'date') !== false || stripos($typeString, 'date') !== false) {
141-
$paramSchema['format'] = 'date-time'; // Or 'date' depending on convention
142-
} elseif (stripos($name, 'uri') !== false || stripos($name, 'url') !== false || stripos($typeString, 'uri') !== false || stripos($typeString, 'url') !== false) {
143-
$paramSchema['format'] = 'uri';
144-
}
145-
// Add more format detections if needed
146-
}
147-
}
134+
// TODO: Revisit format inference or add explicit @schema docblock tag for formats in a future version.
135+
// For now, parameters typed as 'string' will not have a 'format' keyword automatically added.
136+
// Users needing specific string format validation (date-time, email, uri, regex pattern)
137+
// would need to perform that validation within their tool/resource handler method.
148138

149139
// Handle array items type if possible
150140
if (isset($paramSchema['type'])) {
@@ -176,7 +166,7 @@ public function fromMethodParameters(ReflectionMethod $method): array
176166

177167
if (empty($schema['properties'])) {
178168
// Keep properties object even if empty, per spec
179-
$schema['properties'] = new stdClass();
169+
$schema['properties'] = new stdClass;
180170
}
181171
if (empty($schema['required'])) {
182172
unset($schema['required']);

tests/Unit/Support/SchemaGeneratorTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function setupDocBlockExpectations(Mockery\MockInterface $parserMock, Reflection
5656

5757
$schema = $this->schemaGenerator->fromMethodParameters($method);
5858

59-
expect($schema)->toEqual(['type' => 'object', 'properties' => new \stdClass()]);
59+
expect($schema)->toEqual(['type' => 'object', 'properties' => new \stdClass]);
6060
expect($schema)->not->toHaveKey('required');
6161
});
6262

@@ -216,15 +216,3 @@ function setupDocBlockExpectations(Mockery\MockInterface $parserMock, Reflection
216216
expect($schema['properties']['p1'])->toEqual(['type' => 'string', 'description' => 'Docblock overrides int']);
217217
expect($schema['required'])->toEqualCanonicalizing(['p1']);
218218
});
219-
220-
test('generates schema infers format from parameter name', function () {
221-
$method = new ReflectionMethod(SchemaGeneratorTestStub::class, 'formatParams');
222-
setupDocBlockExpectations($this->docBlockParserMock, $method);
223-
224-
$schema = $this->schemaGenerator->fromMethodParameters($method);
225-
226-
expect($schema['properties']['email'])->toEqual(['type' => 'string', 'description' => 'Email address', 'format' => 'email']);
227-
expect($schema['properties']['url'])->toEqual(['type' => 'string', 'description' => 'URL string', 'format' => 'uri']);
228-
expect($schema['properties']['dateTime'])->toEqual(['type' => 'string', 'description' => 'ISO Date time string', 'format' => 'date-time']);
229-
expect($schema['required'])->toEqualCanonicalizing(['email', 'url', 'dateTime']);
230-
});

0 commit comments

Comments
 (0)