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

[Question] Is it possible to use openapi-backend to validate that the response of an API match an OpenAPI spec? #731

Open
davidlag0 opened this issue Jul 21, 2024 · 5 comments

Comments

@davidlag0
Copy link

Hi,

I'm currently using jest-openapi to validate if the responses of my API match my OpenAPI spec. It does not seem to be supported anymore and I'm looking to replace the tool I'm using for this use case. Is it a use case that openapi-backend supports? I see that openapi-backend is used to mock an API by using an OpenAPI spec but instead of doing that, I would like to use my API code and validate the responses it provides against my OpenAPI spec.

Thank you!

@anttiviljami
Copy link
Member

Sure! Would be great actually to document how to use validateResponse in tests 👍

https://openapistack.co/docs/openapi-backend/response-validation/

@davidlag0
Copy link
Author

Wow, thank you for that very quick response! I believe the part I was most struggling with was to understand how I attach my Next.js API endpoints as handlers. Would you already have an example of how to do this?

@anttiviljami
Copy link
Member

Here’s a full stack example of using openapi-backend with nextjs. Doesn’t include tests unfortunately https://github.com/anttiviljami/openapi-stack-nextjs-starter

@davidlag0
Copy link
Author

Thank you very much, I'll give it a try and see where that leads me!

@davidlag0
Copy link
Author

I took the time to run a few tests and have hit some issues. I don't know if I'm doing it the right way but so far it seems to generally work so I'm wondering if I'm hitting a bug with the OpenAPI validator used.

https://github.com/davidlag0/todo-nextjs/pull/663/files

The test is code is in openapi2.test.js.

And the issue I've been trying to understand, and I'm no OpenAPI expert, is why the test fails with this error message when I use content with application/json in the spec:

> jest --testPathPattern='integration.openapi2' --runInBand

  console.log
    res: {"error":"No Task Found"}{"status":502,"err":[{"instancePath":"","schemaPath":"#/oneOf/0/type","keyword":"type","params":{"type":"array"},"message":"must be array"},{"instancePath":"","schemaPath":"#/oneOf","keyword":"oneOf","params":{"passingSchemas":null},"message":"must match exactly one schema in oneOf"}]}

      at Object.log (__tests__/integration/openapi2.test.js:82:13)

  console.log
    res headers: { 'content-type': 'application/json' }

      at Object.log (__tests__/integration/openapi2.test.js:83:13)

 FAIL  __tests__/integration/openapi2.test.js
  Tests to satisfy OpenAPI spec with OpenAPIBackend
    ✕ GET /api/tasks with empty task list (38 ms)

  ● Tests to satisfy OpenAPI spec with OpenAPIBackend › GET /api/tasks with empty task list

    expect(received).toBe(expected) // Object.is equality

    Expected: 404
    Received: 502

      83 |     console.log("res headers:", res._headers);
      84 |
    > 85 |     expect(res._getStatusCode()).toBe(404);
         |                                  ^
      86 |     expect(JSON.parse(res._getData())).toEqual(
      87 |       expect.objectContaining({
      88 |         error: "No Task Found",

      at Object.toBe (__tests__/integration/openapi2.test.js:85:34)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.215 s, estimated 2 s
Ran all test suites matching /integration.openapi2/i.

I removed the console.log lines in the code but you can see in the test response the error message and I don't understand what I need to change to get it to work.

And oddly enough, I don't even have to change the content of the response being tested (this response isn't being tested), which I find a bit strange.

Could you help me out with any pointer?

Thank you!

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