@@ -16,7 +16,9 @@ package openapi
1616
1717import (
1818 "log"
19+ "strings"
1920
21+ "github.com/getkin/kin-openapi/openapi3"
2022 "github.com/mongodb/openapi/tools/cli/internal/openapi/errors"
2123 "github.com/tufin/oasdiff/diff"
2224 "github.com/tufin/oasdiff/load"
@@ -68,7 +70,7 @@ func (o OasDiff) mergePaths() error {
6870
6971 for k , v := range pathsToMerge .Map () {
7072 if ok := basePaths .Value (k ); ok == nil {
71- basePaths .Set (k , v )
73+ basePaths .Set (k , removeExternalRefs ( v ) )
7274 } else {
7375 return errors.PathConflictError {
7476 Entry : k ,
@@ -80,6 +82,130 @@ func (o OasDiff) mergePaths() error {
8082 return nil
8183}
8284
85+ // removeExternalRefs updates the external references of OASes to remove the reference to openapi-mms.json.
86+ // Example of an external ref is "$ref": "openapi-mms.json#/components/responses/internalServerError"
87+ // Example of an external ref after removeExternalRefs: "$ref": "#/components/responses/internalServerError"
88+ func removeExternalRefs (path * openapi3.PathItem ) * openapi3.PathItem {
89+ if path .Get != nil {
90+ updateExternalRefResponses (path .Get .Responses )
91+ updateExternalRefParams (& path .Get .Parameters )
92+ }
93+
94+ if path .Put != nil {
95+ updateExternalRefResponses (path .Put .Responses )
96+ updateExternalRefParams (& path .Put .Parameters )
97+ updateExternalRefReqBody (path .Put .RequestBody )
98+ }
99+
100+ if path .Post != nil {
101+ updateExternalRefResponses (path .Post .Responses )
102+ updateExternalRefParams (& path .Post .Parameters )
103+ updateExternalRefReqBody (path .Post .RequestBody )
104+ }
105+
106+ if path .Patch != nil {
107+ updateExternalRefResponses (path .Patch .Responses )
108+ updateExternalRefParams (& path .Patch .Parameters )
109+ updateExternalRefReqBody (path .Patch .RequestBody )
110+ }
111+
112+ if path .Delete != nil {
113+ updateExternalRefResponses (path .Delete .Responses )
114+ updateExternalRefParams (& path .Delete .Parameters )
115+ }
116+
117+ return path
118+ }
119+
120+ // updateExternalRefResponses updates the external references of OASes to remove the reference to openapi-mms.json
121+ // in the Responses.
122+ // A Response can have an external ref in Response.Ref or in its content (Response.Content.Schema.Ref)
123+ func updateExternalRefResponses (responses * openapi3.Responses ) {
124+ if responses == nil {
125+ return
126+ }
127+
128+ for _ , v := range responses .Map () {
129+ if strings .Contains (v .Ref , ".json#" ) {
130+ v .Ref = removeExternalRef (v .Ref )
131+ continue
132+ }
133+
134+ if v .Value == nil || v .Value .Content == nil {
135+ continue
136+ }
137+
138+ updateExternalRefContent (& v .Value .Content )
139+ }
140+ }
141+
142+ func removeExternalRef (ref string ) string {
143+ pos := strings .Index (ref , "#" )
144+ if pos == - 1 {
145+ return ref
146+ }
147+
148+ return ref [pos :]
149+ }
150+
151+ // updateExternalRefContent updates the external references of OASes to remove the reference to openapi-mms.json
152+ // in the Schema.Content.
153+ func updateExternalRefContent (content * openapi3.Content ) {
154+ for _ , value := range * content {
155+ if value .Schema == nil {
156+ continue
157+ }
158+
159+ if strings .Contains (value .Schema .Ref , ".json#" ) {
160+ value .Schema .Ref = removeExternalRef (value .Schema .Ref )
161+ }
162+ }
163+ }
164+
165+ // updateExternalRefParams updates the external references of OASes to remove the reference to openapi-mms.json
166+ // in the Parameters.
167+ // A Parameter can have an external ref in Parameter.Ref or in its content (Parameter.Content.Schema.Ref)
168+ func updateExternalRefParams (params * openapi3.Parameters ) {
169+ if params == nil {
170+ return
171+ }
172+
173+ for _ , v := range * params {
174+ if strings .Contains (v .Ref , ".json#" ) {
175+ v .Ref = removeExternalRef (v .Ref )
176+ continue
177+ }
178+
179+ if v .Value == nil || v .Value .Content == nil {
180+ continue
181+ }
182+
183+ updateExternalRefContent (& v .Value .Content )
184+ }
185+ }
186+
187+ // updateExternalRefReqBody updates the external references of OASes to remove the reference to openapi-mms.json
188+ // in the RequestBody.
189+ // A RequestBody can have an external ref in RequestBody.Ref or in its content (RequestBody.Content.Schema.Ref)
190+ func updateExternalRefReqBody (reqBody * openapi3.RequestBodyRef ) {
191+ if reqBody == nil {
192+ return
193+ }
194+
195+ if reqBody .Ref != "" {
196+ if strings .Contains (reqBody .Ref , ".json#" ) {
197+ reqBody .Ref = removeExternalRef (reqBody .Ref )
198+ }
199+ return
200+ }
201+
202+ if reqBody .Value == nil || reqBody .Value .Content == nil {
203+ return
204+ }
205+
206+ updateExternalRefContent (& reqBody .Value .Content )
207+ }
208+
83209func (o OasDiff ) mergeTags () error {
84210 tagsToMerge := o .external .Spec .Tags
85211 if len (tagsToMerge ) == 0 {
0 commit comments