Skip to content

Error in parseBodySchema() when sending empty POST request while using allOf in requestBody schema #216

@stevch01

Description

@stevch01

Hello!

On v2.1.2 (and dev-master) I've found that when the requestBody content uses the allOf keyword, I think it skips passed this check https://github.com/hotmeteor/spectator/blob/master/src/Validation/RequestValidator.php#L158 and goes into $actualBodySchema = $this->parseBodySchema();

However, if an empty request is passed through, parseBodySchema() will try to return an array [] and causes the following exception:

{"exception":"TypeError","message":"Spectator\\Validation\\RequestValidator::parseBodySchema(): Return value must be of type stdClass, array returned"}

This is a simplified example of what the OpenAPI schema looks like:

paths:
  /test:
    post:
      summary: An endpoint
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestRequest'
        required: true
      responses:
        '201':
          description: A response
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: 
                    type: string
components:
  schemas:
    TestRequest:
      allOf:
        - $ref: '#/components/schemas/SharedSchema'
        - type: object 
          properties:
            added_property:
              type: string
              description: An additional object property
    SharedSchema:
      type: object
      properties:
        name:
          type: string

This this is how we're trying to test that an empty request body causes a 422:
$this->postJson('/test', [], $token);

I’ve tried a quick fix by adding another condition to the if check, but I’m not sure if that’s the right approach.
|| $expectedBodyRawSchema->allOf

Thank you for your time 🙂

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