Skip to content

Hal jsonschema #4357

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

Merged
merged 13 commits into from
Sep 24, 2021
Merged

Hal jsonschema #4357

merged 13 commits into from
Sep 24, 2021

Conversation

arnedesmedt
Copy link
Contributor

@arnedesmedt arnedesmedt commented Jul 14, 2021

Q A
Branch? main
License MIT
Doc PR api-platform/docs#...

Hi, this is a replacing MR for #4353
My colleague @coudenysj is now on holiday, so I've written the tests for his MR.

I've also needed to extend the SchemaFactoryInterface, because the decoration pattern is using the following order: Hal -> Hydra -> BaseSchemaFactory, we can't use the addDistinctFormat method only on the BaseSchemaFactory.

@@ -63,10 +62,7 @@ final class SchemaFactory implements SchemaFactoryInterface
public function __construct(SchemaFactoryInterface $schemaFactory)
{
$this->schemaFactory = $schemaFactory;

if ($schemaFactory instanceof BaseSchemaFactory) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not keeping the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the check now to the public method addDistinctFormat from the hydra schema factory.

@@ -26,4 +26,6 @@ interface SchemaFactoryInterface
* Builds the JSON Schema document corresponding to the given PHP class.
*/
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema;

public function addDistinctFormat(string $format): void;
Copy link
Member

Choose a reason for hiding this comment

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

Adding a method to an existing interface is a BC break, you cannot do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here is that we decorating the hydra schema factory with the hal schemafactory. So the schemafactory property in the hal schema factory will never be a "BaseSchemaFactory".

We've got HalSchemaFacotry contains the HydraSchemaFactory contains the BaseSchemaFactory. Since you only could add distinct formats in the baseSchemaFactory, it is impossible for the HalSchemaFactory to add the 'hal' as a distinct format.

Do you've got any tips to add the hal distinct format without extending the schemafactory interface?

Copy link
Member

Choose a reason for hiding this comment

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

You can keep the public method in the HydraSchemaFactory but without adding it to the interface too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I did work now with the method_exists function.

$definitions = $resultSchema->getDefinitions();
$rootDefinitionKey = $resultSchema->getRootDefinitionKey();

$this->assertEquals(str_replace('\\', '.', Dummy::class).'.jsonhal', $rootDefinitionKey);
Copy link
Member

Choose a reason for hiding this comment

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

This line duplicates a previous test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the duplicate assert also from the hydra test.

@alanpoulain
Copy link
Member

Hello,
It looks nice! You need to fix PHPStan too.

@arnedesmedt arnedesmedt requested a review from alanpoulain July 16, 2021 14:00
@arnedesmedt
Copy link
Contributor Author

@alanpoulain fixed phpstan errors and ready for review. Thx in advance!

}

return $schema;
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you add a public addDistinctFormat method here too then?
In case this factory would be decorated too in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, will add it.

@arnedesmedt arnedesmedt requested a review from alanpoulain July 19, 2021 06:06
@soyuka
Copy link
Member

soyuka commented Aug 10, 2021

really good feature if you could rebase, thanks!

@arnedesmedt
Copy link
Contributor Author

Did the rebase.

@arnedesmedt
Copy link
Contributor Author

@alanpoulain can this be merged? Or do you expect something else to merge it?

@alanpoulain alanpoulain force-pushed the hal-jsonschema branch 2 times, most recently from 0d62a50 to 7053bfb Compare September 24, 2021 15:58
@alanpoulain alanpoulain merged commit 1056410 into api-platform:main Sep 24, 2021
@alanpoulain
Copy link
Member

Thank you @arnedesmedt and @coudenysj!

@coudenysj
Copy link
Contributor

🥳

vincentchalamon pushed a commit to vincentchalamon/core that referenced this pull request Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants