daveshanley / vacuum

vacuum is the worlds fastest OpenAPI 3, OpenAPI 2 / Swagger linter and quality analysis tool. Built in go, it tears through API specs faster than you can think. vacuum is compatible with Spectral rulesets and generates compatible reports.
https://quobix.com/vacuum
MIT License
607 stars 50 forks source link

Error "unable to open the rolodex file" on x extensions and code samples #523

Open darrenspurgeon opened 3 months ago

darrenspurgeon commented 3 months ago

I'm getting the following error against $refs when linting or bundling non-YAML files. For example, if I have a x-codeSamples property and a $ref: 'echo.go'.

Redocly handles this use case (perhaps exclusively to their implementation), so in the spirit of trying to use Vacuum exclusively, I'm wondering if there's a workaround or enhancement that could be made. As reference, here's their help guide and starter project.

Error

ERROR unable to open the rolodex file, check specification references and base path
    ├ file: /Users/darren/dev/repo/torchlight/backend-services/api_specs/openapi/echo.go
    └ error: open /Users/darren/dev/repo/torchlight/backend-services/api_specs/openapi/echo.go: file does not exist

Commands

Spec files used

openapi.yaml

$ref near bottom of sample

openapi: 3.1.0
info:
  version: 1.0.0
  title: Example API
  termsOfService: https://example.com/terms/
  contact:
    name: Contact our support
    email: contact@example.com
    url: http://example.com/contact
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  description: >
    This is an **example** API to demonstrate features of the OpenAPI
    specification.
tags:
  - name: Echo
    description: "Example echo operations."
servers:
  - url: https://{tenant}/api/v1
    variables:
      tenant:
        default: www
        description: Your tenant id
  - url: https://tl.com/api/v1
paths:
  '/echo':
    get:
      tags:
        - Echo
      summary: Echo test
      description: Returns a hello
      operationId: echo
      security:
        - basic_auth: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: string
              examples:
                response:
                  value: 'Hello world!'
        '400':
          description: Unauthorized
      x-codeSamples:
        - lang: Go
          source:
            $ref: 'echo.go'
components:
  securitySchemes:
    basic_auth:
      description: Basic auth
      type: http
      scheme: basic

echo.go

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello, world!")
    })

    fmt.Println("Server listening on :8080...")
    http.ListenAndServe(":8080", nil)
}

Redocly equivalent

Recdocly's CLI bundles this just fine with an equivalent command redocly bundle openapi.yaml -o ../dist/openapi.yaml. The behavior is that the code file is inserted as either an escaped string or multiline string (seems to vary based on code language) retaining indentation and newlines.

For example, the above with Redocly would result in the following being generated in the bundled file.

x-codeSamples:
  - lang: Go
    source: "package main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n)\n\nfunc main() {\n\thttp.HandleFunc(\"/echo\", func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Fprint(w, \"Hello, world!\")\n\t})\n\n\tfmt.Println(\"Server listening on :8080...\")\n\thttp.ListenAndServe(\":8080\", nil)\n}\n"

Alternatives tried

One workaround I found was that allowed Vacuum to pass and bundle is the following. However, this means the sample code files are not "testable" as part of our test suite, so that adds some inconvience.

daveshanley commented 3 months ago

I need to look into this, technically it should be possible, but something isn't wired up correctly - or is missing.