swagger-php icon indicating copy to clipboard operation
swagger-php copied to clipboard

How to achieve the the class based architecture for the my QueryParameter::class like we do in Responses

Open shahrukh4 opened this issue 1 year ago • 4 comments

This is not an issue, I am wondering how to achieve the class based architecture for the QueryParameter::class like we use for schema based response. like below

class Post {
    #[Property()]
    public string $post
}

Now we can use this Post::class and it will be fine

class User {
    #[Property()]
    public Post $post
}

Now we can directly use this schema as a ref and it is working fine like below

#[Response(
    response: 'MyResponse',
    description: 'MyResponse description',
    content: new JsonContent(
        ref: User
    ),
)]

So in the same way, how can I achieve the query paramter class as a schema or something and how to refer that in my paramters. I want to separate the schema, as the below code blocks will get bigger with time, so less managable. Please tell me how to do this.

#[Get(
    path: '/api/market_data/charts',
    description: 'Get data for market sector to populate charts',
    parameters: [
        new QueryParameter(
            name: 'region',
            required: true,
            schema: new Schema(type: 'string', enum: ['NA', 'EU']),
        ),
    ]
)]

shahrukh4 avatar Jan 24 '24 12:01 shahrukh4

I think at the moment the best is to explicitly use a #/.. path for those refs. Resolving class names to references does not work consistently everywhere in swagger-php ATM :/

<?php declare(strict_types=1);

/**
 * @license Apache 2.0
 */

namespace OpenApi\Tests\Fixtures\Scratch;

use OpenApi\Attributes as OAT;

#[OAT\PathParameter(name: 'itemName', description: 'The item name')]
class UsingRefsParameter
{
}

#[OAT\Response(response: 'item', description: 'Item response')]
class UsingRefsResponse
{
}

#[OAT\Info(title: 'Parameter Ref', version: '1.0.0')]
#[OAT\Get(
    path: '/item/{item_name}',
    parameters: [
        new OAT\Parameter(ref: '#/components/parameters/itemName'),
    ],
    responses: [
        new OAT\Response(response: 200, ref: '#/components/responses/item'),
    ]
)]
class UsingRefsController
{
}

will give you

openapi: 3.0.0
info:
  title: 'Parameter Ref'
  version: 1.0.0
paths:
  '/item/{item_name}':
    get:
      operationId: 6ecb3788642c6ba8ce8d99cbcd554dbe
      parameters:
        -
          $ref: '#/components/parameters/itemName'
      responses:
        '200':
          $ref: '#/components/responses/item'
components:
  responses:
    item:
      description: 'Item response'
  parameters:
    itemName:
      name: itemName
      in: path
      description: 'The item name'

swagger-ui will show that like this: image

DerManoMann avatar Jan 24 '24 21:01 DerManoMann

Yeah, thanks for the response, it is working fine. Can you tell me how to achieve the parameters from the class properties, if I go as above, I have to define various PathParamter. What I exactly need is like below, is it possible to do this with existing functionality @DerManoMann ?

#[OAT\Parameter()]
class UsingRefsParameter
{
    #[QueryParameter()]
    public string $date;

    #[QueryParameter()]
    public float $value;
}

shahrukh4 avatar Jan 25 '24 07:01 shahrukh4

Nope, that doesn't work, I think, sorry.

DerManoMann avatar Jan 25 '24 08:01 DerManoMann

Okay, no issues, trying to do it in a custom way by extending the QueryParameter::class

shahrukh4 avatar Jan 25 '24 10:01 shahrukh4