-
-
Notifications
You must be signed in to change notification settings - Fork 77
Fix operation_tag_defined to return correct file for multifile spec #776
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
Fix operation_tag_defined to return correct file for multifile spec #776
Conversation
|
@daveshanley please take a look |
daveshanley
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually fine, thank you, this is correct
|
You will need to set up the test properly to use the rolodex. you can find examples of how to do this in other openapi functions like the examples functions. |
|
@daveshanley I couldn't find any examples of such tests. Only one test mentions multifile somehow. But when you run it, it gives this error So basically, it doesn't work with multi-file openapi Now I think that I need to create a virtual file system, which seems complicated. Could you give me a more detailed path to example of a test with a multi-file openapi? |
|
Hi. https://github.com/pb33f/libopenapi/blob/main/document_test.go#L1522C1-L1534C2 func TestTagDefined_RunRule_MultiFile_CorrectOrigin(t *testing.T) {
// Create temp directory
tmpDir := t.TempDir()
// Main spec that references an external path
mainSpec := `openapi: 3.0.1
tags:
- name: "ValidTag"
paths:
/external:
$ref: 'external-path.yaml'`
// External file with an undefined tag
externalPath := `get:
tags:
- "UndefinedTag"
responses:
"200":
description: OK`
// Write files
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)
// Read the main spec
specBytes, err := os.ReadFile(filepath.Join(tmpDir, "main.yaml"))
assert.NoError(t, err)
// Configure document with file references enabled
config := datamodel.NewDocumentConfiguration()
config.BasePath = tmpDir
config.AllowFileReferences = true
document, err := libopenapi.NewDocumentWithConfiguration(specBytes, 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 // Important: use the resolved index
def := TagDefined{}
res := def.RunRule(nil, ctx)
// Should find one violation
assert.Len(t, res, 1)
assert.Equal(t, "tag `UndefinedTag` for `GET` operation is not defined as a global tag", res[0].Message)
// Key assertion: Origin should point to the external file, not main.yaml
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)
}The problem you have is trying to use raw yaml parsing + rolodex directly. The issue is that when using libopenapi.NewDocument() without configuration, it doesn't enable file lookups. The key is using NewDocumentWithConfiguration() with: config := datamodel.NewDocumentConfiguration()
config.BasePath = "path/to/spec/directory"
config.AllowFileReferences = trueThis tells libopenapi to create the rolodex with a local filesystem configured, which is what enables rolodex.FindNodeOrigin(node) to work correctly. |
|
@daveshanley thanks for your help, the test worked. Now everything seems to be ready. Do I need to fix the pipeline somehow? |
daveshanley
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me, thank you!
I have two files:
bipki.yamlbipkiPath.yamlIf I
lint bipki.yamlthen such error will be foundThis is not correct, because error is located in file
bipkiPath.yamlTo correct the error, I added field
Originto RuleFunctionResult as is done in other rulesI want to write test, but I am newbie in GO and this project. Are there any examples of tests in the project that work with multifile spec? I could try to write a new one based on them