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

EZP-32261: Added field name override #98

Merged
merged 6 commits into from
Mar 25, 2021

Conversation

mateuszbieniek
Copy link
Contributor

This PR adds an option to define field name overrides to avoid issues like:
https://issues.ibexa.co/browse/EZP-32261

src/Schema/Domain/Content/NameHelper.php Outdated Show resolved Hide resolved
Comment on lines +2 to +3
ezplatform_graphql.schema.content.field_name.override:
id: id_
Copy link
Member

Choose a reason for hiding this comment

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

Is id the only one field which might generate name collision?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the only field that I'm aware of.

Copy link
Member

Choose a reason for hiding this comment

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

OK, but please rememeber ot document this behaviour. // cc @ezsystems/documentation-team

Copy link
Member

Choose a reason for hiding this comment

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

There are actually more, but they are all prefixed with _, making collisions less likely:

_info:
description: "Item's content info"
type: Content
_content:
description: "Underlying content item"
type: Content
deprecationReason: "Renamed to _info"
_type:
description: "Item's content type"
type: ContentType
_location:
description: "The content's main location"
type: Location
_allLocations:
description: "All the content's locations"
type: "[Location]"
_name:
description: "The content item's name, in the prioritized language(s), based on the object name pattern"
type: String
_url:
description: "The content item's url alias, based on the main location."
type: String
_thumbnail:
type: Thumbnail
.


// Workaround for https://issues.ibexa.co/browse/EZP-32261
if (\array_key_exists($fieldName, $this->fieldNameOverrides)) {
return $this->fieldNameOverrides[$fieldName];
Copy link
Member

@adamwojs adamwojs Mar 22, 2021

Choose a reason for hiding this comment

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

IMHO is worth to warn user at this point that name collision happened (e.g. add warning to logs).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a warning level log entry so it will be shown in the output of

php bin/console ezplatform:graphql:generate-schema

@adamwojs adamwojs changed the title Added field name override EZP-32261: Added field name override Mar 22, 2021
@adamwojs adamwojs added the Doc needed The changes require some documentation label Mar 23, 2021
Copy link
Contributor

@Steveb-p Steveb-p left a comment

Choose a reason for hiding this comment

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

Ideally we would rather prevent declaring fields as "id", but I guess that's the next best thing.

I would suggest making it forbidden to do so in 4.x, treating "id" (along with uppercase variations) like a PHP keyword - just not allow it. WDYT?

@mateuszbieniek
Copy link
Contributor Author

Ideally we would rather prevent declaring fields as "id", but I guess that's the next best thing.

I would suggest making it forbidden to do so in 4.x, treating "id" (along with uppercase variations) like a PHP keyword - just not allow it. WDYT?

I agree, but we still have to keep in mind, that we have to prepare some migration scenario for the customers upgrading from (for example) 2.5 LTS, where id was totally valid identifier for the fields.

And of course some entries in the documentation.

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use LoggerAwareTrait and LoggerAwareInterface instead.

Comment on lines 32 to 35
public function __construct(LoggerInterface $logger, array $fieldNameOverrides)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->logger = $logger;
Copy link
Contributor

Choose a reason for hiding this comment

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

Allow logger to be null and initialize it to NullLogger if done so. This will make the eventual testing easier.

Suggested change
public function __construct(LoggerInterface $logger, array $fieldNameOverrides)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->logger = $logger;
public function __construct(array $fieldNameOverrides, ?LoggerInterface $logger = null)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->logger = $logger ?? new NullLogger();

Don't forget to import the class if this suggestion is acceptable for you.

@@ -109,7 +138,7 @@ private function pluralize($name)
}

if (substr($name, -1) === 'y') {
if (in_array(substr($name, -2, 1), ['a', 'e', 'i', 'o', 'u'])) {
if (\in_array(substr($name, -2, 1), ['a', 'e', 'i', 'o', 'u'])) {
Copy link
Member

Choose a reason for hiding this comment

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

CS: you should rather import it with use function in_array;

@@ -124,7 +153,7 @@ private function pluralize($name)
return substr($name, 0, -2) . 'i';
}

if (in_array(substr($name, -2), ['on', 'um'])) {
if (\in_array(substr($name, -2), ['on', 'um'])) {
Copy link
Member

Choose a reason for hiding this comment

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

CS: the same remark as above

Copy link
Contributor Author

@mateuszbieniek mateuszbieniek Mar 23, 2021

Choose a reason for hiding this comment

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

Running php-cs-fixer fix drops use function in_array; and adds \in_array 🤷‍♂️ - I guess if I push it, CS test will fail as well.

Copy link

@tomaszszopinski tomaszszopinski left a comment

Choose a reason for hiding this comment

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

QA approved on Ibexa DXP 3.2.5-dev & v3.2.5 with patch & diff.

@lserwatka lserwatka merged commit 05d017d into ezsystems:2.2 Mar 25, 2021
@lserwatka
Copy link
Member

Could you merge it up?

@mateuszbieniek
Copy link
Contributor Author

Merged to 3.0 in 7d75c68

@mateuszbieniek
Copy link
Contributor Author

Merged to master in 86f5870

@mateuszbieniek mateuszbieniek deleted the ezp-32261_fix branch March 25, 2021 14:14
@DominikaK DominikaK removed the Doc needed The changes require some documentation label Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
9 participants