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

Required response field for nested objects #903

Closed
1 task done
xkrupa12 opened this issue Oct 22, 2024 · 0 comments · Fixed by #905
Closed
1 task done

Required response field for nested objects #903

xkrupa12 opened this issue Oct 22, 2024 · 0 comments · Fixed by #905
Labels
bug Something isn't working triage

Comments

@xkrupa12
Copy link
Contributor

Scribe version

4.38

PHP version

8.3

Framework

Laravel

Framework version

v11.27.2

Scribe config

------ SAME AS DEFAULT CONFIG ------

What happened?

First off cheers to @yannick-softwerft for delivering the feature itself. I happen to need it myself, but I had troubles marking response fields as required on nested objects in OpenAPI specs.

I have an endpoint that provides list of entities, response looks like this:

{
  "data": [
    {
      "name": "Wallet 1",
      "primary": true,
      "currency": "EUR",
      "available_balance": 99.5
    },
    {
      "name": "Wallet 2",
      "primary": false,
      "currency": "EUR",
      "available_balance": 0.0
    }
  ]
}

Here's the endpoint signature and relevant part of docs:

// GetWalletsController
#[ResponseField('data', 'array', 'Wallets', required: true)]
#[ResponseField('data.name', 'string', 'Wallet name', 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'])]
#[ResponseField('data.available_balance', 'float', 'Available balance', required: true)]
public function __invoke(): JsonResponse|JsonResource
{}

Expected result should look like this:

responses:
  200:
    description: ''
    content:
      application/json:
        schema:
          type: object
          example:
            data:
              -
                name: 'Wallet name'
                primary: true
                currency: USD
                available_balance: 90.99
          properties:
            data:
              type: array
              example:
                -
                  name: 'Wallet name'
                  primary: true
                  currency: USD
                  available_balance: 90.99
              description: Wallets
              enum: []
              items:
                type: object
                properties:
                  name:
                    type: string
                    example: 'Wallet name'
                    description: 'Wallet name'
                    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
                  available_balance:
                    type: number
                    example: 90.99
                    description: 'Available balance'
                    enum: []
              required:
                - name
                - primary
                - currency
                - available_balance
          required:
            - data

I was able to generate OpenAPI specs with required response fields list, but only for the single data object in it. For array of objects I wasn't able to achieve expected results. I also tried various notations to define the fields (omitting data itself, trying to hint in index as data.*.name, etc.), but to no avail.

After some digging through the code base I was able to fix the issue and am happy to create PR for it, assuming that my approach in describing the API was correct and this is indeed a bug/missing piece.

Docs

@xkrupa12 xkrupa12 added bug Something isn't working triage labels Oct 22, 2024
peter-krupa added a commit to peter-krupa/scribe that referenced this issue Oct 25, 2024
- added check for `required` fields to the loop inspecting nested object
xkrupa12 pushed a commit to xkrupa12/scribe that referenced this issue Oct 25, 2024
- added check for `required` response fields in nested object inspection
shalvah pushed a commit that referenced this issue Nov 5, 2024
* Fixing #903 - required response fields in OpenAPI

- added check for `required` response fields in nested object inspection

* Testcase for `required` on nested objects

---------

Co-authored-by: Peter Krupa <[email protected]>
@shalvah shalvah closed this as completed Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants