diff --git a/plugins/pacto-plugin-openapi-infer/README.md b/plugins/pacto-plugin-openapi-infer/README.md index 1ab30cb..a27b0be 100644 --- a/plugins/pacto-plugin-openapi-infer/README.md +++ b/plugins/pacto-plugin-openapi-infer/README.md @@ -81,6 +81,24 @@ pacto generate openapi-infer . --option output=interfaces/openapi.json pacto generate openapi-infer . --option output=api/spec.yaml ``` +### `source` (optional) + +Path to the directory containing the source code to scan. This is useful when +the source code lives outside the bundle directory (e.g., the Go source is at +the repository root while the bundle is in `pactos/my-service/`). + +- **Default**: the bundle directory itself +- Relative paths are resolved relative to the bundle directory +- Absolute paths are used as-is + +```bash +# Source code is two levels up from the bundle directory +pacto generate openapi-infer pactos/my-service --option source=../.. + +# Absolute path +pacto generate openapi-infer pactos/my-service --option source=/path/to/repo +``` + ### `framework` (optional) Override automatic framework detection. Useful when the project has diff --git a/plugins/pacto-plugin-openapi-infer/cmd/main.go b/plugins/pacto-plugin-openapi-infer/cmd/main.go index 3b63d51..3c987c4 100644 --- a/plugins/pacto-plugin-openapi-infer/cmd/main.go +++ b/plugins/pacto-plugin-openapi-infer/cmd/main.go @@ -53,6 +53,18 @@ func main() { format = infer.FormatJSON } + // Allow overriding the source directory via --option source=DIR. + // This is useful when the Go/Python source lives outside the bundle + // directory (e.g., repo root vs pactos/my-bundle/). + sourceDir := req.BundleDir + if src, _ := req.Options["source"].(string); src != "" { + if filepath.IsAbs(src) { + sourceDir = src + } else { + sourceDir = filepath.Join(req.BundleDir, src) + } + } + var spec string var fw infer.Framework var err error @@ -60,9 +72,9 @@ func main() { // Allow explicit framework override via --option framework=X. if fwStr, _ := req.Options["framework"].(string); fwStr != "" { fw = infer.Framework(strings.ToLower(fwStr)) - spec, err = infer.InferWithFramework(req.BundleDir, fw, format) + spec, err = infer.InferWithFramework(sourceDir, fw, format) } else { - spec, fw, err = infer.Infer(req.BundleDir, format) + spec, fw, err = infer.Infer(sourceDir, format) } if err != nil {