diff --git a/src/ModelDescriber/ObjectModelDescriber.php b/src/ModelDescriber/ObjectModelDescriber.php index 557dd87a4..c3aae7eec 100644 --- a/src/ModelDescriber/ObjectModelDescriber.php +++ b/src/ModelDescriber/ObjectModelDescriber.php @@ -235,6 +235,9 @@ 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)) { @@ -242,14 +245,25 @@ private function markRequiredProperties(OA\Schema $schema): void } foreach ($properties as $property) { + if (is_array($schema->required) && \in_array($property->property, $schema->required, true)) { + $newRequired[] = $property->property; + 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; + } - $schema->required = array_values(array_unique($existingRequiredFields)); + if ($newRequired ?? null) { + $schema->required = array_values(array_unique($newRequired)); } } diff --git a/tests/Functional/Fixtures/MapQueryStringController.json b/tests/Functional/Fixtures/MapQueryStringController.json index cc9d8af2b..0b53a3eb2 100644 --- a/tests/Functional/Fixtures/MapQueryStringController.json +++ b/tests/Functional/Fixtures/MapQueryStringController.json @@ -1103,9 +1103,9 @@ "SymfonyConstraintsWithValidationGroups": { "required": [ "property", - "propertyNotNullOnSpecificGroup", "propertyInDefaultGroup", - "propertyArray" + "propertyArray", + "propertyNotNullOnSpecificGroup" ], "properties": { "property": { diff --git a/tests/Functional/Fixtures/MapRequestPayloadController.json b/tests/Functional/Fixtures/MapRequestPayloadController.json index 6fc775596..4e39dd338 100644 --- a/tests/Functional/Fixtures/MapRequestPayloadController.json +++ b/tests/Functional/Fixtures/MapRequestPayloadController.json @@ -147,9 +147,9 @@ "SymfonyConstraintsWithValidationGroups": { "required": [ "property", - "propertyNotNullOnSpecificGroup", "propertyInDefaultGroup", - "propertyArray" + "propertyArray", + "propertyNotNullOnSpecificGroup" ], "properties": { "property": { diff --git a/tests/Functional/FunctionalTest.php b/tests/Functional/FunctionalTest.php index 09aa63f5c..0d4e89972 100644 --- a/tests/Functional/FunctionalTest.php +++ b/tests/Functional/FunctionalTest.php @@ -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,