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 #857 - fix on put when both @id and id are present #877

Merged
merged 1 commit into from
Jan 25, 2017

Conversation

Simperfit
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #857
License MIT
Doc PR

Add an non-regression test and modify the comportement of itemNormalizer

@@ -28,12 +28,12 @@
public function denormalize($data, $class, $format = null, array $context = [])
{
// Avoid issues with proxies if we populated the object
if (isset($data['id']) && !isset($context['object_to_populate'])) {
if (isset($data['@id']) && !isset($context['object_to_populate'])) {
Copy link
Member

@dunglas dunglas Dec 10, 2016

Choose a reason for hiding this comment

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

This snippet already exists in the JSON-LD normalizer: https://github.com/api-platform/core/blob/master/src/JsonLd/Serializer/ItemNormalizer.php#L93

@id is specific to JSON-LD, so it should not be here. However id can contain an IRI too (when not using JSON-LD). So it should be kept. I guess it's a priority problem, in https://github.com/api-platform/core/blob/master/src/JsonLd/Serializer/ItemNormalizer.php#L101 the call to the parent method should be done before.

@Simperfit
Copy link
Contributor Author

Simperfit commented Dec 10, 2016

@dunglas This is now working but i'm not sure about the fix, since I don't think it's in the same scope then the iriConverter is, clearly this is not an IRI, do you have any thing in mind that would be better ?

EDIT: it's not the right fix ;)

@Simperfit Simperfit changed the title fix #857 - fix on put when both @id and id are present [WIP] fix #857 - fix on put when both @id and id are present Dec 10, 2016
@Simperfit Simperfit changed the title [WIP] fix #857 - fix on put when both @id and id are present fix #857 - fix on put when both @id and id are present Dec 10, 2016
@Simperfit
Copy link
Contributor Author

Again, maybe not the best fixes, but better than before since it's working and it does not break anything.

@@ -32,8 +32,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
throw new InvalidArgumentException('Update is not allowed for this operation.');
}

$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
$context['object_to_populate'] = $this->iriConverter->getItemFromIri(is_int($data['id']) ? $this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data['id'] : $data['id'], $context + ['fetch_data' => false]);
Copy link
Member

Choose a reason for hiding this comment

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

id can be from other types (uuid are strings for instance).

Copy link
Contributor Author

@Simperfit Simperfit Dec 13, 2016

Choose a reason for hiding this comment

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

What could I do about that ?

Edit: Was thinking about try catching and doing the getIriFromResourceClass, WDYT @dunglas ?

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

When test will be green.

Copy link
Contributor

@theofidry theofidry left a comment

Choose a reason for hiding this comment

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

See my comments for some nitpicking changes, 👍 overall :)

* @ApiResource(attributes={
* "normalization_context"={"groups"={"related_output", "output"}},
* "denormalization_context"={"groups"={"related_input", "input"}}
*
Copy link
Contributor

Choose a reason for hiding this comment

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

useless extra line

/**
* @return int
*/
public function getId()
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't a return typehint more suitable here and the other places in the same class?

"name": "My Dummy",
"alias": "My alias"
}
"""

Scenario: Create a resource with relation
Copy link
Contributor

Choose a reason for hiding this comment

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

Please fix the scenario summary (same for all below)...

@teohhanhui
Copy link
Contributor

teohhanhui commented Dec 20, 2016

The fix is simple: We must not support embedded relations with application/json, because the semantics are undefined. We have had this discussion before on Slack, and I thought a decision was made...

So I'm -1 since this is the wrong fix.

/cc @api-platform/core-team

@dunglas
Copy link
Member

dunglas commented Dec 20, 2016

@teohhanhui the issue here isn't with embedded relations but using raw ids instead of IRIs in raw JSON. Not sure we should support that too.

@teohhanhui
Copy link
Contributor

teohhanhui commented Dec 20, 2016

What is the issue with deserializing using a raw id property (or composite id represented as separate properties)? I think it should be supported.

@Simperfit
Copy link
Contributor Author

@teohhanhui updated

@dunglas dunglas added the bug label Dec 23, 2016
try {
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
} catch (InvalidArgumentException $e) {
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data['id'], $context + ['fetch_data' => false]);
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this going to work with composite identifiers?

Copy link
Contributor

Choose a reason for hiding this comment

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

And also this is hardcoded to use the property "id"? 😢

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@teohhanhui @dunglas do we want to support composite identifiers ?

@Simperfit
Copy link
Contributor Author

ping @teohhanhui @dunglas modification done, WDYT ?

$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
} catch (InvalidArgumentException $e) {
foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $context) as $propertyName) {
if (true === $this->propertyMetadataFactory->create($context['resource_class'], $propertyName)->isIdentifier()) {
Copy link
Member

Choose a reason for hiding this comment

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

If we don't support yet composite identifiers, you can break after the first find. Or you can add support for composite identifiers :)

Copy link
Member

Choose a reason for hiding this comment

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

+1 for the composite support :D

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 think for now we should bug fix this and then add that feature ?

$identifier = $propertyName;
}
}
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data[$identifier], $context + ['fetch_data' => false]);
Copy link
Member

Choose a reason for hiding this comment

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

I would use sprintf here for clarity.

@dunglas
Copy link
Member

dunglas commented Jan 25, 2017

@api-platform/core-team is everyone OK to merge this one too? I want to publish a bugfix release today.

@dunglas dunglas merged commit 01303fa into api-platform:2.0 Jan 25, 2017
@dunglas
Copy link
Member

dunglas commented Jan 25, 2017

Thanks @Simperfit!

@Simperfit Simperfit deleted the fix/857-embedded branch January 25, 2017 15:01
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants