Skip to content

feat: pass #[Context] attribute data to property describers#2722

Open
lacatoire wants to merge 2 commits intonelmio:5.xfrom
lacatoire:feat/context-attribute-support
Open

feat: pass #[Context] attribute data to property describers#2722
lacatoire wants to merge 2 commits intonelmio:5.xfrom
lacatoire:feat/context-attribute-support

Conversation

@lacatoire
Copy link
Copy Markdown
Contributor

Summary

Read per-property normalization context from Symfony's #[Context] serializer attribute and merge it into the serialization context before passing it to property/type describers.

Previously, the #[Context] attribute on properties was completely ignored when generating OpenAPI schemas. This meant that custom context keys (like EnumModelDescriber::FORCE_NAMES) had no effect when set via the attribute.

How it works

In ObjectModelDescriber::describe(), the ClassMetadataFactory (already injected for discriminator mapping) now also provides getAttributesMetadata(). For each property, the normalization context from #[Context] is merged with the model's base serialization context and passed to describers.

Example

use Symfony\Component\Serializer\Attribute\Context;

class MyDto
{
    public ArticleType $type; // enum values: "draft", "final"

    #[Context([EnumModelDescriber::FORCE_NAMES => true])]
    public ArticleType $typeByName; // enum names: "DRAFT", "FINAL"
}

Changes

  • ObjectModelDescriber: extract attributesMetadata alongside discriminator mapping; for each property, merge #[Context] normalization context into the per-property context passed to describeProperty()
  • describeProperty(): accept optional $propertyContext parameter, used instead of the model-level context when provided
  • Functional tests: EntityWithContext entity with #[Context] on an enum property, ContextAttributeController, and auto-generated fixture proving enum names are used

Fixes #2402

Read per-property normalization context from Symfony's #[Context]
serializer attribute and merge it with the model's serialization
context before passing it to property/type describers.

This enables custom model describers to react to context set via
#[Context] on properties. For instance, the built-in
EnumModelDescriber already supports a FORCE_NAMES context key that
switches enum output from backed values to case names — this now
works when set via the attribute.

Fixes nelmio#2402
Copilot AI review requested due to automatic review settings April 10, 2026 19:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for Symfony Serializer’s #[Context] attribute on individual properties so that per-property normalization context is merged into the serialization context passed to property/type describers during OpenAPI schema generation (addressing #2402).

Changes:

  • Extend ObjectModelDescriber to read per-property serializer metadata and merge #[Context] normalization context into the context used for describing each property.
  • Update the internal describeProperty() flow to accept a per-property context and pass it to the property/type describer.
  • Add a functional test (controller + entity + JSON fixture) proving that enum schema generation changes when EnumModelDescriber::FORCE_NAMES is set via #[Context].

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ModelDescriber/ObjectModelDescriber.php Merges per-property #[Context] normalization context into the context passed to describers.
tests/Functional/ControllerTest.php Adds a functional test case enabling Serializer attributes and exercising the new behavior.
tests/Functional/Controller/ContextAttributeController.php New endpoint fixture to generate a schema for an entity using per-property #[Context].
tests/Functional/Entity/EntityWithContext.php Defines an entity with an enum property annotated with #[Context] to force enum name output.
tests/Functional/Fixtures/ContextAttributeController.json Expected OpenAPI output showing two enum schemas (values vs names) based on per-property context.

Comment on lines +210 to +213
private function describeProperty(array|Type $types, Model $model, OA\Schema $property, string $propertyName, array $propertyContext = []): void
{
$context = [] !== $propertyContext ? $propertyContext : $model->getSerializationContext();

Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

describeProperty() treats an explicitly provided empty $propertyContext as “not provided” due to $context = $propertyContext ?: $model->getSerializationContext();. This makes it impossible to intentionally override the model context with an empty context and also doesn’t match the docblock wording (“used instead of the model-level context when provided”). Consider changing the parameter to ?array $propertyContext = null and using ?? to distinguish null from an intentionally empty array (or remove the default and always pass a context).

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.69%. Comparing base (212d490) to head (1056564).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##              5.x    #2722      +/-   ##
==========================================
+ Coverage   95.68%   95.69%   +0.01%     
==========================================
  Files          94       94              
  Lines        3079     3088       +9     
==========================================
+ Hits         2946     2955       +9     
  Misses        133      133              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

[Question]: Is using #[Context(...)] supposed to be supported?

2 participants