opticdev / optic

OpenAPI linting, diffing and testing. Optic helps prevent breaking changes, publish accurate documentation and improve the design of your APIs.
https://useoptic.com
MIT License
1.36k stars 83 forks source link

Failed writing patches to openapi.yml #2727

Closed StefanLecho closed 8 months ago

StefanLecho commented 9 months ago

Describe the bug When executing optic capture openapi.yml --update interactive, I'm getting this output

√ Finished running requests
- GET /books/{book_id}
Error: Failed writing patches to C:\projects\bookstore-example\openapi.yml

To Reproduce Steps to reproduce the behavior:

  1. Go to bookstore-example
  2. Run optic capture openapi.yml --update interactive

Details (please complete the following information):

Additional context I've executed optic capture openapi.yml --update interactive with a normal and an admin powershell, but both resulted in the same error.

niclim commented 9 months ago

Could you set the env variable LOG_LEVEL to debug and rerun the command? It should provide some extra debug information

Also, if you can provide the openapi file as well that would be helpful

StefanLecho commented 9 months ago

I'm using the openapi.yml from https://github.com/opticdev/bookstore-api.

Here's the debug output:

error: Error: Expected YAML collection at id. Remaining path: type
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:89:19)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25)
      at YAMLMap.deleteIn (C:\Users\stefa\AppData\Roaming\npm\node_modules\@useoptic\optic\node_modules\yaml\dist\nodes\Collection.js:87:25),
  operations: '[{"op":"add","path":"/paths/~1books~1{book_id}/get/responses/200/content/application~1json/schema/required/-","value":"author_id"},{"op":"add","path":"/paths/~1books~1{book_id}/get/responses/200/content/application~1json/schema/properties/author_id","value":{"type":"string"}},{"op":"remove","path":"/paths/~1books~1{book_id}/get/responses/200/content/application~1json/schema/properties/id/type"},{"op":"add","path":"/paths/~1books~1{book_id}/get/responses/200/content/application~1json/schema/properties/id/oneOf","value":[{"type":"number"},{"type":"string"}]}]',
  parsed: 'openapi: 3.1.3\r\n' +
    'info:\r\n' +
    '  title: Optic Bookstore Demo Spec\r\n' +
notnmeyer commented 9 months ago

@StefanLecho can you share the contents of the optic.yml file you're using with that repo? opticdev/bookstore-api's optic config doesn't contain a capture block out of the box.

notnmeyer commented 9 months ago

oh, are you actually using https://github.com/opticdev/examples/tree/main/apps/bookstore-api by chance? i think that contextually makes a bit more sense here.

still though, i cant replicate your issue. im in a fresh Windows 11 VM with Node 18 installed and it seems to work,

PS C:\Users\nate\code\examples\apps\bookstore-api> node --version
v18.18.1

PS C:\Users\nate\code\examples\apps\bookstore-api> yarn install
yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...

Done in 15.48s.

PS C:\Users\nate\code\examples\apps\bookstore-api> optic capture openapi.yml --update interactive
yarn run v1.22.19

$ jest

PASS src/__tests__/integration.test.ts (12.084 s)

  book endpoints
    √ GET /books (253 ms)
    √ POST /books (23 ms)
    √ GET /books/{bookId} (14 ms)
    √ PATCH /books/{bookId} (16 ms)
  author endpoints
    √ GET /authors (53 ms)
    √ POST /authors (26 ms)
    √ GET /authors/{authorId} (17 ms)
    √ PATCH /authors/{authorId} (17 ms)

Test Suites: 1 passed, 1 total
Tests:       8 passed, 8 total
Snapshots:   0 total
Time:        12.224 s
Ran all test suites.

Done in 13.75s.

√ Finished running requests
√ GET /authors/{authorId}
  ✓ 200 response, ✓ 404 response
√ PATCH /authors/{authorId}
  ✓ Request Body, ✓ 200 response, ✓ 404 response
√ GET /authors
  ✓ 200 response
√ POST /authors
  ✓ Request Body, ✓ 200 response
√ GET /books/{bookId}
  ✓ 200 response, ✓ 404 response
√ PATCH /books/{bookId}
  ✓ Request Body, ✓ 200 response, ✓ 404 response
√ GET /books
  ✓ 200 response
√ POST /books
  ✓ Request Body, ✓ 200 response, ✓ 400 response
...and 1 endpoint that did not receive traffic

if you've made any changes, can you fork the repo and share them with us?

brianjenkins94 commented 9 months ago

I might also be running into this:

✔ GET /admin/user/permissions
  ✓ 200 response
✔ GET /api/alert-configs
  ✓ 200 response
✔ GET /api/alert-notifications
  ✓ 200 response
✔ GET /api/alerts/alert-filters
  ✓ 200 response
✔ GET /api/alerts/alert-stats
  ✓ 200 response
⠋ GET /api/alerts
Error: Failed writing patches to C:\Users\User\Documents\GitHub\customer-success\openapi.json
{
  location: 'patch files',
  error: TypeError: Cannot read properties of undefined (reading 'content')
      at applyOperation (C:\Users\User\Documents\GitHub\customer-success\node_modules\fast-json-patch\commonjs\core.js:242:22)
      at Object.applyPatch (C:\Users\User\Documents\GitHub\customer-success\node_modules\fast-json-patch\commonjs\core.js:280:22)
      at writePatchesToFiles (C:\Users\User\Documents\GitHub\customer-success\node_modules\@useoptic\optic\build\commands\capture\write\file.js:45:56)
      at diffExistingEndpoint (C:\Users\User\Documents\GitHub\customer-success\node_modules\@useoptic\optic\build\commands\capture\actions\documented.js:198:41)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async processCaptures (C:\Users\User\Documents\GitHub\customer-success\node_modules\@useoptic\optic\build\commands\capture\capture.js:339:9)
      at async C:\Users\User\Documents\GitHub\customer-success\node_modules\@useoptic\optic\build\commands\capture\capture.js:149:25
      at async Command.<anonymous> (C:\Users\User\Documents\GitHub\customer-success\node_modules\@useoptic\optic\build\error-handler.js:18:14),
  operations: '[{"op":"remove","path":"/paths/~1api~1alerts/get/responses/200/content/application~1json/schema/properties/content/items/required/1"}, ...

OS and version: Windows 10 Optic version: 0.53.20 NodeJS version: 21.1.0

Same issue with 0.54.3.

Is there a cache I need to clear? I removed a few paths from the openapi.json file so they could be rediscovered, but the file is still perfectly valid.

If there's no cache then it could be Windows-specific.

niclim commented 9 months ago

@brianjenkins94 that looks like a separate issue - could you open up a different issue with the full debug output and your openapi spec (trimmed down to the endpoint that is failing)? The debug output includes request + responses that it's patching against

notnmeyer commented 8 months ago

@StefanLecho please let us know if you're able to provide more detailed replication steps. closing this for now. thanks!

brianjenkins94 commented 7 months ago

Is there a local cache though? If so, where is it?