Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsonschema): make all required properties optional in PATCH operation with 'json' format #6478

Open
wants to merge 1 commit into
base: 3.4
Choose a base branch
from

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Jul 19, 2024

by @ttskch see #6394

@@ -88,6 +88,19 @@ public function buildSchema(string $className, string $format = 'json', string $
return $schema;
}

$isJsonMergePatch = 'json' === $format && 'PATCH' === $operation->getMethod() && Schema::TYPE_INPUT === $type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$isJsonMergePatch = 'json' === $format && 'PATCH' === $operation->getMethod() && Schema::TYPE_INPUT === $type;
$isJsonMergePatch = 'json' === $format && 'PATCH' === $method && Schema::TYPE_INPUT === $type;

}

if (true === $skipRequiredProperties) {
$definitionName .= self::PATCH_SCHEMA_POSTFIX;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined constant

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think if we add this postfix here on Patch operations, it should also be added on other operations (Post, Put, Delete, Get, GetCollection, etc.)

Comment on lines +95 to +96
if (null === ($skipRequiredProperties = $operation->getExtraProperties()['patch_skip_schema_required_properties'] ?? null)) {
trigger_deprecation('api-platform/core', '3.4', "Set 'patch_skip_schema_required_properties' on extra properties as API Platform 4 will remove required properties from the JSON Schema on Patch request.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (null === ($skipRequiredProperties = $operation->getExtraProperties()['patch_skip_schema_required_properties'] ?? null)) {
trigger_deprecation('api-platform/core', '3.4', "Set 'patch_skip_schema_required_properties' on extra properties as API Platform 4 will remove required properties from the JSON Schema on Patch request.");
if (null === ($skipRequiredProperties = $operation->getExtraProperties()['skip_patch_json_schema_required_properties'] ?? null)) {
trigger_deprecation('api-platform/core', '3.4', "Set 'skip_patch_json_schema_required_properties' on extra properties as API Platform 4 will remove "required" JSON Schema node on Patch requests.");

Comment on lines +338 to +349
/**
* @group legacy
* TODO: find a way to keep required properties if needed
*/
public function testPatchSchemaRequiredProperties(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PatchRequired\PatchMe', '--format' => 'json']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);

$this->assertEquals(['b'], $json['definitions']['PatchMe']['required']);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test when a user wants to force required on a property on a Patch operation with "skip_patch_json_schema_required_properties" => true.

@soyuka
Copy link
Member Author

soyuka commented Sep 3, 2024

See #6485 (comment) for this PR's resolution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants