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
Add feature to update swagger context for properties #1433
Add feature to update swagger context for properties #1433
Conversation
mab05k
commented
Oct 13, 2017
•
edited by dunglas
edited by dunglas
Q | A |
---|---|
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | |
License | MIT |
Doc PR | api-platform/docs#315 |
@dunglas Pull request for documentation is at api-platform/docs#315 |
e5675aa
to
c606cd9
Compare
* | ||
* @return array | ||
*/ | ||
private function getPropertyAttributes(array $attributes): array |
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.
Is this new method necessary?
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.
My thought here was to have that method there to add additional functionality in the future. In case you wanted to do more with the "attributes" on properties.
@@ -532,7 +537,22 @@ private function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayObj | |||
|
|||
$valueSchema = $this->getType($builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $definitions, $serializerContext); | |||
|
|||
return new \ArrayObject((array) $propertySchema + $valueSchema); | |||
return new \ArrayObject((array) $propertySchema + $valueSchema + $attributeSchema); |
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.
This should be enough to get rid of all other lines:
$attributes = $propertyMetadata->getAttributes();
return new \ArrayObject((array) $propertySchema + $valueSchema + $attributes['swagger_context'] ?? []);
c606cd9
to
e8df5cd
Compare
@dunglas Updated per your comments. |
|
||
return new \ArrayObject((array) $propertySchema + $valueSchema); | ||
return new \ArrayObject((array) $propertySchema + $valueSchema + ($attributeSchema['swagger_context'] ?? [])); |
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.
$attributeSchema
is useless. ($propertyMetadata->getAttributes()['swagger_context'] ?? [])
should do the trick.
e8df5cd
to
9ba21ef
Compare
@meyerbaptiste Updated. |
Thanks @mab05k! |
Maybe I just don't understand the feature, but I can't get it to work because of this guard clause: core/src/Swagger/Serializer/DocumentationNormalizer.php Lines 524 to 526 in c7e2cda
Because of it, the code in the PR is never reached, and I can't get the type to be anything else than Here is what my resource definition looks like: resources:
EMA\MD\Interfaces\Work\Dto:
shortName: Work
description: Work DTO
attributes:
filters:
- App\Infrastructure\ApiPlatform\DataProvider\WorkCollectionDataProvider
properties:
contentId:
identifier: true
type: string
attributes:
swagger_context:
type: string
enum: ['one', 'two']
example: 'one'
itemOperations:
get:
method: 'GET'
path: /works/{id}
collectionOperations:
search:
method: 'GET'
path: /works
|
My guess is there must be something with the entity class you are using that is causing the $propertyMetadata to resolve to a type of "null". I would need to see the rest of what you're working with to give you a better answer. |
It's a DTO, maybe this could explain a lot of things? namespace EMA\MD\Interfaces\Work;
final class Dto
{
public $identifier;
public $contentId;
public $runLength;
public $releaseYear;
public $releaseDate;
public $workType;
public $originalTitle;
public $countryOfOrigin;
public $title;
} |
I wrote #1466 to fix this. |
I actually have used a DTO for this. Try adding annotations of the types. And make sure you type hint your setter. /*
* @var string|null
*/
private $contentId; |
Thanks, using the comment works! |
…rty-swagger-context Add feature to update swagger context for properties