Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Bug: allOf with multiple $refs #175

Closed
siggi-k opened this issue May 27, 2024 · 1 comment · Fixed by SOHELAHMED7/yii2-openapi#36
Closed

Bug: allOf with multiple $refs #175

siggi-k opened this issue May 27, 2024 · 1 comment · Fixed by SOHELAHMED7/yii2-openapi#36

Comments

@siggi-k
Copy link
Contributor

siggi-k commented May 27, 2024

PROBLEM

So that the description of the schema.yaml is complete, I would also like to show the related models (which are also returned).

This works for the description.
But unfortunately I can't generate any code afterwards.

GIVEN


openapi: 3.0.3

info:
  title: 'Proxy-Service'
  version: 1.0.0

components:

  responses:

    AccountExpanded:
      description: 'Returns one account by ID with additional information.'
      content:
        application/vnd.api+json:
          schema:
            type: object
            required:
              - status
              - Account
            properties:
              status:
                type: string
                enum:
                  - valid
                  - invalid
              Account:
                allOf:
                  - $ref: '#/components/schemas/Account'
                  - type: object
                    properties:
                      invoiceContact:
                        $ref: "#/components/schemas/Contact"
                      paymentMethod:
                        $ref: "#/components/schemas/PaymentMethod"
              errors:
                type: object
                description: only exists if status = invalid

  schemas:

    Account:
      description: Account
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          readOnly: true
        name:
          description: account name
          type: string
          maxLength: 128
        paymentMethodName:
          type: string

    Contact:
      description: Contact
      type: object
      required:
        - id
        - account
      properties:
        id:
          type: integer
          readOnly: true
        account:
          $ref: '#/components/schemas/Account'
        active:
          type: boolean
          default: false
        nickname:
          type: string

    PaymentMethod:
      type: object
      description: PaymentMethod
      x-indexes:
        - 'unique:name'
      required:
        - id
        - name
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string
          example: Bank transfer within 14 days
          maxLength: 150
          x-faker: false

paths:

  '/account/{id}':
    parameters:
      - name: Id
        in: path
        description: ID of Account
        required: true
        schema:
          type: integer

    get:
      operationId: getAccount
      summary: Account information
      responses:
        '200':
          $ref: '#/components/responses/AccountExpanded'
        '404':
          description: Account with id = "\<account_id\>" was not found.



EXECUTE

./yii gii/api

GENERATION BUG

Exception 'Error' with message 'Call to undefined method cebe\openapi\spec\Schema::getJsonReference()'

in /app/vendor/cebe/yii2-openapi/src/lib/openapi/ResponseSchema.php:52

Stack trace:
#0 /app/vendor/cebe/yii2-openapi/src/lib/openapi/ResponseSchema.php(236): cebe\yii2openapi\lib\openapi\ResponseSchema::schemaNameByRef(Object(cebe\openapi\spec\Schema))
#1 /app/vendor/cebe/yii2-openapi/src/lib/openapi/ResponseSchema.php(151): cebe\yii2openapi\lib\openapi\ResponseSchema::guessModelClassFromContent(Object(cebe\openapi\spec\MediaType))
#2 /app/vendor/cebe/yii2-openapi/src/lib/generators/RestActionGenerator.php(90): cebe\yii2openapi\lib\openapi\ResponseSchema::guessModelClass(Object(cebe\openapi\spec\Operation), 'view')
#3 /app/vendor/cebe/yii2-openapi/src/lib/generators/RestActionGenerator.php(74): cebe\yii2openapi\lib\generators\RestActionGenerator->prepareAction('get', Object(cebe\openapi\spec\Operation), Object(cebe\yii2openapi\lib\items\RouteData))
#4 /app/vendor/cebe/yii2-openapi/src/lib/generators/RestActionGenerator.php(56): cebe\yii2openapi\lib\generators\RestActionGenerator->resolvePath('/account/{id}', Object(cebe\openapi\spec\PathItem))
#5 /app/vendor/cebe/yii2-openapi/src/generator/ApiGenerator.php(469): cebe\yii2openapi\lib\generators\RestActionGenerator->generate()
#6 /app/vendor/yiisoft/yii2-gii/src/console/GenerateAction.php(53): cebe\yii2openapi\generator\ApiGenerator->generate()
#7 /app/vendor/yiisoft/yii2-gii/src/console/GenerateAction.php(36): yii\gii\console\GenerateAction->generateCode()
#8 [internal function]: yii\gii\console\GenerateAction->run()
#9 /app/vendor/yiisoft/yii2/base/Action.php(93): call_user_func_array(Array, Array)
#10 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\Action->runWithParams(Array)
#11 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('api', Array)
#12 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\console\Controller->runAction('api', Array)
#13 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('gii/api', Array)
#14 /app/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('gii/api', Array)
#15 /app/vendor/yiisoft/yii2/base/Application.php(384): yii\console\Application->handleRequest(Object(yii\console\Request))
#16 /app/yii(9): yii\base\Application->run()
#17 {main}

@SOHELAHMED7
Copy link
Contributor

I have fixed this error in SOHELAHMED7#36. I will create PR to merge it to upstream repo.

Currently, in order to use this library most effectively, create a non-DB model and use it is response schema.

# Pseudo schema
component
  schema
    AccountExpanded:
      allOf: 
        ...

  responses:

    AccountExpanded:
      description: 'Returns one account by ID with additional information.'
      content:
        application/vnd.api+json:
          schema:
            $ref: '#/components/schemas/AccountExpanded'


paths:

  '/account/{id}':
    parameters:
      - name: Id
        in: path
        description: ID of Account
        required: true
        schema:
          type: integer

    get:
      operationId: getAccount
      summary: Account information
      responses:
        '200':
          $ref: '#/components/responses/AccountExpanded'

@cebe cebe closed this as completed in 949a32f Jun 4, 2024
cebe added a commit that referenced this issue Jun 4, 2024
| Fix Issue # | Fork PR # |
| ------ | ------ |
| #175 | SOHELAHMED7#36 |
| #172 | SOHELAHMED7#37 |
| #159  | SOHELAHMED7#39 |
| #158 | SOHELAHMED7#40 |
| #178 |
SOHELAHMED7#41 |
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants