openapi-library / OpenAPIValidators

Use Jest or Chai to assert that HTTP responses satisfy an OpenAPI spec
MIT License
189 stars 35 forks source link

False positive when validating with schema type: string and format: binary #275

Open Zubnix opened 2 years ago

Zubnix commented 2 years ago

Are you using jest or chai? Jest

Are you using OpenAPI 2, 3.0.X, or 3.1.0? 3.0.x

Describe the bug clearly Verification fails when using binary string format, incorrectly expects plain string.

Steps to reproduce the bug:

  1. Define an openapi (response) spec with
    content:
    application/octet-stream:
    schema:
      type: string
      format: binary
  2. Return binary data.
  3. See error (please paste error output or a screenshot)
Error: expect(received).toSatisfyApiSpec() // Matches 'received' to a response defined in your API spec, then validates 'received' against it

expected received to satisfy the '200' response defined for endpoint 'GET /webfd/{fd}' in your API spec

received did not satisfy it because: response must be string

received contained: { body: <Buffer 01 02> }

The '200' response defined for endpoint 'GET /webfd/{fd}' in API spec: {
  '200': {
    description: 'Content sent succesfully.',
    content: {
      'application/octet-stream': { schema: { type: 'string', format: 'binary' } }
    }
  }
}

What did you expect to happen instead? Verification should succeed.

Are you going to resolve the issue? No, as I don't know here to look.

rwalle61 commented 2 years ago

Thanks @Zubnix! Would you posting a reproduction link / example of how you receive a response as a Buffer? Then I can check the code and point you / someone else in the right direction 🙂

balazsbencs-sc commented 2 years ago

I also have the same issue with this in a Nest project: I have an endpoint generating a PDF and in the controller I do nothing else just returning a StreamableFile. This StreamableFile gets the input as a Buffer from puppeteer - pretty simple setup. When running test again the openapi documentation using supertest, in the console it prints out as a buffer. See examples below from my code:

    received did not satisfy it because: response must be string

    received contained: {
      body: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 12824 more bytes>
    }

test:

    test('returns pdf with application/octet-stream', async () => {
      const agent = supertest(app.getHttpServer());
      const res = await agent
        .post('/v1/report/generate')
        .send([......]);
      expect(res).toSatisfyApiSpec();
    });

example openapi doc:

  /v1/report/generate:
    post:
      tags:
        - reports
      description: '...'
      summary: '...'
      operationId: generateReport
      requestBody:
        $ref: '#/components/requestBodies/........'
      responses:
        '201':
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
          description: ......