From d73fde2e428cf4c1bd5cdd522171f43fc715d789 Mon Sep 17 00:00:00 2001 From: "Benjamin Nolan (TwoWholeWorms)" Date: Mon, 9 Jan 2023 17:13:24 +0100 Subject: [PATCH] chore: Adds tests for optional circular references to low/references.go --- datamodel/low/reference_test.go | 203 +++++++++++++++++++++++++++++++- 1 file changed, 198 insertions(+), 5 deletions(-) diff --git a/datamodel/low/reference_test.go b/datamodel/low/reference_test.go index f8ff450..4942d4c 100644 --- a/datamodel/low/reference_test.go +++ b/datamodel/low/reference_test.go @@ -133,10 +133,42 @@ func TestIsCircular_LookupFromJourney(t *testing.T) { assert.True(t, IsCircular(ref, idx)) } -func TestIsCircular_LookupFromLoopPoint(t *testing.T) { +func TestIsCircular_LookupFromJourney_Optional(t *testing.T) { yml := `components: schemas: + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + yml = `$ref: '#/components/schemas/Something'` + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + ref, err := LocateRefNode(idxNode.Content[0], idx) + assert.NoError(t, err) + assert.True(t, IsCircular(ref, idx)) +} + +func TestIsCircular_LookupFromLoopPoint(t *testing.T) { + yml := `components: + schemas: Something: properties: nothing: @@ -170,6 +202,38 @@ func TestIsCircular_LookupFromLoopPoint(t *testing.T) { assert.True(t, IsCircular(ref, idx)) } +func TestIsCircular_LookupFromLoopPoint_Optional(t *testing.T) { + yml := `components: + schemas: + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + yml = `$ref: '#/components/schemas/Nothing'` + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + ref, err := LocateRefNode(idxNode.Content[0], idx) + assert.NoError(t, err) + assert.True(t, IsCircular(ref, idx)) +} + func TestIsCircular_FromRefLookup(t *testing.T) { yml := `components: @@ -211,6 +275,42 @@ func TestIsCircular_FromRefLookup(t *testing.T) { assert.False(t, IsCircular(idxNode.Content[0], idx)) } +func TestIsCircular_FromRefLookup_Optional(t *testing.T) { + yml := `components: + schemas: + NotCircle: + description: not a circle + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + yml = `$ref: '#/components/schemas/Nothing'` + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + assert.True(t, IsCircular(idxNode.Content[0], idx)) + + yml = `$ref: '#/components/schemas/NotCircle'` + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + assert.False(t, IsCircular(idxNode.Content[0], idx)) +} + func TestIsCircular_NoNode(t *testing.T) { assert.False(t, IsCircular(nil, nil)) } @@ -220,7 +320,6 @@ func TestGetCircularReferenceResult_NoNode(t *testing.T) { } func TestGetCircularReferenceResult_FromJourney(t *testing.T) { - yml := `components: schemas: Something: @@ -256,11 +355,43 @@ func TestGetCircularReferenceResult_FromJourney(t *testing.T) { circ := GetCircularReferenceResult(ref, idx) assert.NotNil(t, circ) assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) +} +func TestGetCircularReferenceResult_FromJourney_Optional(t *testing.T) { + yml := `components: + schemas: + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + yml = `$ref: '#/components/schemas/Something'` + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + ref, err := LocateRefNode(idxNode.Content[0], idx) + assert.NoError(t, err) + circ := GetCircularReferenceResult(ref, idx) + assert.NotNil(t, circ) + assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) } func TestGetCircularReferenceResult_FromLoopPoint(t *testing.T) { - yml := `components: schemas: Something: @@ -296,11 +427,43 @@ func TestGetCircularReferenceResult_FromLoopPoint(t *testing.T) { circ := GetCircularReferenceResult(ref, idx) assert.NotNil(t, circ) assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) +} +func TestGetCircularReferenceResult_FromLoopPoint_Optional(t *testing.T) { + yml := `components: + schemas: + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + yml = `$ref: '#/components/schemas/Nothing'` + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + ref, err := LocateRefNode(idxNode.Content[0], idx) + assert.NoError(t, err) + circ := GetCircularReferenceResult(ref, idx) + assert.NotNil(t, circ) + assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) } func TestGetCircularReferenceResult_FromMappedRef(t *testing.T) { - yml := `components: schemas: Something: @@ -334,11 +497,41 @@ func TestGetCircularReferenceResult_FromMappedRef(t *testing.T) { circ := GetCircularReferenceResult(idxNode.Content[0], idx) assert.NotNil(t, circ) assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) +} +func TestGetCircularReferenceResult_FromMappedRef_Optional(t *testing.T) { + yml := `components: + schemas: + Something: + properties: + nothing: + $ref: '#/components/schemas/Nothing' + Nothing: + properties: + something: + $ref: '#/components/schemas/Something' +` + + var iNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &iNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&iNode) + + yml = `$ref: '#/components/schemas/Nothing'` + + resolve := resolver.NewResolver(idx) + errs := resolve.CheckForCircularReferences() + assert.Len(t, errs, 0) + + var idxNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNode) + + circ := GetCircularReferenceResult(idxNode.Content[0], idx) + assert.NotNil(t, circ) + assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath()) } func TestGetCircularReferenceResult_NothingFound(t *testing.T) { - yml := `components: schemas: NotCircle: