Skip to content

Commit 7bbbf8e

Browse files
committed
Upgraded extension handling.
1 parent ed47525 commit 7bbbf8e

6 files changed

Lines changed: 140 additions & 3 deletions

File tree

what-changed/model/response.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (r *ResponseChanges) TotalChanges() int {
8888
// Response Objects
8989
func (r *ResponseChanges) TotalBreakingChanges() int {
9090
c := r.PropertyChanges.TotalBreakingChanges()
91+
if r.ExtensionChanges != nil {
92+
c += r.ExtensionChanges.TotalBreakingChanges()
93+
}
9194
if r.SchemaChanges != nil {
9295
c += r.SchemaChanges.TotalBreakingChanges()
9396
}

what-changed/model/response_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,38 @@ x-toot: pooty`
329329
assert.Len(t, extChanges.GetAllChanges(), 5)
330330
assert.Equal(t, 2, extChanges.TotalBreakingChanges())
331331
}
332+
333+
func TestCompareResponse_V3_BreakingExtensionRemovalCounted(t *testing.T) {
334+
low.ClearHashCache()
335+
336+
left := `description: response
337+
x-summary: legacy summary
338+
content:
339+
application/json:
340+
schema:
341+
type: string`
342+
343+
right := `description: response
344+
content:
345+
application/json:
346+
schema:
347+
type: string`
348+
349+
var lNode, rNode yaml.Node
350+
_ = yaml.Unmarshal([]byte(left), &lNode)
351+
_ = yaml.Unmarshal([]byte(right), &rNode)
352+
353+
var lDoc v3.Response
354+
var rDoc v3.Response
355+
_ = low.BuildModel(lNode.Content[0], &lDoc)
356+
_ = low.BuildModel(rNode.Content[0], &rDoc)
357+
_ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil)
358+
_ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil)
359+
360+
changes := CompareResponse(&lDoc, &rDoc)
361+
assert.NotNil(t, changes)
362+
assert.Equal(t, 1, changes.TotalChanges())
363+
assert.Equal(t, 1, changes.TotalBreakingChanges())
364+
assert.NotNil(t, changes.ExtensionChanges)
365+
assert.Equal(t, 1, changes.ExtensionChanges.TotalBreakingChanges())
366+
}

what-changed/model/responses.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (r *ResponsesChanges) TotalBreakingChanges() int {
6666
if r.DefaultChanges != nil {
6767
c += r.DefaultChanges.TotalBreakingChanges()
6868
}
69+
if r.ExtensionChanges != nil {
70+
c += r.ExtensionChanges.TotalBreakingChanges()
71+
}
6972
return c
7073
}
7174

what-changed/model/responses_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,61 @@ func TestCompareResponses_V3_ConfigurableCodesBreaking(t *testing.T) {
558558
assert.Equal(t, 3, changes3.TotalChanges())
559559
assert.Equal(t, 1, changes3.TotalBreakingChanges()) // only the addition is breaking now
560560
}
561+
562+
func TestCompareResponses_V3_BreakingNestedResponseExtensionRemovalCounted(t *testing.T) {
563+
low.ClearHashCache()
564+
565+
left := `200:
566+
description: OK response
567+
x-summary: legacy summary`
568+
569+
right := `200:
570+
description: OK response`
571+
572+
var lNode, rNode yaml.Node
573+
_ = yaml.Unmarshal([]byte(left), &lNode)
574+
_ = yaml.Unmarshal([]byte(right), &rNode)
575+
576+
var lDoc v3.Responses
577+
var rDoc v3.Responses
578+
_ = low.BuildModel(lNode.Content[0], &lDoc)
579+
_ = low.BuildModel(rNode.Content[0], &rDoc)
580+
_ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil)
581+
_ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil)
582+
583+
changes := CompareResponses(&lDoc, &rDoc)
584+
assert.NotNil(t, changes)
585+
assert.Equal(t, 1, changes.TotalChanges())
586+
assert.Equal(t, 1, changes.TotalBreakingChanges())
587+
assert.NotNil(t, changes.ResponseChanges["200"])
588+
assert.Equal(t, 1, changes.ResponseChanges["200"].TotalBreakingChanges())
589+
}
590+
591+
func TestCompareResponses_V3_BreakingExtensionRemovalCounted(t *testing.T) {
592+
low.ClearHashCache()
593+
594+
left := `200:
595+
description: OK response
596+
x-summary: legacy summary`
597+
598+
right := `200:
599+
description: OK response`
600+
601+
var lNode, rNode yaml.Node
602+
_ = yaml.Unmarshal([]byte(left), &lNode)
603+
_ = yaml.Unmarshal([]byte(right), &rNode)
604+
605+
var lDoc v3.Responses
606+
var rDoc v3.Responses
607+
_ = low.BuildModel(lNode.Content[0], &lDoc)
608+
_ = low.BuildModel(rNode.Content[0], &rDoc)
609+
_ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil)
610+
_ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil)
611+
612+
changes := CompareResponses(&lDoc, &rDoc)
613+
assert.NotNil(t, changes)
614+
assert.Equal(t, 1, changes.TotalChanges())
615+
assert.Equal(t, 1, changes.TotalBreakingChanges())
616+
assert.NotNil(t, changes.ExtensionChanges)
617+
assert.Equal(t, 1, changes.ExtensionChanges.TotalBreakingChanges())
618+
}

what-changed/reports/summary_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestCreateSummary_OverallReport(t *testing.T) {
2929
// Callbacks are now properly counted as individual expression changes
3030
// instead of a single PropertyAdded/PropertyRemoved change
3131
assert.Equal(t, 45, report.ChangeReport[v3.PathsLabel].Total)
32-
assert.Equal(t, 10, report.ChangeReport[v3.PathsLabel].Breaking)
32+
assert.Equal(t, 11, report.ChangeReport[v3.PathsLabel].Breaking)
3333
assert.Equal(t, 3, report.ChangeReport[v3.TagsLabel].Total)
3434
assert.Equal(t, 1, report.ChangeReport[v3.ExternalDocsLabel].Total)
3535
assert.Equal(t, 2, report.ChangeReport[v3.WebhooksLabel].Total)
@@ -58,3 +58,41 @@ func TestCreateSummary_OverallReport_IncludesDocumentPropertyChanges(t *testing.
5858
assert.Equal(t, 1, report.ChangeReport[v3.InfoLabel].Total)
5959
assert.Equal(t, 0, report.ChangeReport[v3.InfoLabel].Breaking)
6060
}
61+
62+
func TestCreateSummary_OverallReport_CountsBreakingResponseExtensions(t *testing.T) {
63+
left := []byte(`openapi: 3.1.0
64+
info:
65+
title: test
66+
version: 1.0.0
67+
paths:
68+
/burgers:
69+
get:
70+
responses:
71+
"500":
72+
description: no burgers
73+
x-summary: legacy summary
74+
`)
75+
right := []byte(`openapi: 3.1.0
76+
info:
77+
title: test
78+
version: 1.0.0
79+
paths:
80+
/burgers:
81+
get:
82+
responses:
83+
"500":
84+
description: no burgers
85+
`)
86+
87+
originalDoc, originalErr := libopenapi.NewDocument(left)
88+
assert.NoError(t, originalErr)
89+
updatedDoc, updatedErr := libopenapi.NewDocument(right)
90+
assert.NoError(t, updatedErr)
91+
92+
documentChanges, compareErrs := libopenapi.CompareDocuments(originalDoc, updatedDoc)
93+
assert.Empty(t, compareErrs)
94+
95+
report := CreateOverallReport(documentChanges)
96+
assert.Equal(t, 1, report.ChangeReport[v3.PathsLabel].Total)
97+
assert.Equal(t, 1, report.ChangeReport[v3.PathsLabel].Breaking)
98+
}

what-changed/what_changed_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestCompareOpenAPIDocuments(t *testing.T) {
2626
changes := CompareOpenAPIDocuments(origDoc, modDoc)
2727
// Callbacks are now properly counted as individual expression changes
2828
assert.Equal(t, 77, changes.TotalChanges())
29-
assert.Equal(t, 19, changes.TotalBreakingChanges())
29+
assert.Equal(t, 20, changes.TotalBreakingChanges())
3030
}
3131

3232
func TestCompareSwaggerDocuments(t *testing.T) {
@@ -257,7 +257,7 @@ func ExampleCompareOpenAPIDocuments() {
257257
// Print out some interesting stats.
258258
fmt.Printf("There are %d changes, of which %d are breaking. %v schemas have changes.",
259259
changes.TotalChanges(), changes.TotalBreakingChanges(), len(schemaChanges))
260-
// Output: There are 77 changes, of which 19 are breaking. 6 schemas have changes.
260+
// Output: There are 77 changes, of which 20 are breaking. 6 schemas have changes.
261261
}
262262

263263
func TestCheckExplodedFileCheck(t *testing.T) {

0 commit comments

Comments
 (0)