swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Unable to set examples for response

Open myshalov-taptima opened this issue 4 years ago • 9 comments

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?

myshalov-taptima avatar Oct 07 '21 07:10 myshalov-taptima

@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 :)

myshalov-taptima avatar Oct 07 '21 07:10 myshalov-taptima

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.

GuilhemN avatar Oct 07 '21 07:10 GuilhemN

OK, I checked more carefully. You should put @SWG\Schema out of examples and only put an array of scalar/array values in there.

GuilhemN avatar Oct 07 '21 07:10 GuilhemN

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:

image

myshalov-taptima avatar Oct 07 '21 07:10 myshalov-taptima

I suppose you still need an @SWG\Schema at the root of @Response to make it work

GuilhemN avatar Oct 07 '21 07:10 GuilhemN

     *     @SWG\Response(
     *         @SWG\Schema(),
     *         response=500,
     *         description="Error response",
     *         examples={{1}, {2}, {3}}
     *     )

image

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)

myshalov-taptima avatar Oct 07 '21 07:10 myshalov-taptima

Your @SWG\Schema is empty, I believe you should specify a type such as type="integer"

GuilhemN avatar Oct 07 '21 11:10 GuilhemN

No, it does not work like that.

     *     @SWG\Response(
     *         @SWG\Schema(type="integer"),
     *         response=500,
     *         description="Error response",
     *         examples={1, 2, 3}
     *     )

image

But if I do it like this:

     *     @SWG\Response(
     *         response=500,
     *         description="Error response",
     *         examples={"application/json": {1}, "application/xml": {2}}
     *     )

I will get

image

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 :)

myshalov-taptima avatar Oct 08 '21 01:10 myshalov-taptima

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

myshalov-taptima avatar Oct 08 '21 03:10 myshalov-taptima