Skip to content

Commit d78d9cf

Browse files
DerManoMannclaude
andcommitted
fix: resolve Schema ref from parameter type in AugmentRequestBody
When #[OA\RequestBody] is on a method parameter whose type has a #[OA\Schema] annotation, the existing augmentSchemaType + type2ref lookup fails because it searches for a RequestBody annotation on the class. Add a fallback that resolves the parameter type to a Schema ref when ref remains UNDEFINED after the existing logic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 85c56aa commit d78d9cf

9 files changed

Lines changed: 73 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ vendor/
1515
.idea/
1616
.phpstan/
1717
/.phpunit.cache/test-results
18+
/.claude/settings.local.json

src/Processors/AugmentRequestBody.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ protected function augmentRequestBody(Analysis $analysis, array $requestBodies):
5858
$requestBody->ref = $schema->ref;
5959
}
6060

61+
// Fall back: resolve parameter type to a Schema ref
62+
if (Generator::isDefault($requestBody->ref)) {
63+
$type = $context->reflector->getType();
64+
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
65+
$className = $type->getName();
66+
if ($schemaAnnotation = $analysis->getAnnotationForSource($className, OA\Schema::class)) {
67+
$requestBody->ref = OA\Components::ref($schemaAnnotation);
68+
}
69+
}
70+
}
71+
6172
if (Generator::isDefault($requestBody->required)) {
6273
$requestBody->required = !$schema->isNullable();
6374
}

tests/Fixtures/Scratch/RequestBody.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ public function postRef()
8989
public function postRefFoo(#[OAT\RequestBody] RequestBodyRefFoo $body)
9090
{
9191
}
92+
93+
#[OAT\Post(
94+
path: '/endpoint/schema-param',
95+
responses: [
96+
new OAT\Response(
97+
response: 200,
98+
description: 'All good'
99+
),
100+
]
101+
)]
102+
public function postSchemaParam(#[OAT\RequestBody] RequestBodySchema $body)
103+
{
104+
}
92105
}

tests/Fixtures/Scratch/RequestBody3.0.0-legacy.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

tests/Fixtures/Scratch/RequestBody3.0.0-type-info.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

tests/Fixtures/Scratch/RequestBody3.1.0-legacy.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

tests/Fixtures/Scratch/RequestBody3.1.0-type-info.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

tests/Fixtures/Scratch/RequestBody3.2.0-legacy.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

tests/Fixtures/Scratch/RequestBody3.2.0-type-info.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ paths:
4343
responses:
4444
'200':
4545
description: 'All good'
46+
/endpoint/schema-param:
47+
post:
48+
operationId: e9325a4f940b91d3babe398eeebd8426
49+
requestBody:
50+
$ref: '#/components/schemas/RequestBodySchema'
51+
responses:
52+
'200':
53+
description: 'All good'
4654
components:
4755
schemas:
4856
RequestBodySchema: { }

0 commit comments

Comments
 (0)