88 "sort"
99 "strings"
1010
11+ orderedmap "github.com/wk8/go-ordered-map/v2"
12+
1113 "github.com/getkin/kin-openapi/openapi2"
1214 "github.com/getkin/kin-openapi/openapi3"
1315)
@@ -69,16 +71,14 @@ func ToV3(doc2 *openapi2.T) (*openapi3.T, error) {
6971 }
7072 }
7173
72- if paths := doc2 .Paths ; len (paths ) != 0 {
73- doc3Paths := make (map [string ]* openapi3.PathItem , len (paths ))
74- for path , pathItem := range paths {
75- r , err := ToV3PathItem (doc2 , & doc3 .Components , pathItem , doc2 .Consumes )
76- if err != nil {
77- return nil , err
78- }
79- doc3Paths [path ] = r
74+ doc3 .Paths = openapi3 .NewPathsWithCapacity (doc2 .Paths .Len ())
75+ for pair := doc2 .Paths .Iter (); pair != nil ; pair = pair .Next () {
76+ path , pathItem := pair .Key , pair .Value
77+ r , err := ToV3PathItem (doc2 , & doc3 .Components , pathItem , doc2 .Consumes )
78+ if err != nil {
79+ return nil , err
8080 }
81- doc3 .Paths = doc3Paths
81+ doc3 .Paths . Set ( path , r )
8282 }
8383
8484 if responses := doc2 .Responses ; len (responses ) != 0 {
@@ -556,9 +556,9 @@ func ToV3SecurityScheme(securityScheme *openapi2.SecurityScheme) (*openapi3.Secu
556556 result .Type = "oauth2"
557557 flows := & openapi3.OAuthFlows {}
558558 result .Flows = flows
559- scopesMap := make ( map [string ] string )
559+ scopesMap := orderedmap. New [string , string ]( len ( securityScheme . Scopes ) )
560560 for scope , desc := range securityScheme .Scopes {
561- scopesMap [ scope ] = desc
561+ scopesMap . Set ( scope , desc )
562562 }
563563 flow := & openapi3.OAuthFlow {
564564 AuthorizationURL : securityScheme .AuthorizationURL ,
@@ -628,7 +628,8 @@ func FromV3(doc3 *openapi3.T) (*openapi2.T, error) {
628628 if isHTTP {
629629 doc2 .Schemes = append (doc2 .Schemes , "http" )
630630 }
631- for path , pathItem := range doc3 .Paths {
631+ for pair := doc3 .Paths .Iter (); pair != nil ; pair = pair .Next () {
632+ path , pathItem := pair .Key , pair .Value
632633 if pathItem == nil {
633634 continue
634635 }
@@ -654,7 +655,7 @@ func FromV3(doc3 *openapi3.T) (*openapi2.T, error) {
654655 params = append (params , p )
655656 }
656657 sort .Sort (params )
657- doc2 .Paths [ path ] .Parameters = params
658+ doc2 .Paths . Value ( path ) .Parameters = params
658659 }
659660
660661 for name , param := range doc3 .Components .Parameters {
@@ -1179,9 +1180,9 @@ func FromV3SecurityScheme(doc3 *openapi3.T, ref *openapi3.SecuritySchemeRef) (*o
11791180 return nil , nil
11801181 }
11811182
1182- result .Scopes = make (map [string ]string , len ( flow .Scopes ))
1183- for scope , desc := range flow .Scopes {
1184- result .Scopes [scope ] = desc
1183+ result .Scopes = make (map [string ]string , flow .Scopes . Len ( ))
1184+ for pair := flow .Scopes . Oldest (); pair != nil ; pair = pair . Next () {
1185+ result .Scopes [pair . Key ] = pair . Value
11851186 }
11861187 }
11871188 default :
@@ -1204,15 +1205,13 @@ func stripNonCustomExtensions(extensions map[string]interface{}) {
12041205}
12051206
12061207func addPathExtensions (doc2 * openapi2.T , path string , extensionProps openapi3.ExtensionProps ) {
1207- paths := doc2 .Paths
1208- if paths == nil {
1209- paths = make (map [string ]* openapi2.PathItem )
1210- doc2 .Paths = paths
1208+ if doc2 .Paths == nil {
1209+ doc2 .Paths = openapi2 .NewPaths ()
12111210 }
1212- pathItem := paths [ path ]
1211+ pathItem := doc2 . Paths . Value ( path )
12131212 if pathItem == nil {
12141213 pathItem = & openapi2.PathItem {}
1215- paths [ path ] = pathItem
1214+ doc2 . Paths . Set ( path , pathItem )
12161215 }
12171216 pathItem .ExtensionProps = extensionProps
12181217}
0 commit comments