Skip to content

[Laravel / JSON:API] Request body OpenAPI docs ignore serialization groups #7294

@cay89

Description

@cay89

API Platform version(s) affected: 4.1.18

Description

When using serialization groups, request body documentation doesn't match actual API behavior:

  • API behavior: POST request ignores relationships (respects denormalizationContext groups)
  • OpenAPI docs: POST request body shows relationships (ignores groups)
class ListType extends AbstractModel
{
    use IsApiResource;

    /** @use HasFactory<ListTypeFactory> */
    use HasFactory;

    protected $fillable = ['label', 'status'];

    protected function casts(): array
    {
        return [
            'status' => ListTypeStatus::class,
            'global' => 'boolean',
        ];
    }

    /**
     * @return HasMany<ListItem, $this>
     */
    public function listitems(): HasMany
    {
        return $this->hasMany(ListItem::class, 'type_key', 'key');
    }
   
    #[Groups(['list-type:read'])]
    public function getListitems() {
        return $this->listitems;
    }

    #[Groups(['list-type:read', 'list-type:write'])]
    public function getStatus(): ListTypeStatus {
        return $this->status;
    }

    #[Groups(['list-type:read', 'list-type:write'])]
    public function getLabel(): ?string {
        return $this->label;
    }

    public static function apiResource(): array
    {
        return [
            new ApiResource(
                operations: [
                    new GetCollection(
                        normalizationContext: ['groups' => ['list-type:read']],
                    ),
                    new Post(
                        normalizationContext: ['groups' => ['list-type:write']],
                        denormalizationContext: ['groups' => ['list-type:write']],
                    ),
                ]
            )
        ];
    }
}

Result

POST response correctly excludes relationships, but OpenAPI request body example still shows them.

Image

Proposed Solution

The request body documentation should respect the denormalizationContext serialization groups, similar to how response documentation respects normalizationContext groups.

Ideally, when a POST operation is configured with:

new Post(
    denormalizationContext: ['groups' => ['list-type:write']],
)

The generated OpenAPI request body schema should only include fields that belong to the list-type:write group.

Impact

This inconsistency leads to:

  • Developer confusion
  • Incorrect client code generation

This issue affects the core principle that documentation should accurately reflect API behavior. When documentation and behavior diverge, it undermines developer trust and creates maintenance overhead.

Would it be possible to implement serialization group support for request body documentation, or is there a recommended pattern we should follow to achieve consistent documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions