11import { Tables } from "@/utils/supabase/database.types" ;
22
33interface SwaggerSchema {
4- swagger : string ;
4+ swagger ?: string ;
5+ openapi ?: string ; // Add support for OpenAPI 3.0
56 info : {
67 title : string ;
78 description ?: string ;
89 version : string ;
910 } ;
10- host : string ;
11- basePath : string ;
12- schemes : string [ ] ;
11+ // Swagger 2.0 properties
12+ host ?: string ;
13+ basePath ?: string ;
14+ schemes ?: string [ ] ;
15+ // OpenAPI 3.0 properties
16+ servers ?: {
17+ url : string ;
18+ description ?: string ;
19+ variables ?: Record < string , any > ;
20+ } [ ] ;
1321 paths : {
1422 [ path : string ] : {
1523 [ method : string ] : {
@@ -19,10 +27,15 @@ interface SwaggerSchema {
1927 operationId : string ;
2028 parameters ?: any [ ] ;
2129 responses : any ;
30+ requestBody ?: any ; // Added for OpenAPI 3.0
2231 }
2332 }
2433 } ;
25- securityDefinitions ?: Record < string , any > ;
34+ securityDefinitions ?: Record < string , any > ; // Swagger 2.0
35+ components ?: { // OpenAPI 3.0
36+ schemas ?: Record < string , any > ;
37+ securitySchemes ?: Record < string , any > ;
38+ } ;
2639}
2740
2841export interface ParsedSwaggerResult {
@@ -35,7 +48,6 @@ export interface ParsedSwaggerResult {
3548 */
3649export function parseSwagger ( swaggerJson : string ) : ParsedSwaggerResult {
3750 try {
38-
3951 const swagger : SwaggerSchema = JSON . parse ( swaggerJson ) ;
4052
4153 // Create the service object
@@ -55,9 +67,19 @@ export function parseSwagger(swaggerJson: string): ParsedSwaggerResult {
5567 * Create a service object based on Swagger info
5668 */
5769function createServiceObject ( swagger : SwaggerSchema ) : Tables < 'services' > {
58- // Determine the base URL from host, basePath, and schemes
59- const scheme = getPreferredScheme ( swagger . schemes ) ;
60- const baseUrl = `${ scheme } ://${ swagger . host } ${ swagger . basePath } ` ;
70+ // Determine the base URL based on the specification version
71+ let baseUrl = '' ;
72+
73+ if ( swagger . swagger ) {
74+ // Swagger 2.0 format
75+ const scheme = getPreferredScheme ( swagger . schemes ) ;
76+ baseUrl = `${ scheme } ://${ swagger . host || '' } ${ swagger . basePath || '' } ` ;
77+ } else if ( swagger . openapi ) {
78+ // OpenAPI 3.0 format
79+ if ( swagger . servers && swagger . servers . length > 0 ) {
80+ baseUrl = swagger . servers [ 0 ] . url ;
81+ }
82+ }
6183
6284 // Determine authentication type
6385 const { authType, authEnabled, authConfig} = determineAuthSettings ( swagger ) ;
@@ -102,21 +124,13 @@ function getPreferredScheme(schemes?: string[]): string {
102124}
103125
104126/**
105- * Determine authentication settings from Swagger security definitions
127+ * Determine authentication settings from security definitions
106128 */
107129function determineAuthSettings ( swagger : SwaggerSchema ) : {
108130 authType : 'api_key' | 'token' | null ;
109131 authEnabled : boolean ;
110132 authConfig : any | null ;
111133} {
112- if ( ! swagger . securityDefinitions ) {
113- return {
114- authType : null ,
115- authEnabled : false ,
116- authConfig : null
117- } ;
118- }
119-
120134 // Initialize empty auth config object with the required structure
121135 const authConfig = {
122136 api_key : "" ,
@@ -126,8 +140,21 @@ function determineAuthSettings(swagger: SwaggerSchema): {
126140 api_key_location : ""
127141 } ;
128142
143+ // Check for security definitions based on the spec version
144+ const securityDefinitions = swagger . swagger
145+ ? swagger . securityDefinitions
146+ : swagger . components ?. securitySchemes ;
147+
148+ if ( ! securityDefinitions ) {
149+ return {
150+ authType : null ,
151+ authEnabled : false ,
152+ authConfig : null
153+ } ;
154+ }
155+
129156 // Check for API Key authentication
130- const apiKeyDef = Object . entries ( swagger . securityDefinitions )
157+ const apiKeyDef = Object . entries ( securityDefinitions )
131158 // eslint-disable-next-line @typescript-eslint/no-unused-vars
132159 . find ( ( [ _ , def ] ) => def . type === 'apiKey' ) ;
133160
@@ -146,7 +173,7 @@ function determineAuthSettings(swagger: SwaggerSchema): {
146173 }
147174
148175 // Check for OAuth or other token-based authentication
149- const tokenDef = Object . entries ( swagger . securityDefinitions )
176+ const tokenDef = Object . entries ( securityDefinitions )
150177 // eslint-disable-next-line @typescript-eslint/no-unused-vars
151178 . find ( ( [ _ , def ] ) => def . type === 'oauth2' || def . type === 'http' ) ;
152179
@@ -163,7 +190,7 @@ function determineAuthSettings(swagger: SwaggerSchema): {
163190 }
164191
165192 // Handle unsupported auth types
166- const unsupportedAuth = Object . entries ( swagger . securityDefinitions )
193+ const unsupportedAuth = Object . entries ( securityDefinitions )
167194 // eslint-disable-next-line @typescript-eslint/no-unused-vars
168195 . find ( ( [ _ , def ] ) => def . type !== 'apiKey' && def . type !== 'oauth2' ) ;
169196
@@ -184,6 +211,9 @@ function determineAuthSettings(swagger: SwaggerSchema): {
184211function createEndpointsArray ( swagger : SwaggerSchema , baseUrl : string ) : Tables < 'endpoints' > [ ] {
185212 const endpoints : Tables < 'endpoints' > [ ] = [ ] ;
186213
214+ // Get component schemas (for OpenAPI 3.0)
215+ const componentSchemas = swagger . openapi ? swagger . components ?. schemas : null ;
216+
187217 // Process each path and method combination
188218 for ( const [ path , pathItem ] of Object . entries ( swagger . paths ) ) {
189219 for ( const [ method , operation ] of Object . entries ( pathItem ) ) {
@@ -204,6 +234,22 @@ function createEndpointsArray(swagger: SwaggerSchema, baseUrl: string): Tables<'
204234 // Get description from the operation
205235 const description = operation . description || operation . summary || '' ;
206236
237+ // Create schema object with components references for OpenAPI 3.0
238+ const schema : any = {
239+ parameters : operation . parameters || [ ] ,
240+ responses : operation . responses ,
241+ } ;
242+
243+ // Add requestBody for OpenAPI 3.0
244+ if ( operation . requestBody ) {
245+ schema . requestBody = operation . requestBody ;
246+ }
247+
248+ // Add component schemas if available (OpenAPI 3.0)
249+ if ( componentSchemas ) {
250+ schema . components = { schemas : componentSchemas } ;
251+ }
252+
207253 // Create endpoint object
208254 const endpoint : Partial < Tables < 'endpoints' > > = {
209255 name,
@@ -212,10 +258,7 @@ function createEndpointsArray(swagger: SwaggerSchema, baseUrl: string): Tables<'
212258 regex_path,
213259 description,
214260 source : 'openapi' ,
215- schema : {
216- parameters : operation . parameters || [ ] ,
217- responses : operation . responses ,
218- } ,
261+ schema,
219262 } ;
220263
221264 endpoints . push ( endpoint as Tables < 'endpoints' > ) ;
0 commit comments