Skip to content

Commit

Permalink
Merge pull request #358 from snyk/fix/conflict-components
Browse files Browse the repository at this point in the history
Fix: rename conflicting components
  • Loading branch information
jgresty authored Aug 16, 2024
2 parents d4237e2 + 8c6966e commit b51c4e3
Show file tree
Hide file tree
Showing 16 changed files with 775 additions and 401 deletions.
20 changes: 10 additions & 10 deletions collator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
inliner := NewInliner()
for k, v := range rv.T.Components.Schemas {
ref := "#/components/schemas/" + k
if current, ok := c.result.Components.Schemas[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Schemas[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Schemas[k] = v
Expand All @@ -159,7 +159,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Parameters {
ref := "#/components/parameters/" + k
if current, ok := c.result.Components.Parameters[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Parameters[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Parameters[k] = v
Expand All @@ -168,7 +168,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Headers {
ref := "#/components/headers/" + k
if current, ok := c.result.Components.Headers[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Headers[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Headers[k] = v
Expand All @@ -177,7 +177,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.RequestBodies {
ref := "#/components/requestBodies/" + k
if current, ok := c.result.Components.RequestBodies[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.RequestBodies[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.RequestBodies[k] = v
Expand All @@ -186,7 +186,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Responses {
ref := "#/components/responses/" + k
if current, ok := c.result.Components.Responses[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Responses[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Responses[k] = v
Expand All @@ -195,7 +195,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.SecuritySchemes {
ref := "#/components/securitySchemas/" + k
if current, ok := c.result.Components.SecuritySchemes[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.SecuritySchemes[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.SecuritySchemes[k] = v
Expand All @@ -204,7 +204,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Examples {
ref := "#/components/examples/" + k
if current, ok := c.result.Components.Examples[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Examples[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Examples[k] = v
Expand All @@ -213,7 +213,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Links {
ref := "#/components/links/" + k
if current, ok := c.result.Components.Links[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Links[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Links[k] = v
Expand All @@ -222,7 +222,7 @@ func (c *Collator) mergeComponents(rv *ResourceVersion) error {
}
for k, v := range rv.T.Components.Callbacks {
ref := "#/components/callbacks/" + k
if current, ok := c.result.Components.Callbacks[k]; ok && !componentsEqual(current, v) {
if current, ok := c.result.Components.Callbacks[k]; ok && !ComponentsEqual(current, v) {
inliner.AddRef(ref)
} else {
c.result.Components.Callbacks[k] = v
Expand Down Expand Up @@ -258,7 +258,7 @@ var cmpComponents = cmp.Options{
}, cmp.Ignore()),
}

func componentsEqual(x, y interface{}) bool {
func ComponentsEqual(x, y interface{}) bool {
return cmp.Equal(x, y, cmpComponents)
}

Expand Down
3 changes: 2 additions & 1 deletion collator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func TestRefRemover(t *testing.T) {
errDoc := resp400.Value.Content["application/vnd.api+json"].Schema
c.Assert(err, qt.IsNil)
c.Assert("{\"$ref\":\"../errors.yaml#/ErrorDocument\"}", qt.JSONEquals, errDoc)
vervet.RemoveRefs(errDoc)
err = vervet.RemoveRefs(errDoc)
c.Assert(err, qt.IsNil)
//nolint:lll // acked
c.Assert("{\"additionalProperties\":false,\"example\":{\"errors\":[{\"detail\":\"Permission denied for this "+
"resource\",\"status\":\"403\"}],\"jsonapi\":{\"version\":\"1.0\"}},\"properties\":{\"errors\":{\"example\":"+
Expand Down
5 changes: 4 additions & 1 deletion document.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func NewDocumentFile(specFile string) (_ *Document, returnErr error) {
if err != nil {
return nil, err
}
newRefAliasResolver(&t).resolve()
err = newRefAliasResolver(&t).resolve()
if err != nil {
return nil, err
}

l := openapi3.NewLoader()
l.IsExternalRefsAllowed = true
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/reflectwalk v1.0.2
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ github.com/maxbrunsfeld/counterfeiter/v6 v6.7.0 h1:z0CfPybq3CxaJvrrpf7Gme1psZTqH
github.com/maxbrunsfeld/counterfeiter/v6 v6.7.0/go.mod h1:RVP6/F85JyxTrbJxWIdKU2vlSvK48iCMnMXRkSz7xtg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/moby v24.0.7+incompatible h1:RrVT5IXBn85mRtFKP+gFwVLCcnNPZIgN3NVRJG9Le+4=
github.com/moby/moby v24.0.7+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
Expand Down
79 changes: 48 additions & 31 deletions inliner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,67 @@ type Inliner struct {
refs map[string]struct{}
}

func (in *Inliner) ProcessCallbackRef(ref *openapi3.CallbackRef) {
func (in *Inliner) ProcessCallbackRef(ref *openapi3.CallbackRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessExampleRef(ref *openapi3.ExampleRef) {
func (in *Inliner) ProcessExampleRef(ref *openapi3.ExampleRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessHeaderRef(ref *openapi3.HeaderRef) {
func (in *Inliner) ProcessHeaderRef(ref *openapi3.HeaderRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessLinkRef(ref *openapi3.LinkRef) {
func (in *Inliner) ProcessLinkRef(ref *openapi3.LinkRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessParameterRef(ref *openapi3.ParameterRef) {
func (in *Inliner) ProcessParameterRef(ref *openapi3.ParameterRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessRequestBodyRef(ref *openapi3.RequestBodyRef) {
func (in *Inliner) ProcessRequestBodyRef(ref *openapi3.RequestBodyRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessResponseRef(ref *openapi3.ResponseRef) {
func (in *Inliner) ProcessResponseRef(ref *openapi3.ResponseRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessSchemaRef(ref *openapi3.SchemaRef) {
func (in *Inliner) ProcessSchemaRef(ref *openapi3.SchemaRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

func (in *Inliner) ProcessSecuritySchemeRef(ref *openapi3.SecuritySchemeRef) {
func (in *Inliner) ProcessSecuritySchemeRef(ref *openapi3.SecuritySchemeRef) error {
if in.matched(ref.Ref) {
RemoveRefs(ref)
return RemoveRefs(ref)
}
return nil
}

// NewInliner returns a new Inliner instance.
Expand All @@ -81,9 +90,8 @@ func (in *Inliner) Inline(doc *openapi3.T) error {
if len(in.refs) == 0 {
return nil
}
openapiwalker.ProcessRefs(doc, in)

return nil
return openapiwalker.ProcessRefs(doc, in)
}

func (in *Inliner) matched(ref string) bool {
Expand All @@ -95,45 +103,54 @@ func (in *Inliner) matched(ref string) bool {
// fragment. If the reference has already been resolved, this has the effect of
// "inlining" the formerly referenced object when serializing the OpenAPI
// document.
func RemoveRefs(target interface{}) {
openapiwalker.ProcessRefs(target, clearRefs{})
func RemoveRefs(target interface{}) error {
return openapiwalker.ProcessRefs(target, clearRefs{})
}

type clearRefs struct {
}

func (c clearRefs) ProcessCallbackRef(ref *openapi3.CallbackRef) {
func (c clearRefs) ProcessCallbackRef(ref *openapi3.CallbackRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessExampleRef(ref *openapi3.ExampleRef) {
func (c clearRefs) ProcessExampleRef(ref *openapi3.ExampleRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessHeaderRef(ref *openapi3.HeaderRef) {
func (c clearRefs) ProcessHeaderRef(ref *openapi3.HeaderRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessLinkRef(ref *openapi3.LinkRef) {
func (c clearRefs) ProcessLinkRef(ref *openapi3.LinkRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessParameterRef(ref *openapi3.ParameterRef) {
func (c clearRefs) ProcessParameterRef(ref *openapi3.ParameterRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessRequestBodyRef(ref *openapi3.RequestBodyRef) {
func (c clearRefs) ProcessRequestBodyRef(ref *openapi3.RequestBodyRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessResponseRef(ref *openapi3.ResponseRef) {
func (c clearRefs) ProcessResponseRef(ref *openapi3.ResponseRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessSchemaRef(ref *openapi3.SchemaRef) {
func (c clearRefs) ProcessSchemaRef(ref *openapi3.SchemaRef) error {
ref.Ref = ""
return nil
}

func (c clearRefs) ProcessSecuritySchemeRef(ref *openapi3.SecuritySchemeRef) {
func (c clearRefs) ProcessSecuritySchemeRef(ref *openapi3.SecuritySchemeRef) error {
ref.Ref = ""
return nil
}
Loading

0 comments on commit b51c4e3

Please sign in to comment.