mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 12:37:48 +00:00
(feat): Added Reference tracking to low-level model #25
When building a document, everything that IS NOT a schema is auto-resolved in the model, this is very convenient because it creates a nice tree to explore and there is no need to perform lookups to when using `$ref` instead of inline definitions. The issue is however being able to determine if the node was originally a reference or not, that data was lost, including the name of the reference used. This use case surfaced in issue #25, where the need to know what is and what is not referenced has different requirements for different applications. This update now tracks that data and makes it available in `NodeReference` and `ValueReference` for every property. Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -520,12 +520,37 @@ func TestExtractObjectRaw(t *testing.T) {
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
tag, err := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
tag, err, _, _ := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, tag)
|
||||
assert.Equal(t, "hello pizza", tag.Description.Value)
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_With_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
description: hello`
|
||||
|
||||
var idxNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
||||
assert.NoError(t, mErr)
|
||||
idx := index.NewSpecIndex(&idxNode)
|
||||
|
||||
yml = `$ref: '#/components/schemas/pizza'`
|
||||
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
tag, err, isRef, rv := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, tag)
|
||||
assert.Equal(t, "hello", tag.Description.Value)
|
||||
assert.True(t, isRef)
|
||||
assert.Equal(t, "#/components/schemas/pizza", rv)
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_Ref_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
@@ -548,7 +573,7 @@ func TestExtractObjectRaw_Ref_Circular(t *testing.T) {
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
tag, err := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
tag, err, _, _ := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.NotNil(t, tag)
|
||||
|
||||
@@ -570,7 +595,7 @@ func TestExtractObjectRaw_RefBroken(t *testing.T) {
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
tag, err := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
tag, err, _, _ := ExtractObjectRaw[*pizza](cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, tag)
|
||||
|
||||
@@ -592,7 +617,7 @@ func TestExtractObjectRaw_Ref_NonBuildable(t *testing.T) {
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
_, err := ExtractObjectRaw[*test_noGood](cNode.Content[0], idx)
|
||||
_, err, _, _ := ExtractObjectRaw[*test_noGood](cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
@@ -613,7 +638,7 @@ func TestExtractObjectRaw_Ref_AlmostBuildable(t *testing.T) {
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &cNode)
|
||||
|
||||
_, err := ExtractObjectRaw[*test_almostGood](cNode.Content[0], idx)
|
||||
_, err, _, _ := ExtractObjectRaw[*test_almostGood](cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user