If you need to provide several options for incoming or outgoing data, how do you specify `examples’?
You can manually specify fields and their values, but
if I have a DTO model, shouldn’t I make a call to this model for an example?
In php, I tried to do it through a call, the example field accepts string, array, int but not the model.
I also tried using the factory, but you can’t call methods in php attributes.
There is an error
Compile Error: Constant expression contains invalid operations
The only thing that worked fine was the inheritance of OpenAPIAttributesExamples and in my class I made calls and it worked
Example my class (a handwritten example, but like this : ))
<?php
namespace AppDto;
use OpenApiAttributes as OA;
use OpenApiAttributesExamples;
use SymfonyComponentSerializerAnnotationGroups;
class AggregatedProductDataSelectExample extends Examples
{
public function __construct(
AggregatedProductDataSelect $model,
string $example,
string $summary,
) {
$this->product = $product;
$this->is_forecast = $is_forecast;
$this->max_price = $max_price;
parent::__construct(
example: $example,
summary: $summary,
value: $model->toArray()
);
}
}
Call
responses: [
new OAResponse(
response: 200,
description: 'Агрегированные данные по продуктам в периоде',
content: [
new OAJsonContent(
examples: [
new AggregatedProductDataSelectExample(
new AggregatedProductDataSelect(
is_forecast: true,
max_price: 1.0,
product: '1',
),
summary: 'test1',
example: 'test ...'
),
],
properties: [
new OAProperty(
property: 'result',
ref: new Model(type: AggregatedProductDataResponse::class, groups: ["default"])
),
]
),
]
),
]
What is the right way to do this?