[Capability] Apply a parameter's complete schema definition (#435)
* [Capability] Apply a parameter's complete schema definition Schema(definition:) is documented as taking precedence over the individual keywords, and generate() unwraps it that way at method level. At parameter level it was merged as an ordinary key instead, in both the plain and the variadic path, so the property reached clients carrying a literal "definition" member - not JSON Schema, read by nothing, and everything inside it lost. Both paths now replace the schema with the definition. The variadic one still forces type: array afterwards, since a variadic arrives as an array however the definition describes it, and the signature default survives a definition that states none - it is what the handler receives when the argument is omitted, and default is an annotation rather than a constraint. * Fix docblock description overwriting definition on variadic params * Apply suggestion from @chr-hertel
This commit is contained in:
parent
08d332c7ac
commit
cfe5e8f570
1 changed files with 120 additions and 6 deletions
|
|
@ -237,7 +237,17 @@ final class SchemaGenerator implements SchemaGeneratorInterface
|
|||
// Parameter-level takes highest precedence
|
||||
$parameterLevelSchema = $paramInfo['parameter_schema'];
|
||||
if (!empty($parameterLevelSchema)) {
|
||||
$mergedSchema = array_merge($mergedSchema, $parameterLevelSchema);
|
||||
// A complete definition replaces the schema, as it does at method level.
|
||||
if (isset($parameterLevelSchema['definition']) && \is_array($parameterLevelSchema['definition'])) {
|
||||
$mergedSchema = $parameterLevelSchema['definition'];
|
||||
|
||||
// The default comes from the signature, not from the schema.
|
||||
if (!\array_key_exists('default', $mergedSchema) && \array_key_exists('default', $inferredSchema)) {
|
||||
$mergedSchema['default'] = $inferredSchema['default'];
|
||||
}
|
||||
} else {
|
||||
$mergedSchema = array_merge($mergedSchema, $parameterLevelSchema);
|
||||
}
|
||||
}
|
||||
|
||||
// Run after all merges so that when a Schema attribute reshapes the parameter
|
||||
|
|
@ -324,17 +334,24 @@ final class SchemaGenerator implements SchemaGeneratorInterface
|
|||
{
|
||||
$paramSchema = ['type' => 'array'];
|
||||
|
||||
// Apply parameter-level Schema attributes first
|
||||
if (!empty($paramInfo['parameter_schema'])) {
|
||||
$paramSchema = array_merge($paramSchema, $paramInfo['parameter_schema']);
|
||||
// Ensure type is always array for variadic
|
||||
$paramSchema['type'] = 'array';
|
||||
}
|
||||
|
||||
// Add description from docblock before parameter-level schema is merged in,
|
||||
// so a definition's own description takes precedence (matches buildParameterSchema).
|
||||
if ($paramInfo['description']) {
|
||||
$paramSchema['description'] = $paramInfo['description'];
|
||||
}
|
||||
|
||||
// Apply parameter-level Schema attributes first
|
||||
if (!empty($paramInfo['parameter_schema'])) {
|
||||
$parameterLevelSchema = $paramInfo['parameter_schema'];
|
||||
|
||||
$paramSchema = isset($parameterLevelSchema['definition']) && \is_array($parameterLevelSchema['definition'])
|
||||
? $parameterLevelSchema['definition']
|
||||
: array_merge($paramSchema, $parameterLevelSchema);
|
||||
|
||||
// Ensure type is always array for variadic
|
||||
$paramSchema['type'] = 'array';
|
||||
}
|
||||
|
||||
// If no items specified by Schema attribute, infer from type
|
||||
if (!isset($paramSchema['items'])) {
|
||||
$itemJsonTypes = $this->mapPhpTypeToJsonSchemaType($paramInfo['type_string']);
|
||||
|
|
|
|||
Loading…
Reference in a new issue