1515package  openapi
1616
1717import  (
18+ 	"errors" 
19+ 	"fmt" 
1820	"sort" 
21+ 	"strings" 
1922
2023	"github.com/getkin/kin-openapi/openapi3" 
2124	"github.com/mongodb/openapi/tools/cli/internal/apiversion" 
@@ -53,11 +56,22 @@ func extractVersions(oas *openapi3.T) ([]string, error) {
5356				if  response .Value  ==  nil  ||  response .Value .Content  ==  nil  {
5457					continue 
5558				}
56- 				for  contentType  :=  range  response .Value .Content  {
59+ 				for  contentType ,  contentTypeValue  :=  range  response .Value .Content  {
5760					version , err  :=  apiversion .Parse (contentType )
58- 					if  err  = =  nil  {
59- 						versions [ version ]  =   struct {}{} 
61+ 					if  err  ! =  nil  {
62+ 						continue 
6063					}
64+ 
65+ 					if  apiversion .IsPreviewSabilityLevel (version ) {
66+ 						// parse if it is public or not 
67+ 						version , err  =  getPreviewVersionName (contentTypeValue )
68+ 						if  err  !=  nil  {
69+ 							fmt .Printf ("failed to parse preview version name: %v\n " , err )
70+ 							continue 
71+ 						}
72+ 					}
73+ 
74+ 					versions [version ] =  struct {}{}
6175				}
6276			}
6377		}
@@ -66,6 +80,66 @@ func extractVersions(oas *openapi3.T) ([]string, error) {
6680	return  mapKeysToSortedSlice (versions ), nil 
6781}
6882
83+ func  getPreviewVersionName (contentTypeValue  * openapi3.MediaType ) (name  string , err  error ) {
84+ 	public , name , err  :=  parsePreviewExtensionData (contentTypeValue )
85+ 	if  err  !=  nil  {
86+ 		return  "" , err 
87+ 	}
88+ 
89+ 	if  public  {
90+ 		return  "preview" , nil 
91+ 	}
92+ 
93+ 	if  ! public  &&  name  !=  ""  {
94+ 		return  "private-preview-"  +  name , nil 
95+ 	}
96+ 
97+ 	return  "" , errors .New ("no preview extension found" )
98+ }
99+ 
100+ func  parsePreviewExtensionData (contentTypeValue  * openapi3.MediaType ) (public  bool , name  string , err  error ) {
101+ 	// Expected formats: 
102+ 	// 
103+ 	//   "x-xgen-preview": { 
104+ 	// 		"name": "api-registry-private-preview" 
105+ 	//   } 
106+ 	// 
107+ 	//   "x-xgen-preview": { 
108+ 	// 		"public": "true" 
109+ 	//   } 
110+ 
111+ 	name  =  "" 
112+ 	public  =  false 
113+ 
114+ 	if  contentTypeValue .Extensions  ==  nil  {
115+ 		return  false , "" , errors .New ("no preview extension found" )
116+ 	}
117+ 
118+ 	previewExtension , ok  :=  contentTypeValue .Extensions ["x-xgen-preview" ]
119+ 	if  ! ok  {
120+ 		return  false , "" , errors .New ("no preview extension found" )
121+ 	}
122+ 
123+ 	previewExtensionMap , ok  :=  previewExtension .(map [string ]interface {})
124+ 	if  ! ok  {
125+ 		return  false , "" , errors .New ("no preview extension found" )
126+ 	}
127+ 
128+ 	// Reading if it's public or not 
129+ 	publicV , ok  :=  previewExtensionMap ["public" ].(string )
130+ 	if  ok  {
131+ 		public  =  strings .EqualFold (publicV , "true" )
132+ 	}
133+ 
134+ 	// Reading the name 
135+ 	nameV , ok  :=  previewExtensionMap ["name" ].(string )
136+ 	if  ok  {
137+ 		name  =  nameV 
138+ 	}
139+ 
140+ 	return  public , name , nil 
141+ }
142+ 
69143// mapKeysToSortedSlice converts map keys to a sorted slice. 
70144func  mapKeysToSortedSlice (m  map [string ]struct {}) []string  {
71145	keys  :=  make ([]string , 0 , len (m ))
0 commit comments