Allow plain IDs with allow_plain_identifiers#1365
Conversation
soyuka
commented
Sep 12, 2017
| Q | A |
|---|---|
| Bug fix? | yes/no (depends on the POV) |
| New feature? | yes/no (same) |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | api-platform/api-platform#249 #1363 |
| License | MIT |
| Doc PR | n/a |
e7218e0 to
6f862a7
Compare
| } | ||
|
|
||
| if (!is_array($value)) { | ||
| // repeat the code so that IRIs keep working with the json format |
There was a problem hiding this comment.
I'd like to test that Content-Type is application/json here, not sure if it's possible or wanted...
There was a problem hiding this comment.
you can just check the $format var.
There was a problem hiding this comment.
not really, json is the format for hal, jsonld and json no?
There was a problem hiding this comment.
No, it's jsonld for JSON-LD and jsonhal for HAL.
There was a problem hiding this comment.
Okay, my bad dunno why I thought so...
3f4a223 to
0cbf4dd
Compare
0cbf4dd to
5e8619f
Compare
|
|
||
| if (!is_array($value)) { | ||
| // repeat the code so that IRIs keep working with the json format | ||
| if ('json' === $format && $this->itemDataProvider) { |
There was a problem hiding this comment.
I get the idea and the use case, but it hurts me a bit:
- it doesn't fit the hypermedia philosophie of API Platform
- it doesn't work consistently across formats (XML or CSV for instance may need this feature as well)
May I suggest an alternative implementation:
- add a new flag
allow_plain_identifiersdefaulting tofalse - if this flag is on, execute your code in all circonstances, regardless of the format
There was a problem hiding this comment.
And then it will be merged in master.
There was a problem hiding this comment.
You see :p had the same concerns ^^
43017af to
6a11dd7
Compare
allow_plain_identifiers
|
I don't think we need a config option. |
|
To you we should:
@dunglas wdyt? I'm also to remove |
|
There is no question that |
|
Agreed @teohhanhui, though IRIs are such great at identifying things that it'd be a shame not to be able to use them for other things (might be a point to keep them with other formats). |
|
For now I can go back to my previous commit to do: |
|
I don't agree:
|
|
In that case @dunglas feel free to merge! I see no issues in keeping |
| protected $allowPlainIdentifiers; | ||
|
|
||
| public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null) | ||
| public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, ItemDataProviderInterface $itemDataProvider = null, $allowPlainIdentifiers = false) |
There was a problem hiding this comment.
bool $allowPlainIdentifiers = false
6a11dd7 to
965ff42
Compare
|
Should we use IRIs as identifiers with JSON API too? |
965ff42 to
b28d524
Compare
b28d524 to
44b6fab
Compare
|
rebased |
|
I really don't think this is a great choice to do so. What's the point? It's just adding another way to do something already possible... If you look at #1363 it's just because "I don't want to use this code" or in api-platform/api-platform#249 the reason is "I think plain IDs will be used for many years to come". We are convinced that semantic web is the way to go. The way to properly go towards to this direction is to have people using IRIs as their identifiers... let's educate for this instead of allow "plain IDs" :( |
|
@sroze totally agree when it comes to formats like |
|
@sroze This is needed for example in our project, where we are currently using JSON (not LD-JSON), so having IRIs is not required and forcing IRIs on our developers is unneccessary and adds to the work. Also our routing is not fixed as the project is still evolving, so it's easier to send IDs, instead of constantly following the changes of routing on frontend and backend too. |
|
|
||
| if (!is_array($value)) { | ||
| // repeat the code so that IRIs keep working with the json format | ||
| if (true === $this->allowPlainIdentifiers && $this->itemDataProvider) { |
There was a problem hiding this comment.
What is $this->itemDataProvider === null and true === $this->allowPlainIdentifiers ? Instead of silently not doing anything, should we throw an exception instead?
There was a problem hiding this comment.
We could, maybe a bit overkill, this dependency should be a hard dep, it's just nullable to avoid breaking.
That's an interesting argument because that's actually where using IRIs should help you :) But fair point for the JSON format. If it helps... 👍 then... |
44b6fab to
ac4622c
Compare
ac4622c to
808b5d3
Compare
|
Hello guys. Is it possible to merge this in 2.1 branch? I think this is a really important feature and I hope to see it in stable version ASAP. Thanks! |
|
Sorry we respect semver and new features are always merged in minor releases. Patch releases only contain bug and security fixes. |
|
According to this PR, I should be able to update (PUT) nested document with plain id ? {
"id":1,
"name": "foo",
"groups": [
{
"id":1,
"name": "bar"
}
]
}Composer.json : (composer update done) config.sjon Forget something ? Yet not ready maybe ? |
|
@julienpillias you have a serialization issue fix your groups and this will indeed work. |
|
OK, had to put denormalization_context into the nested document too. TY for the tips This route exists, but should be http://api.xxxxx.project.local/app_dev.php/groups/1, because of my routing I think : api:
resource: '.'
type: 'api_platform'
host: 'api.{project_name}'
defaults:
project_name: xxxx.project.local
requirements:
project_name: xxxx.project.local|xxxx.fr|preprod.xxxx.frI don't see any option in apiPlatform to specify a route parameter. At least, is it possible to use the default parameter ? |
|
So just dig into the code, and found this in https://github.com/api-platform/core/blob/master/src/Bridge/Symfony/Routing/IriConverter.php (Line 141) : Change it to (ABS_PATH => ABS_URL). EDIT : maybe still having the problem.... digging in |
this should be configurable I think but it is not actually :|. |
|
could try a PR for this. Something in config.json ? api_platform:
use_absolute_url: true # default will be false |
|
Not sure, I think that this is a configuration issue on your side but I have no idea what (something linked to symfony routing configuration) :|. |
|
On my project, I have a "classic" web site (SF + twig), using mysite.com (or www.mysite.com) AND have the API on api.mysite.com for mobile application for example. So if apiPlatform check route /entity/id, it would not be an API ressource. It's why I need ABS_URL their, so ressource will be found with http://api.mysite.com/entity/id Don't think I have a routing problem here, i've checked router:debug, and do not have any problem with api doc, or my "classic" web site". |
|
Can you open an issue about this with all the details? Thanks. |
|
Already done few week's ago :) |
Allow plain IDs with `allow_plain_identifiers`