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

Bugfix | Don't try to set null values when merging metadata #3711

Merged
merged 2 commits into from
Sep 10, 2020

Conversation

stloyd
Copy link
Contributor

@stloyd stloyd commented Sep 8, 2020

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tickets fixes #...
License MIT

Fatal error:

TypeError: Argument 1 passed to ApiPlatform\Core\Metadata\Resource\ResourceMetadata::withDescription() must be of the type string, null given, called in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/AnnotationResourceMetadataFactory.php on line 120

This was cause when trying to merge two metadata, parent & child ones. By default ApiPlatform\Core\Annotation\ApiResource (as well as parent here: ApiPlatform\Core\Metadata\Resource\ResourceMetadata) has some of fields marked as default = null, when merging right now it's not validated that child has value set, thus due too type-hint it fails with null values.

Dumps (parent + child):

^ ApiPlatform\Core\Metadata\Resource\ResourceMetadata^ {#9948
  -shortName: "Administrator"
  -description: null
  -iri: null
  -itemOperations: array:3 [
    "get" => []
    "put" => array:1 [
      "denormalization_context" => array:1 [
        "groups" => "admin_user:update"
      ]
    ]
    "delete" => []
  ]
  -collectionOperations: array:2 [
    "get" => []
    "post" => array:1 [
      "validation_groups" => array:2 [
        0 => "sylius"
        1 => "sylius_user_create"
      ]
    ]
  ]
  -subresourceOperations: null
  -graphql: null
  -attributes: array:4 [
    "route_prefix" => "admin"
    "normalization_context" => array:1 [
      "groups" => array:1 [
        0 => "admin_user:read"
      ]
    ]
    "denormalization_context" => array:1 [
      "groups" => array:1 [
        0 => "admin_user:create"
      ]
    ]
    "validation_groups" => "sylius"
  ]
}
ApiPlatform\Core\Annotation\ApiResource^ {#9922
  +collectionOperations: array:2 [
    0 => "get"
    "post" => array:2 [
      "security" => "is_granted('ROLE_ADMIN')"
      "security_message" => "Only admins can add doctors."
    ]
  ]
  +description: null
  +graphql: null
  +iri: null
  +itemOperations: array:2 [
    0 => "get"
    "patch" => array:1 [
      "security" => "is_granted('ROLE_ADMIN') or object == user"
    ]
  ]
  +shortName: null
  +subresourceOperations: null
  -cacheHeaders: null
  -denormalizationContext: null
  -deprecationReason: null
  -elasticsearch: null
  -fetchPartial: null
  -forceEager: null
  -formats: null
  -filters: null
  -hydraContext: null
  -input: null
  -maximumItemsPerPage: null
  -mercure: null
  -messenger: null
  -normalizationContext: null
  -openapiContext: null
  -order: null
  -output: null
  -paginationClientEnabled: null
  -paginationClientItemsPerPage: null
  -paginationClientPartial: null
  -paginationViaCursor: null
  -paginationEnabled: null
  -paginationFetchJoinCollection: null
  -paginationItemsPerPage: null
  -paginationMaximumItemsPerPage: null
  -paginationPartial: null
  -routePrefix: null
  -security: null
  -securityMessage: null
  -securityPostDenormalize: null
  -securityPostDenormalizeMessage: null
  -sunset: null
  -swaggerContext: null
  -validationGroups: null
  +attributes: array:3 [
    "route_prefix" => "%app.security.api_route%"
    "normalization_context" => array:1 [
      "groups" => array:1 [
        0 => "user:read"
      ]
    ]
    "denormalization_context" => array:1 [
      "groups" => array:1 [
        0 => "user:write"
      ]
    ]
  ]
}

@alanpoulain
Copy link
Member

Could you try to add a non regression unit test?

@stloyd stloyd force-pushed the patch-1 branch 3 times, most recently from 95f0bd0 to 061e53d Compare September 8, 2020 14:40
@alanpoulain
Copy link
Member

I think I see the issue, but could you give a little more context?
When does the problem occur exactly?

@stloyd
Copy link
Contributor Author

stloyd commented Sep 8, 2020

Sylius has defined resources for i.e. admin user/shop user etc.
https://github.com/Sylius/Sylius/blob/1.8/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/AdminUser.xml

But when you have own entities extending those base ones (which is 100% valid) and you add own @ApiResource it's not able to correctly "match" them (due too null issue reported here), nor it's not merging those definitions et all (which could be both bug and/or new feature).

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.

LGTM

@alanpoulain alanpoulain merged commit 1cf1097 into api-platform:2.5 Sep 10, 2020
@alanpoulain
Copy link
Member

Thanks @stloyd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants