core icon indicating copy to clipboard operation
core copied to clipboard

[JsonSchema] Only build schemas which can be serialized / deserialied

Open GeLoLabs opened this issue 1 year ago • 5 comments

Q A
Branch? main
Tickets None
License MIT
Doc PR None

Hello,

This PR is a first step for a discussion about a bugfix.

Given the following ApiResource:

#[ApiResource(operations: [new Delete()])]
class MyResource
{
}

If I generate the OpenAPI documentation using php bin/console api:openapi:export, the ApiPlatform\OpenApi\Factory\OpenApiFactory will generate a doc including the components "MyResource" whereas no operation references it (request / response -> no content).

Futhermore, it exposes all MyResource properties which should stay internal as it is not part of our API.

I would expect that the OpenApiFactory only builds schemas that operation can deserialize / serialize and this is what this PR does.

Now, to fix my OpenAPI doc, I can just do the following:

#[ApiResource(operations: [new Delete(
    deserialize: false,
    serialize: false,
)])]
class MyResource
{
}

WDYT ?

GeLoLabs avatar Apr 01 '24 20:04 GeLoLabs

use openapi: false?

soyuka avatar Apr 02 '24 06:04 soyuka

@soyuka Using the flag openapi: false would remove the operation from the OpenAPI spec.

What I want is to keep the operation in the OpenAPI spec but skip the associated schema as my DELETE operation does not have request body as well as no response body (204).

Currenlty, it includes the schema of the class tagged as ApiResource in the OpenAPI spec.

GeLoLabs avatar Apr 02 '24 19:04 GeLoLabs

Futhermore, it exposes all MyResource properties which should stay internal as it is not part of our API.

This is quite wrong if a class is marked as a resource it is a public API. I like your patch though, is it possible to add a test on that though?

soyuka avatar Apr 03 '24 08:04 soyuka

@soyuka I mean it exposes unwanted properties as I use deserialization / serialization groups everywhere on this ApiResource for all its operations except for the delete operation. Then, it ends by adding the "root" resource (not specific to a (de)serializer group) with all is properties, something I don't want.

I will complete this PR with some tests.

GeLoLabs avatar Apr 04 '24 17:04 GeLoLabs

@soyuka @GwendolenLynch I'm not familiar with the project test suite. Can you point me where / how I can add tests about this feature ?

GeLoLabs avatar Jun 01 '24 16:06 GeLoLabs