(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:
Dave Shanley
2022-12-03 12:30:27 -05:00
parent 0f774a4c4b
commit b5436e8d4e
5 changed files with 115 additions and 40 deletions

View File

@@ -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)
}