Skip to content

Commit

Permalink
fix: component extension merge
Browse files Browse the repository at this point in the history
PR #269 introduces a bug with the upgrade of kin-openapi. Component
extensions are not correctly identified and considered as part of
merging specs. However, a nil pointer exception can occur when no
extensions are specified as part of a spec.

This PR correctly handles this nil pointer and add tests to validate
behaviour.
  • Loading branch information
jlourenc committed Feb 7, 2023
1 parent 54e762a commit d74dcb8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func mergeTags(dst, src *openapi3.T, replace bool) {
}

func initDestinationComponents(dst, src *openapi3.T) {
if src.Components.Extensions != nil && dst.Components.Extensions == nil {
dst.Components.Extensions = make(map[string]interface{})
}
if src.Components.Schemas != nil && dst.Components.Schemas == nil {
dst.Components.Schemas = make(map[string]*openapi3.SchemaRef)
}
Expand Down
12 changes: 12 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func TestMergeComponents(t *testing.T) {
dst := mustLoadFile(c, "merge_test_dst.yaml")
vervet.Merge(dst, src, false)

c.Assert(dst.Components.Extensions["x-snyk-extension-0"], openapiCmp, dstOrig.Components.Extensions["x-snyk-extension-0"])
c.Assert(dst.Components.Extensions["x-snyk-extension-1"], openapiCmp, dstOrig.Components.Extensions["x-snyk-extension-1"])
c.Assert(dst.Components.Extensions["x-snyk-extension-2"], openapiCmp, src.Components.Extensions["x-snyk-extension-2"])

c.Assert(dst.Components.Schemas["Foo"], openapiCmp, dstOrig.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
Expand Down Expand Up @@ -66,6 +70,10 @@ func TestMergeComponents(t *testing.T) {
dst := mustLoadFile(c, "merge_test_dst.yaml")
vervet.Merge(dst, src, true)

c.Assert(dst.Components.Extensions["x-snyk-extension-0"], openapiCmp, dstOrig.Components.Extensions["x-snyk-extension-0"])
c.Assert(dst.Components.Extensions["x-snyk-extension-1"], openapiCmp, src.Components.Extensions["x-snyk-extension-1"])
c.Assert(dst.Components.Extensions["x-snyk-extension-2"], openapiCmp, src.Components.Extensions["x-snyk-extension-2"])

c.Assert(dst.Components.Schemas["Foo"], openapiCmp, src.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
Expand Down Expand Up @@ -102,6 +110,10 @@ func TestMergeComponents(t *testing.T) {
dst := mustLoadFile(c, "merge_test_dst_missing_components.yaml")
vervet.Merge(dst, src, true)

c.Assert(dst.Components.Extensions["x-snyk-extension-0"], openapiCmp, dstOrig.Components.Extensions["x-snyk-extension-0"])
c.Assert(dst.Components.Extensions["x-snyk-extension-1"], openapiCmp, src.Components.Extensions["x-snyk-extension-1"])
c.Assert(dst.Components.Extensions["x-snyk-extension-2"], openapiCmp, src.Components.Extensions["x-snyk-extension-2"])

c.Assert(dst.Components.Schemas["Foo"], openapiCmp, src.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
Expand Down
5 changes: 2 additions & 3 deletions testdata/merge_test_dst.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
components:
x-snyk-extension-0: "extension-0"
x-snyk-extension-1: "extension-1"
schemas:
Foo:
type: object
Expand Down Expand Up @@ -73,6 +75,3 @@ components:
value: foo-natured
Baz:
value: bazil
x-extension:
key0: value0
key1: value1
5 changes: 2 additions & 3 deletions testdata/merge_test_src.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
components:
x-snyk-extension-1: "extension-11"
x-snyk-extension-2: "extension-2"
schemas:
Foo:
type: object
Expand Down Expand Up @@ -69,6 +71,3 @@ components:
value: foo
Bar:
value: bar
x-extension:
key1: value11
key2: value2
3 changes: 1 addition & 2 deletions versionware/example/releases/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import "embed"

// Embed compiled OpenAPI specs in Go projects.

// Versions contains OpenAPI specs for each distinct release version.
//
//go:embed 2021-11-01~experimental/spec.json
//go:embed 2021-11-01~experimental/spec.yaml
//go:embed 2021-11-08~experimental/spec.json
//go:embed 2021-11-08~experimental/spec.yaml
//go:embed 2021-11-20~experimental/spec.json
//go:embed 2021-11-20~experimental/spec.yaml
// Versions contains OpenAPI specs for each distinct release version.
var Versions embed.FS

0 comments on commit d74dcb8

Please sign in to comment.