symfonyswaggeropenapinelmioapidocbundle

Symfony,NelmioApiDoc,Swagger,OpenApi: Annotate a class with jsonSerialized model


I created a little example:

class Response
{
    /**
     * @OA\Property(ref=@Model(type=EncodedModel::class))
     */
    public RuntimeClass $filters;
}
class RuntimeClass implements JsonSerializable
{
    public int $field;
    public function jsonSerialize()
    {
        $model = new EncodedModel();
        $model->field = "$this->field";
        return $model;
    }
}
class EncodedModel {
    public string $field;
}

I have an endpoint which response is of model Response::class.
Response class has a field $filters of type RuntimeClass BUT when json encoded, due to JsonSerializable implemented function, it is displayed as a class EncodedModel.

My goal is to obtain in the generated api doc, that the field Response->filters is of type EncodedModel and not RuntimeClass.

I have annotated the field directly with @OA\Property(ref=@Model(type=EncodedModel::class)) and it does the trick but i don't like it so much.
As i see it, it is a RuntimeClass class responsability tell that json encoded results as a EncodedModel class and not a RuntimeClass class. Otherwise everytime i use RuntimeClass as a model field i have to remember to annotate it.

What i was hoping is that annotating the class like this would do the trick as well once for all, but it doesn't :/

/**
 * @Model(type=EncodedModel::class)
 */
class RuntimeClass implements JsonSerializable
...

Anyone know if it is possible some way?

Thank you

----------------------- EDIT

Sorry i solved myself, i was about at the solution, just wrong annotation:

/**
 * @OA\Schema(ref=@Model(type=EncodedModel::class))
 */
class RuntimeClass implements JsonSerializable
...

Thank you anyway


Solution

  • Sorry i solved myself, i was about at the solution, just wrong annotation:

    /**
     * @OA\Schema(ref=@Model(type=EncodedModel::class))
     */
    class RuntimeClass implements JsonSerializable
    ...