Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test
  • Loading branch information
Marat-Tim committed Dec 29, 2025
commit 44a08641611bbae8a6e6180574a2f81b65f2a8ac
58 changes: 58 additions & 0 deletions functions/openapi/operation_tag_defined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"github.com/daveshanley/vacuum/model"
drModel "github.com/pb33f/doctor/model"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"go.yaml.in/yaml/v4"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -113,3 +117,57 @@ paths:
assert.Len(t, res, 1)
assert.Equal(t, "tag `such_a_naughty_dog` for `GET` operation is not defined as a global tag", res[0].Message)
}

func TestTagDefined_RunRule_MultiFile_CorrectOrigin(t *testing.T) {
tmpDir := t.TempDir()

mainSpec := `
openapi: 3.0.1
tags:
- name: "ValidTag"
paths:
/external:
$ref: 'external-path.yaml'`

externalPath := `
get:
tags:
- "UndefinedTag"
responses:
"200":
description: OK`

err := os.WriteFile(filepath.Join(tmpDir, "main.yaml"), []byte(mainSpec), 0644)
assert.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpDir, "external-path.yaml"), []byte(externalPath), 0644)
assert.NoError(t, err)

config := datamodel.NewDocumentConfiguration()
config.BasePath = tmpDir
config.AllowFileReferences = true

document, err := libopenapi.NewDocumentWithConfiguration([]byte(mainSpec), config)
assert.NoError(t, err)

m, errs := document.BuildV3Model()
assert.NoError(t, errs)

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction("$", "tag-defined", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)
ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule
ctx.Index = m.Index

def := TagDefined{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "tag `UndefinedTag` for `GET` operation is not defined as a global tag", res[0].Message)

assert.NotNil(t, res[0].Origin)
assert.True(t, strings.HasSuffix(res[0].Origin.AbsoluteLocation, "external-path.yaml"),
"Expected origin to be external-path.yaml but got: %s", res[0].Origin.AbsoluteLocation)
}
Loading