Skip to content

How to use Schema and Response on the same class? #1777

@MrMeshok

Description

@MrMeshok

I want to use attributes Schema and Response on the same class, but if I use this class in ref it always transforms to #/components/responses/
Take this code for example

namespace App\Http\Controllers\Api\v1;

use OpenApi\Attributes as OA;

#[OA\Info(version: 'v1', title: 'Title')]
class TestController
{
    #[OA\Get(
        path: '/tests',
        responses: [
            new OA\Response(
                response: 200,
                description: 'List',
                content: new OA\MediaType(
                    mediaType: 'application/json',
                    schema: new OA\Schema(
                        type: 'array',
                        items: new OA\Items(ref: TestItem::class
                    )
                ),
            ),
        )],
    )]
    public function index() {}

    #[OA\Post(
        path: '/tests',
        responses: [
            new OA\Response(
                response: 200,
                ref: TestItem::class,
        )],
    )]
    public function create() {}
}

#[OA\Schema()]
#[OA\Response(
    response: 'TestItem',
    description: 'Item',
    content: new OA\JsonContent(ref: TestItem::class)
)]
class TestItem
{
    public function __construct(
        #[OA\Property()]
        public int $id,
    ) {}
}

After generation you will get this output

openapi: 3.0.0
info:
  title: Title
  version: v1
paths:
  /tests:
    get:
      operationId: 3c97f735250e388264eb4f95b8499b8a
      responses:
        '200':
          description: List
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/responses/TestItem'
    post:
      operationId: b45a34afafafd02e16125577d557dd10
      responses:
        '200':
          $ref: '#/components/responses/TestItem'
components:
  schemas:
    TestItem:
      properties:
        id:
          type: integer
      type: object
  responses:
    TestItem:
      description: Item
      content:
        application/json:
          schema:
            $ref: '#/components/responses/TestItem'

But I would expect to see this

openapi: 3.0.0
info:
  title: Title
  version: v1
paths:
  /tests:
    get:
      operationId: 3c97f735250e388264eb4f95b8499b8a
      responses:
        '200':
          description: List
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/TestItem'
    post:
      operationId: b45a34afafafd02e16125577d557dd10
      responses:
        '200':
          $ref: '#/components/responses/TestItem'
components:
  schemas:
    TestItem:
      properties:
        id:
          type: integer
      type: object
  responses:
    TestItem:
      description: Item
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/TestItem'

Is there a way to make it work without creating another class to differentiate between TestItem schema and TestItem response?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions