Skip to content

[Feature]: (fully) support Examples on Property #1988

@bramslob

Description

@bramslob

Problem Statement

swagger-php v6 exposes examples on OA\Property and OA\Schema, but schema/property examples still do not work correctly for OpenAPI 3.1.

There appear to be two related issues.

First, OA\Property(..., examples: [new OA\Examples(...)]) is accepted by the attribute constructor API, but the nested examples do not merge or serialize. From code inspection, Attributes\Property accepts examples, but Annotations\Property::$_nested does not allow Examples::class, and Annotations\Examples::$_parents does not allow Property::class.

Second, OA\Schema(..., examples: [new OA\Examples(...)]) appears to model schema examples using Example Object semantics, while schema examples should be emitted as a plain array of literal example values. In other words, parameter/media-type examples and schema examples are being treated the same, but they are different shapes in OpenAPI.

This looks related to issue #1560 , but the current behavior in 6.1.0 still appears incomplete for schema/property examples.

When I have this piece of code

   use OpenApi\Attributes as OA;

    // 

    #[OA\Property(description: 'Name of the Category', example: 'Category name', examples: [new OA\Examples(example: 'default', summary: 'Category name example', value: 'Category name')])]
    private ?string $name = null;

and try to build the OpenAPI schema using https://github.com/nelmio/NelmioApiDocBundle, I get the following warning:

User Warning: Unexpected @OA\Examples(), expected to be inside @OA\Components, @OA\Schema, @OA\Parameter, @OA\PathParameter, @OA\MediaType, @OA\JsonContent, @OA\XmlContent in \App\Entity\Category->name   
  in [directory]/src/Entity/Category.php 

Proposed Solution

Complete the implementation by:

  • allowing Examples::class under Annotations\Property::$_nested
  • allowing Property::class in Annotations\Examples::$_parents
  • handling schema/property examples using schema semantics, not parameter/media-type Example Object semantics

Alternatives Considered

No response

Priority

priority/medium

Area

None

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions