Skip to content

Hal jsonschema#4357

Merged
alanpoulain merged 13 commits into
api-platform:mainfrom
arnedesmedt:hal-jsonschema
Sep 24, 2021
Merged

Hal jsonschema#4357
alanpoulain merged 13 commits into
api-platform:mainfrom
arnedesmedt:hal-jsonschema

Conversation

@arnedesmedt

@arnedesmedt arnedesmedt commented Jul 14, 2021

Copy link
Copy Markdown
Contributor
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.

{
$this->schemaFactory = $schemaFactory;

if ($schemaFactory instanceof BaseSchemaFactory) {

Copy link
Copy Markdown
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
Copy Markdown
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.

*/
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

}

return $schema;
}

Copy link
Copy Markdown
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
Copy Markdown
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

soyuka commented Aug 10, 2021

Copy link
Copy Markdown
Member

really good feature if you could rebase, thanks!

@arnedesmedt

Copy link
Copy Markdown
Contributor Author

Did the rebase.

@arnedesmedt

Copy link
Copy Markdown
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
Copy Markdown
Member

Thank you @arnedesmedt and @coudenysj!

@coudenysj

Copy link
Copy Markdown
Contributor

🥳

vincentchalamon pushed a commit to vincentchalamon/core that referenced this pull request Oct 15, 2021
…m#4357)

Co-authored-by: Jachim Coudenys <jachim.coudenys@combellgroup.com>
Co-authored-by: Alan Poulain <contact@alanpoulain.eu>
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