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

Request body validation is not working for allOf object referencing #2575

Open
darkoandreev opened this issue Aug 1, 2024 · 1 comment
Open

Comments

@darkoandreev
Copy link

darkoandreev commented Aug 1, 2024

Context

When using the Stoplight mock server, I encountered an issue where the validation does not work as expected when an object uses allOf and includes constraints such as required, minLength, maxLength, etc. This issue affects users who rely on accurate validation responses for API testing and mocking.

Current Behavior

Instead of throwing an error for invalid data, the mock server returns a 200 OK status, incorrectly indicating that the validation passed. For instance, sending an empty string for a required field does not trigger any validation errors.

Expected Behavior

The mock server should return a validation error when the input does not meet the defined schema constraints, such as missing required fields or failing to meet minLength requirements.

Possible Workaround/Solution

After investigating, I found that downgrading the @stoplight/json package to version 3.21.3 resolves the issue. It seems that newer versions might have introduced a regression or incompatibility with the current Prism setup.

Steps to Reproduce

  1. Define a new path with request body (object) in the Stoplight project using allOf with validation constraints.
  2. Send a request with data that does not meet the validation criteria (e.g., missing required fields or invalid field lengths).
  3. Observe that the server incorrectly returns a 200 OK status.

Part of the YAML API specification:

  /test:
    post:
      summary: ''
      operationId: post-test
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/MyTestRequest'
        
  MyTestRequest:
    title: MyTestRequest
    type: object
    properties:
      currencyCode:
        $ref: '#/components/schemas/TestCurrency'
    required:
      - currencyCode
      
  TestCurrency:
    type: string
    title: TestCurrency
    minLength: 3
    maxLength: 3

Example request body:

{
  "currencyCode": ""
}
@darkoandreev darkoandreev changed the title Validation Not Working for allOf Objects with Constraints Request body validation is not working for allOf object referencing Aug 1, 2024
@TowhidKashem
Copy link

Thanks, downgrading to 3.2.3 did the trick! (there was a typo, it's not 3.21.3 for anyone else reading this)

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