Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/required fields for nested objects #905

Conversation

xkrupa12
Copy link
Contributor

@xkrupa12 xkrupa12 commented Oct 25, 2024

Added check for required fields to the loop inspecting nested object. Closes #903

Example input:

use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ResponseDoc;
use Knuckles\Scribe\Attributes\ResponseField;
use Symfony\Component\HttpFoundation\Response;

#[Endpoint('List wallets', 'Lists wallets of current customer.')]
#[ResponseField('data', 'object', 'Wallets', required: true)]
#[ResponseField('data.name', 'string', 'Wallet name', required: true)]
#[ResponseField('data.technical_id', 'string', 'Unique wallet ID', required: true)]
#[ResponseField('data.primary', 'bool', 'Is primary wallet for given currency', required: true)]
#[ResponseField('data.currency', 'string', 'Wallet currency', required: true, enum: ['USD', 'EUR', 'JPY', 'SGD'])]
#[ResponseDoc(
    [
        'data' => [
            [
                'name' => 'Wallet name',
                'technical_id' => '987654',
                'primary' => true,
                'currency' => 'USD',
            ],
        ],
    ],
    Response::HTTP_OK
)]

OpenAPI spec before (relevant part - 200 response):

responses:
        200:
          description: ''
          content:
            application/json:
              schema:
                type: object
                example:
                  data:
                    -
                      name: 'Wallet name'
                      technical_id: '987654'
                      primary: true
                      currency: USD
                properties:
                  data:
                    type: array
                    example:
                      -
                        name: 'Wallet name'
                        technical_id: '987654'
                        primary: true
                        currency: USD
                    description: Wallets
                    enum: []
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                          example: 'Wallet name'
                          description: 'Wallet name'
                          enum: []
                        technical_id:
                          type: string
                          example: '987654'
                          description: 'Unique wallet ID'
                          enum: []
                        primary:
                          type: boolean
                          example: true
                          description: 'Is primary wallet for given currency'
                          enum: []
                        currency:
                          type: string
                          example: USD
                          description: 'Wallet currency'
                          enum:
                            - USD
                            - EUR
                            - JPY
                            - SGD
                required:
                  - data

OpenAPI spec after (notice required list at the end):

responses:
        200:
          description: ''
          content:
            application/json:
              schema:
                type: object
                example:
                  data:
                    -
                      name: 'Wallet name'
                      technical_id: '987654'
                      primary: true
                      currency: USD
                properties:
                  data:
                    type: array
                    example:
                      -
                        name: 'Wallet name'
                        technical_id: '987654'
                        primary: true
                        currency: USD
                    description: Wallets
                    enum: []
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                          example: 'Wallet name'
                          description: 'Wallet name'
                          enum: []
                        technical_id:
                          type: string
                          example: '987654'
                          description: 'Unique wallet ID'
                          enum: []
                        primary:
                          type: boolean
                          example: true
                          description: 'Is primary wallet for given currency'
                          enum: []
                        currency:
                          type: string
                          example: USD
                          description: 'Wallet currency'
                          enum:
                            - USD
                            - EUR
                            - JPY
                            - SGD
                    required:
                      - name
                      - technical_id
                      - primary
                      - currency
                required:
                  - data

- added check for `required` response fields in nested object inspection
@shalvah
Copy link
Contributor

shalvah commented Nov 5, 2024

Nice, thanks! 🫶

@shalvah shalvah merged commit 018cac9 into knuckleswtf:master Nov 5, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Required response field for nested objects
3 participants