-
-
Notifications
You must be signed in to change notification settings - Fork 912
Support union/intersect types #5470
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
Changes from all commits
98d4b98
d543e88
ae8cd18
2005493
df9d7fa
2c5fc99
0c50d19
3118da4
66c9ba9
05d1f43
31d8462
e8d09a6
0f3fb19
c00562f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
Feature: Union/Intersect types | ||
|
||
Scenario Outline: Create a resource with union type | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I add "Accept" header equal to "application/ld+json" | ||
And I send a "POST" request to "/issue-5452/books" with body: | ||
""" | ||
{ | ||
"number": <number>, | ||
"isbn": "978-3-16-148410-0" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
And the JSON should be valid according to this schema: | ||
""" | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"@type": { | ||
"type": "string", | ||
"pattern": "^Book$" | ||
}, | ||
"@context": { | ||
"type": "string", | ||
"pattern": "^/contexts/Book$" | ||
}, | ||
"@id": { | ||
"type": "string", | ||
"pattern": "^/.well-known/genid/.+$" | ||
}, | ||
"number": { | ||
"type": "<type>" | ||
}, | ||
"isbn": { | ||
"type": "string", | ||
"pattern": "^978-3-16-148410-0$" | ||
} | ||
}, | ||
"required": [ | ||
"@type", | ||
"@context", | ||
"@id", | ||
"number", | ||
"isbn" | ||
] | ||
} | ||
""" | ||
Examples: | ||
| number | type | | ||
| "1" | string | | ||
| 1 | integer | | ||
|
||
Scenario: Create a resource with valid intersect type | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/issue-5452/books" with body: | ||
""" | ||
{ | ||
"number": 1, | ||
"isbn": "978-3-16-148410-0", | ||
"author": "/issue-5452/authors/1" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
And the JSON should be valid according to this schema: | ||
""" | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"@type": { | ||
"type": "string", | ||
"pattern": "^Book$" | ||
}, | ||
"@context": { | ||
"type": "string", | ||
"pattern": "^/contexts/Book$" | ||
}, | ||
"@id": { | ||
"type": "string", | ||
"pattern": "^/.well-known/genid/.+$" | ||
}, | ||
"number": { | ||
"type": "integer" | ||
}, | ||
"isbn": { | ||
"type": "string", | ||
"pattern": "^978-3-16-148410-0$" | ||
}, | ||
"author": { | ||
"type": "string", | ||
"pattern": "^/issue-5452/authors/1$" | ||
} | ||
}, | ||
"required": [ | ||
"@type", | ||
"@context", | ||
"@id", | ||
"number", | ||
"isbn", | ||
"author" | ||
] | ||
} | ||
""" | ||
|
||
Scenario: Create a resource with invalid intersect type | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/issue-5452/books" with body: | ||
""" | ||
{ | ||
"number": 1, | ||
"isbn": "978-3-16-148410-0", | ||
"library": "/issue-5452/libraries/1" | ||
} | ||
""" | ||
Then the response status code should be 400 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
And the JSON node "hydra:description" should be equal to 'Could not denormalize object of type "ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5452\ActivableInterface", no supporting normalizer found.' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,17 +213,23 @@ public function getResourceObjectTypeFields(?string $resourceClass, Operation $o | |
'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null, | ||
]; | ||
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property, $context); | ||
$propertyTypes = $propertyMetadata->getBuiltinTypes(); | ||
|
||
if ( | ||
null === ($propertyType = $propertyMetadata->getBuiltinTypes()[0] ?? null) | ||
!$propertyTypes | ||
|| (!$input && false === $propertyMetadata->isReadable()) | ||
|| ($input && $operation instanceof Mutation && false === $propertyMetadata->isWritable()) | ||
) { | ||
continue; | ||
} | ||
|
||
if ($fieldConfiguration = $this->getResourceFieldConfiguration($property, $propertyMetadata->getDescription(), $propertyMetadata->getDeprecationReason(), $propertyType, $resourceClass, $input, $operation, $depth, null !== $propertyMetadata->getSecurity())) { | ||
$fields['id' === $property ? '_id' : $this->normalizePropertyName($property, $resourceClass)] = $fieldConfiguration; | ||
// guess union/intersect types: check each type until finding a valid one | ||
foreach ($propertyTypes as $propertyType) { | ||
if ($fieldConfiguration = $this->getResourceFieldConfiguration($property, $propertyMetadata->getDescription(), $propertyMetadata->getDeprecationReason(), $propertyType, $resourceClass, $input, $operation, $depth, null !== $propertyMetadata->getSecurity())) { | ||
$fields['id' === $property ? '_id' : $this->normalizePropertyName($property, $resourceClass)] = $fieldConfiguration; | ||
// stop at the first valid type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alanpoulain doesn't graphql support multiple types ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently, it seems possible, but it requires a special trick: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's not an input type, yes. Otherwise no (see graphql/graphql-spec#825). |
||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling that having a property that is a scalar and a resource class is such a bad practice that we should just not support it.