Unable to set examples for response
Hello, I'm facing kinda strange problem with response examples.
* @SWG\Response(
* response=500,
* description="Error response",
* examples={
* @SWG\Schema(
* @SWG\Property(property="data", type="object", description="Response payload", @SWG\Schema()),
* @SWG\Property(property="status", type="string", example=Message::CUSTOMER_IS_BLOCKED_MESSAGE, description="Status of request"),
* @SWG\Property(property="errors", type="array", description="Errors descriptions. Should be empty on success", example={},
* @SWG\Items(default="[]")
* )
* )}
* )
It does not seem to work properly. I got empty 500 response (just an 500 code and description). After debugging for a while, I made a conclusion, that this vendor code just skips examples regardless of value I'm passing.
protected function doMerge($data, $overwrite = false)
{
foreach ($data['examples'] ?? [] as $mimeType => $example) {
$this->examples[$mimeType] = $this->examples[$mimeType] ?? null;
MergeHelper::mergeFields($this->examples[$mimeType], $example, $overwrite);
}
$this->headers->merge($data['headers'] ?? [], $overwrite);
$this->mergeDescription($data, $overwrite);
$this->mergeExtensions($data, $overwrite);
$this->mergeRef($data, $overwrite);
$this->mergeSchema($data, $overwrite);
}
What am i missing?
By the way, I have an additional question. My responses differ only on status property. I'd like to add an example just for this property. Is there a way for this?
@GuilhemN to be honest, I doubt if this repo is valid for this issue. If it's not, please, redirect me to the proper one :)
Have you tried using examples (plural) instead? I don't remember exactly how this works in our code but from the sample you provided at least this should work.
And it should also be a solution to your problem with difference based on status: you can add several examples.
OK, I checked more carefully.
You should put @SWG\Schema out of examples and only put an array of scalar/array values in there.
Thank you very much for the quick response!
Now I have:
* @SWG\Response(
* response=500,
* description="Error response",
* examples={{1}, {2}, {3}}
* )
But the result is same: I got empty data response:

I suppose you still need an @SWG\Schema at the root of @Response to make it work
* @SWG\Response(
* @SWG\Schema(),
* response=500,
* description="Error response",
* examples={{1}, {2}, {3}}
* )

Maybe I misunderstood your message, but IMHO this is expected behavior :)
Also, I've tried to pass examples inside @SWG\Schema (and yes, ofcorse, I've got an error)
Your @SWG\Schema is empty, I believe you should specify a type such as type="integer"
No, it does not work like that.
* @SWG\Response(
* @SWG\Schema(type="integer"),
* response=500,
* description="Error response",
* examples={1, 2, 3}
* )

But if I do it like this:
* @SWG\Response(
* response=500,
* description="Error response",
* examples={"application/json": {1}, "application/xml": {2}}
* )
I will get

I suppose this examples field works in some weird way, or it's just me messing around.
So, going back to my additional question. How should I describe ONE response schema with several status field examples? This one was very hard to search and google :)
okay, I decided to use enum in this case like that
* @SWG\Property(property="status", type="string", enum={Message::INVALID_ORDER_DATA_MESSAGE, "test"}, description="Status of request"),
seems like very good workaround here. This is answer for my additional question, but the main issue was not solved, so I don't think it should be closed