Skip to content

Commit

Permalink
ensure required has the same order as properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Oct 17, 2024
1 parent 99ebeeb commit b34f5fa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,35 @@ private function describeProperty(array $types, Model $model, OA\Schema $propert
throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@OA\Property(type="")` annotation to specify it manually.', $types[0]->getBuiltinType(), $model->getType()->getClassName(), $propertyName));
}

/**
* Mark properties as required while ordering them in the same order as the properties of the schema.
*/
private function markRequiredProperties(OA\Schema $schema): void
{
if (Generator::isDefault($properties = $schema->properties)) {
return;
}

foreach ($properties as $property) {
if (is_array($schema->required) && \in_array($property->property, $schema->required, true)) {
$newRequired[] = $property->property;

Check failure on line 249 in src/ModelDescriber/ObjectModelDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Implicit array creation is not allowed - variable $newRequired might not exist.
continue;
}

if (true === $property->nullable || !Generator::isDefault($property->default)) {
continue;
}

$existingRequiredFields = Generator::UNDEFINED !== $schema->required ? $schema->required : [];
$existingRequiredFields[] = $property->property;
// Initialize the required fields array if it's not already set
if (Generator::UNDEFINED === $schema->required) {
$schema->required = [];
}

$newRequired[] = $property->property;

Check failure on line 262 in src/ModelDescriber/ObjectModelDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Implicit array creation is not allowed - variable $newRequired might not exist.
}

$schema->required = array_values(array_unique($existingRequiredFields));
if ($newRequired ?? null) {

Check failure on line 265 in src/ModelDescriber/ObjectModelDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Only booleans are allowed in an if condition, array<int, string>|null given.
$schema->required = array_values(array_unique($newRequired));
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Fixtures/MapQueryStringController.json
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,9 @@
"SymfonyConstraintsWithValidationGroups": {
"required": [
"property",
"propertyNotNullOnSpecificGroup",
"propertyInDefaultGroup",
"propertyArray"
"propertyArray",
"propertyNotNullOnSpecificGroup"
],
"properties": {
"property": {
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Fixtures/MapRequestPayloadController.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@
"SymfonyConstraintsWithValidationGroups": {
"required": [
"property",
"propertyNotNullOnSpecificGroup",
"propertyInDefaultGroup",
"propertyArray"
"propertyArray",
"propertyNotNullOnSpecificGroup"
],
"properties": {
"property": {
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ public function testSymfonyConstraintDocumentation(): void
];

if (Helper::isCompoundValidatorConstraintSupported()) {
array_splice($expected['required'], 2, 0, 'propertyWithCompoundValidationRule');
$expected['required'][] = 'propertyWithCompoundValidationRule';
$expected['properties']['propertyWithCompoundValidationRule'] = [
'type' => 'integer',
'maximum' => 5,
Expand Down

0 comments on commit b34f5fa

Please sign in to comment.