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

Response Validation Always Passes if Spec Contains Object With No Properties #201

Closed
spodmore opened this issue Nov 20, 2024 · 1 comment

Comments

@spodmore
Copy link
Contributor

Hello,

I noticed that if a response specification has an item defined as object with no properties, the response is always considered valid even if it is in fact not valid.

For example, consider this definition in the spec (in a yaml file, with only relevant stuff):

  data:
    ...
    required:
      - id
      - date
      - custom_data
    properties:
      id:
        type: integer
        description: The unique ID
        example: 1
      date:
        type: integer
        description: The UNIX timestamp
        example: 1724750000
      custom_data:
        type:
          - object
          - 'null'
        description: Custom data

Having a response like this

{
  "data": [
    {
      "date": "abcdefg",
      "custom_data": null
    }
  ]
}

using

$this->response->assertValidRequest()
    ->assertValidResponse(Response::HTTP_OK)

you'd expect the validation to fail with an error like this

The properties must match schema: data
All array items must match schema
The required properties (id) are missing

object++ <== The properties must match schema: data
    data: array <== All array items must match schema
        object++ <== The required properties (id) are missing
            id*: integer
            date*: integer
...

But, even with an incorrect response (missing required id and date being a string), the response validation passes.

As soon as I add a property to custom_data, the validation fails as expected. The same thing applies to the array type without any items.

The thing is, I don't care what the structure of the custom_data object is, it could be any. In Stoplight, where I maintain the API spec, I can create an object without adding any properties.

So I had to work around this issue by specifying a dummy foo-bar object, listing all possible types, except object and array (because, apparently, it is what the issue is about):

    custom_data:
      type:
        - object
        - 'null'
      description: Custom data
      properties:
        foo:
          type:
            - string
            - integer
            - number
            - boolean
            - 'null'

And now the test fails as expected if an incorrect response is being returned.

I would expect validation to be performed correctly, and not skipped, if the spec contains an object with no properties or an array with no items.

PHP version is 7.4; Laravel 8; Spectator 1.8.0

Thank you.

@bastien-phi
Copy link
Collaborator

Spectator 1.8.0 is not maintained anymore. I would advise to upgrade to Laravel 11 and Spectator 2

@bastien-phi bastien-phi closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2025
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

No branches or pull requests

2 participants